2010/3/19 Cec Tre <cecil.treadwell / gmail.com>:
> Hey, bit of a ruby noob here :)
>
> First, hello ruby-forum community.
>
> 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.
>
> Firstly, I started off with this:
>
> =A0 =A0@row_1 =3D ['x','x','x','x','x']
> =A0 =A0@row_2 =3D ['x','x','x','x','x']
> =A0 =A0@row_3 =3D ['x','x','x','x','x']
> =A0 =A0@row_4 =3D ['x','x','x','x','x']
> =A0 =A0@row_5 =3D ['x','x','x','x','x']
> =A0 =A0@grid =3D [@row_1,@row_2,@row_3,@row_4,@row_5]
>
> So, obviously this creates a 5x5 grid, in which each of the 'cells' or
> @row_1..5[1..5] =3D=3D 'x'.
>
> I used to just alter the string value in each cell by using:
>
> =A0 =A0@grid[y][x] =3D 'new string'
>
> But, I ultimately want each cell to be an array.. So that I can push and
> pop their values as they change, and then the grid will always display
> the last value in the array, by using:
>
> =A0 =A0@row_1 =3D [array.last,array1.last,array2.last,array3.last]
> =A0 =A0#etc etc etc...
>
> My problem then is, that I have a different variable name for each of
> the five points in each of the five rows.
>
> 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?
>
> Because, I'm going about implementing cell00, cell01, cell02, cell03 etc
> etc each as an array and my code is getting messy.

It's not a good idea to encode what is an index into the variable
name.  Rather you should use proper indexing.

> Please is there an (easier) way, to do what I want?

grid =3D Array.new(5) { Array.new(5) { [] } }

Now you can do

irb(main):006:0> grid[1][2]
=3D> []
irb(main):007:0> grid[1][2] << "dat"
=3D> ["dat"]
irb(main):008:0> grid[1][2]
=3D> ["dat"]
irb(main):009:0> grid[1][2] << "foo"
=3D> ["dat", "foo"]
irb(main):010:0> grid[1][2]
=3D> ["dat", "foo"]

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/