Two Dimensional Arrays in Ruby?

Discussion in 'Ruby' started by Mason Kelsey, Sep 7, 2009.

  1. Mason Kelsey

    Mason Kelsey Guest

    [Note: parts of this message were removed to make it a legal post.]

    No doubt this question has been asked before but I cannot find it and it is
    not documented in three Ruby books I've checked, which is very strange as
    two dimensional arrays are a common need in programming.

    How does one define and use a two dimensional array in Ruby?

    I tried
    anarray = [[1, 2, 3], [4, 5, 6] [7, 8, 9]]
    with
    puts anarray[1][1]
    does not deliver the 5 that I would expect but instead, provides (in SciTE)
    the error message on the line defining the array:
    >ruby test_2_dimensional_array.rb

    test_2_dimensional_array.rb:1:in `[]': wrong number of arguments (3 for 2)
    (ArgumentError)
    from test_2_dimensional_array.rb:1
    >Exit code: 1


    So what is the secret for working with two dimensional arrays in Ruby? And
    is it documented somewhere?

    Thanks in advance!
    Mason Kelsey, Sep 7, 2009
    #1
    1. Advertising

  2. > Mon, Sep 7, 2009 at 12:11 PM, Mason Kelsey <> wrote:

    > anarray = [[1, 2, 3], [4, 5, 6] [7, 8, 9]]
    > puts anarray[1][1]


    Try adding a comma between the second and third array. :)
    anarray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    Harry
    --
    A Look into Japanese Ruby List in English
    http://www.kakueki.com/ruby/list.html
    Harry Kakueki, Sep 7, 2009
    #2
    1. Advertising

  3. Mason Kelsey

    7stud -- Guest

    Mason Kelsey wrote:
    >
    > So what is the secret for working with two dimensional arrays in Ruby?
    > And
    > is it documented somewhere?
    >
    > Thanks in advance!


    Maybe you should call on your 20 years of computer programming
    experience in 30 different languages to do a little debugging:


    anarray = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ]
    puts anarray[1][1]

    --output:--
    5
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Sep 7, 2009
    #3
  4. Mason Kelsey

    7stud -- Guest

    Mason Kelsey wrote:
    > And
    > is it documented somewhere?
    >


    Oh, yeah. If you look up Arrays in the index of your book, on one of
    the pages you will find something like this:

    ---
    However, in Ruby specifically, arrays are ordered collection of objects.
    ecause everything in Ruby is an object[,] [a]rrays can contain any
    combination of objects of any class.
    ---
    p. 574 Beginning Ruby: From Novice to Professional


    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Sep 7, 2009
    #4
  5. Mason Kelsey

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Sun, Sep 6, 2009 at 10:11 PM, Mason Kelsey <> wrote:

    > No doubt this question has been asked before but I cannot find it and it is
    > not documented in three Ruby books I've checked, which is very strange as
    > two dimensional arrays are a common need in programming.
    >
    > How does one define and use a two dimensional array in Ruby?
    >
    > I tried
    > anarray = [[1, 2, 3], [4, 5, 6] [7, 8, 9]]
    > with
    > puts anarray[1][1]
    > does not deliver the 5 that I would expect but instead, provides (in SciTE)
    > the error message on the line defining the array:
    > >ruby test_2_dimensional_array.rb

    > test_2_dimensional_array.rb:1:in `[]': wrong number of arguments (3 for 2)
    > (ArgumentError)
    > from test_2_dimensional_array.rb:1
    > >Exit code: 1

    >
    > So what is the secret for working with two dimensional arrays in Ruby? And
    > is it documented somewhere?
    >
    > Thanks in advance!
    >


    Hi, Mason

    In Ruby, Arrays are objects, they only have one dimension. But they can hold
    anything, including other arrays, so they can behave as multi dimensional
    arrays. To get your desired output, you would so something like this

    #a two dimensional array
    x = Array.new(3){ Array.new(3) }
    p x # => [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]

    #a two dimensional array with your desired output
    x = Array.new(3){|i| Array.new(3){|j| 3*i+j+1 } }
    x # => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    In this example, we say Array.new(3) which says to make an array with three
    indexes. Then we pass it a block, which determines how to initialize each
    index. The block accepts the variable i, which is the index being assigned,
    so it will have values 0, 1, and 2. Then for each of these indexes, we want
    it to be an Array, which gets us our second dimension. So in our block we
    assign another Array.new(3), to say an array with three indexes. We pass
    this Array the block {|j| 3*i+j+1} so j will have values 0, 1, 2 and i will
    have values 0, 1, 2. Then we multiply i by 3, because each second dimension
    has three indexes, and we add 1 because we want to begin counting at 1
    instead of zero.

    So this says "create an array with three indexes, where each index is an
    array with three indexes, whose values are 3 times the index of the 1st
    dimension, plus the second dimension, plus 1"

    Hope that helps.
    Josh Cheek, Sep 7, 2009
    #5
  6. Mason Kelsey

    Mason Kelsey Guest

    [Note: parts of this message were removed to make it a legal post.]

    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.

    Again, thanks.

    No Sam

    On Sun, Sep 6, 2009 at 11:23 PM, Harry Kakueki <> wrote:

    > > Mon, Sep 7, 2009 at 12:11 PM, Mason Kelsey <>

    > wrote:
    >
    > > anarray = [[1, 2, 3], [4, 5, 6] [7, 8, 9]]
    > > puts anarray[1][1]

    >
    > Try adding a comma between the second and third array. :)
    > anarray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >
    > Harry
    > --
    > A Look into Japanese Ruby List in English
    > http://www.kakueki.com/ruby/list.html
    >
    >
    Mason Kelsey, Sep 7, 2009
    #6
  7. Mason Kelsey

    Mason Kelsey Guest

    [Note: parts of this message were removed to make it a legal post.]

    7stud, you need not be jealous of my experience. Yes, I have learned over
    100 languages and had a career of 36 years as a programmer. But only a few
    of the languages I learned are still viable. Computer languages are dime a
    dozen and knowledge of them has a funny way of becoming irrelevant. And age
    (I'm 68) does have its own clever ways of leveling the playing field. My
    eyesight was not good enough to catch such a trivial error. If I am
    fortunate, then, I have helped you debug the problem you have. I wish you
    the best and hope you will use your talents wisely.

    No Sam

    On Sun, Sep 6, 2009 at 11:24 PM, 7stud -- <> wrote:

    > Mason Kelsey wrote:
    > >
    > > So what is the secret for working with two dimensional arrays in Ruby?
    > > And
    > > is it documented somewhere?
    > >
    > > Thanks in advance!

    >
    > Maybe you should call on your 20 years of computer programming
    > experience in 30 different languages to do a little debugging:
    >
    >
    > anarray = [
    > [1, 2, 3],
    > [4, 5, 6],
    > [7, 8, 9]
    > ]
    > puts anarray[1][1]
    >
    > --output:--
    > 5
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Mason Kelsey, Sep 7, 2009
    #7
  8. 2009/9/7 Mason Kelsey <>:
    > Your helpfulness is appreciated. =A0Unfortunately at age 68 my eyesight i=

    s not
    > what it used to be. =A0It works fine when I insert to missing comma. =A0A=

    h!
    > Programming is a young person's game.
    >
    > anarray =3D [[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 =3D Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 0]]
    =3D> Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 0]]
    irb(main):002:0> m[1,1]
    =3D> 5
    irb(main):003:0>

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Sep 7, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    435
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,924
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    653
    Vivek
    Aug 21, 2003
  4. Kev Jackson
    Replies:
    2
    Views:
    110
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page