On Mar 18, 6:26=A0pm, Cec Tre <cecil.treadw... / gmail.com> wrote:
> So, here is my problem. I want to create a 5x5 grid, however, each of
> the 'cells' in the grid should be able to hold a variety of values.

rb(main):001:0> a =3D Array.new(5){ Array.new(5) }
=3D> [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil,
nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]

irb(main):002:0> require 'pp'; pp a
[[nil, nil, nil, nil, nil],
 [nil, nil, nil, nil, nil],
 [nil, nil, nil, nil, nil],
 [nil, nil, nil, nil, nil],
 [nil, nil, nil, nil, nil]]
=3D> nil

That says:
Create an array with 5 elements; for each of the five elements, call
this block and use the return value for the value of the element. When
you call the block, create a new array of 5 empty elements.

You also can use the index of the array in your block. For example:

irb(main):009:0> CHARS =3D ('A'..'Z').to_a
=3D> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

irb(main):010:0> excel =3D Array.new(5){ |row| Array.new(5){ |col|
"#{CHARS[col]}#{row+1}" } }
=3D> [["A1", "B1", "C1", "D1", "E1"], ["A2", "B2", "C2", "D2", "E2"],
["A3", "B3", "C3", "D3", "E3"], ["A4", "B4", "C4", "D4", "E4"], ["A5",
"B5", "C5", "D5", "E5"]]

irb(main):012:0> pp excel
[["A1", "B1", "C1", "D1", "E1"],
 ["A2", "B2", "C2", "D2", "E2"],
 ["A3", "B3", "C3", "D3", "E3"],
 ["A4", "B4", "C4", "D4", "E4"],
 ["A5", "B5", "C5", "D5", "E5"]]
=3D> nil


> So, the solution I really want is:
>
> Is it possible to have a variable called, say... cellxx ... Then, some
> piece of code that alters the xx part of the actual variable name
> itself?

That's a bad solution. Programmatically create or modify your array in
loops; don't create explicit temporary variables you don't need.
Here's another solution:

irb(main):013:0> a =3D []
=3D> []

irb(main):014:0> 5.times{ |i| a[i] =3D [] }
=3D> 5

irb(main):015:0> a
=3D> [[], [], [], [], []]

irb(main):016:0> a[2][3] =3D "hi"
=3D> "hi"

irb(main):017:0> a
=3D> [[], [], [nil, nil, nil, "hi"], [], []]