From: Robert Klemme Date: 2009-09-08T00:08:25+09:00 Subject: Re: Two Dimensional Arrays in Ruby? 2009/9/7 Mason Kelsey : > Your helpfulness is appreciated.  Unfortunately at age 68 my eyesight is not > what it used to be.  It works fine when I insert to missing comma.  Ah! > Programming is a young person's game. > > anarray = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] > puts anarray[1][1] > puts anarray[0] > puts anarray[2][0] > > produces (SciTE) > >>ruby test_2_dimensional_array.rb > 5 > 1 > 2 > 3 > 7 >>Exit code: 0 > > I'm writing a move method for the 8-puzzle problem for my artificial > intelligence class and need a two dimensional way of representing the > positions of the tiles. You don't have to do this low level. After all, Ruby is an OO language. You can use class Matrix: $ irb19 -r matrix Ruby version 1.9.1 irb(main):001:0> m = Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 0]] => Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 0]] irb(main):002:0> m[1,1] => 5 irb(main):003:0> Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/