Newbiest ? ever

Discussion in 'Ruby' started by Orion Hunter, Feb 9, 2004.

  1. Orion Hunter

    Orion Hunter Guest

    (ruby 1.8.0 Linux)

    I don't understand why I get the following behavriour:

    irb(main):001:0> matrix = Array.new( 3, Array.new( 4, 0 ) )
    => [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    irb(main):002:0> matrix[0][0] = 1
    => 1
    irb(main):003:0> matrix
    => [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
    irb(main):004:0>

    I would have expected the assignment of matrix[0][0] to produce a matrix:
    [[1,0,0,0],[0,0,0,0][0,0,0,0]]

    What am I not getting here?

    _________________________________________________________________
    Plan your next US getaway to one of the super destinations here.
    http://special.msn.com/local/hotdestinations.armx
     
    Orion Hunter, Feb 9, 2004
    #1
    1. Advertising

  2. Orion Hunter

    Kent S. Guest

    Because, matrix = Array.new( 3, Array.new( 4, 0 ) )
    creates an array with three elements all poining to the same array
    object. What you need is something like that:

    matrix = Array.new( 3 ) { Array.new( 4, 0 ) }

    /kent

    Orion Hunter wrote:
    > (ruby 1.8.0 Linux)
    >
    > I don't understand why I get the following behavriour:
    >
    > irb(main):001:0> matrix = Array.new( 3, Array.new( 4, 0 ) )
    > => [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    > irb(main):002:0> matrix[0][0] = 1
    > => 1
    > irb(main):003:0> matrix
    > => [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
    > irb(main):004:0>
    >
    > I would have expected the assignment of matrix[0][0] to produce a matrix:
    > [[1,0,0,0],[0,0,0,0][0,0,0,0]]
    >
    > What am I not getting here?
    >
    > _________________________________________________________________
    > Plan your next US getaway to one of the super destinations here.
    > http://special.msn.com/local/hotdestinations.armx
    >
    >
    >
     
    Kent S., Feb 9, 2004
    #2
    1. Advertising

  3. il Tue, 10 Feb 2004 02:56:28 +0900, "Orion Hunter"
    <> ha scritto::

    >(ruby 1.8.0 Linux)
    >
    >I don't understand why I get the following behavriour:
    >
    >irb(main):001:0> matrix = Array.new( 3, Array.new( 4, 0 ) )
    >=> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    >irb(main):002:0> matrix[0][0] = 1
    >=> 1
    >irb(main):003:0> matrix
    >=> [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
    >irb(main):004:0>
    >
    >I would have expected the assignment of matrix[0][0] to produce a matrix:
    >[[1,0,0,0],[0,0,0,0][0,0,0,0]]
    >
    >What am I not getting here?


    Array.new(num,object) just creates num refernces to object:
    irb(main):001:0> a=Array.new(3,'yo')
    => ["yo", "yo", "yo"]
    irb(main):002:0> a[0].id
    => 20896272
    irb(main):003:0> a[1].id
    => 20896272

    what you want is different object with the same value, you can do this
    with:
    irb(main):004:0> a=Array.new(3) {'yo'}
    => ["yo", "yo", "yo"]
    irb(main):005:0> a[0].id
    => 20863668
    irb(main):006:0> a[1].id
    => 20863656

    so you should write:

    matrix= Array.new(3) do
    Array.new(4,0)
    end

    or just do:
    require 'matrix'
    :)
     
    gabriele renzi, Feb 9, 2004
    #3
    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. walala
    Replies:
    8
    Views:
    698
    Ralf Hildebrandt
    Sep 24, 2003
  2. Steve M
    Replies:
    1
    Views:
    344
  3. Jerry Camel

    Why won't my download ever end?

    Jerry Camel, Feb 23, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    1,130
    Malek
    Feb 24, 2004
  4. Darren Clark
    Replies:
    1
    Views:
    454
    Andrew de la Harpe
    Apr 10, 2004
  5. Jason
    Replies:
    0
    Views:
    230
    Jason
    Jul 6, 2004
Loading...

Share This Page