has any one tried this?

Discussion in 'Ruby' started by Wu Ning, Jan 3, 2008.

  1. Wu Ning

    Wu Ning Guest

    My ruby version is 1.8.6
    a = Array.new(3,Array.new())
    a[2]<<1
    puts a

    the result of a is
    [[1], [1], [1]]
    in irb..

    I don't understand why.
    I just wannt insert into a 1 into the third array of a.
    --
    Posted via http://www.ruby-forum.com/.
    Wu Ning, Jan 3, 2008
    #1
    1. Advertising

  2. On 3 Jan 2008, at 11:21, Wu Ning wrote:

    > My ruby version is 1.8.6
    > a = Array.new(3,Array.new())
    > a[2]<<1
    > puts a
    >
    > the result of a is
    > [[1], [1], [1]]
    > in irb..
    >
    > I don't understand why.

    Array.new will insert the second argument you give into the array (3
    times since that's what you've asked for). However it's the same array
    (You can see this quite easily if you do Array.new(3,Array.new).map {|
    x| x.object_id}). So a is not an array containing 3 arrays, it's an
    array containing the same array 3 times.
    a = Array.new(3) {[]}
    should do the trick (since the block is called once for each element
    of the array)

    Fred


    >
    > I just wannt insert into a 1 into the third array of a.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Frederick Cheung, Jan 3, 2008
    #2
    1. Advertising

  3. Wu Ning wrote:
    > My ruby version is 1.8.6
    > a = Array.new(3,Array.new())
    > a[2]<<1
    > puts a
    >
    > the result of a is
    > [[1], [1], [1]]
    > in irb..
    >
    > I don't understand why.
    > I just wannt insert into a 1 into the third array of a.


    After a = Array.new(3,Array.new()), a is an array containing three
    references to the same object. There is only one array, but it is
    repeated three times.

    Do
    a = []; 3.times { a << [] }
    and you'll have three different arrays.

    best,
    Dan
    --
    Posted via http://www.ruby-forum.com/.
    Daniel Lucraft, Jan 3, 2008
    #3
  4. Wu Ning

    Casimir Guest

    Wu Ning kirjoitti:
    > My ruby version is 1.8.6
    > a = Array.new(3,Array.new())
    > a[2]<<1
    > puts a


    I suppose you wanted to type

    irb(main):012:0> uber = [[], [], 3]
    => [[], [], 3]


    Csmr
    Casimir, Jan 3, 2008
    #4
  5. Wu Ning

    Wu Ning Guest

    Thanks,
    I know what's going on now,
    :)

    Daniel Lucraft wrote:
    > Wu Ning wrote:
    >> My ruby version is 1.8.6
    >> a = Array.new(3,Array.new())
    >> a[2]<<1
    >> puts a
    >>
    >> the result of a is
    >> [[1], [1], [1]]
    >> in irb..
    >>
    >> I don't understand why.
    >> I just wannt insert into a 1 into the third array of a.

    >
    > After a = Array.new(3,Array.new()), a is an array containing three
    > references to the same object. There is only one array, but it is
    > repeated three times.
    >
    > Do
    > a = []; 3.times { a << [] }
    > and you'll have three different arrays.
    >
    > best,
    > Dan


    --
    Posted via http://www.ruby-forum.com/.
    Wu Ning, Jan 3, 2008
    #5
  6. Wu Ning

    Robert Dober Guest

    On Jan 3, 2008 1:11 PM, Wu Ning <> wrote:
    >
    > Thanks,
    > I know what's going on now,
    > :)

    Good ;) Ruby has however a syntax that will do what you intended in
    the first place, see below please... and yes err
    please do not top post unless you have a very good reason to do so in
    which case I apologize for having mentioned it.
    >
    >
    > Daniel Lucraft wrote:
    > > Wu Ning wrote:
    > >> My ruby version is 1.8.6
    > >> a = Array.new(3,Array.new())


    a = Array.new(3){ [] }

    > >> a[2]<<1
    > >> puts a


    [ [],[],[1] ]

    HTH
    Robert

    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
    Robert Dober, Jan 3, 2008
    #6
    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. asanka
    Replies:
    1
    Views:
    371
    Roedy Green
    Sep 23, 2005
  2. Lad
    Replies:
    3
    Views:
    303
    Mike C. Fletcher
    Sep 5, 2005
  3. batok
    Replies:
    0
    Views:
    335
    batok
    Feb 27, 2007
  4. dd
    Replies:
    1
    Views:
    276
    Richard Heathfield
    Apr 6, 2006
  5. Snis Pilbor
    Replies:
    13
    Views:
    403
    Rod Pemberton
    May 27, 2006
Loading...

Share This Page