From: Damjan Rems on

ar = Array.new( 2,[])
%w(a b).each do |e|
ar[0] << e
ar[1] << e
end
ar.each do |e|
e.each {|line| p line}
end

Returns
"a"
"a"
"b"
"b"
"a"
"a"
"b"
"b"

I would expect:
"a"
"b"
"a"
"b"

by
TheR
--
Posted via http://www.ruby-forum.com/.

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

Hi Damjan

The code ar = Array.new(n, []) actually makes n copies of the same object
(empty array), so each time you do either ar[0] << e or ar[1] << e you are
appending elements to the same array.

What I guess you want is to different objects, and you can do that by e.g.
ar = Array.new(2){[]}
(or just ar = [[],[]] ...or many other ways :o) )

Best regards,
Rolf

On Fri, Apr 23, 2010 at 2:49 PM, Damjan Rems <d_rems(a)yahoo.com> wrote:

>
> ar = Array.new( 2,[])
> %w(a b).each do |e|
> ar[0] << e
> ar[1] << e
> end
> ar.each do |e|
> e.each {|line| p line}
> end
>
> Returns
> "a"
> "a"
> "b"
> "b"
> "a"
> "a"
> "b"
> "b"
>
> I would expect:
> "a"
> "b"
> "a"
> "b"
>
> by
> TheR
> --
> Posted via http://www.ruby-forum.com/.
>
>

From: Glenn Jackman on
At 2010-04-23 08:49AM, "Damjan Rems" wrote:
>
> ar = Array.new( 2,[])

ar[0] and ar[1] both refer to the same (empty) array at this point

ar = Array.new(2, []) # => [[], []]
ar[0].object_id # => 136474520
ar[1].object_id # => 136474520

You want the block syntax for Array.new

ar = Array.new(2) {[]} # => [[], []]
ar[0].object_id # => 136514378
ar[1].object_id # => 136514364



--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
From: Robert Klemme on
2010/4/23 Rolf Pedersen <rolfhsp(a)gmail.com>:
> The code ar = Array.new(n, []) actually makes n copies of the same object
> (empty array), so each time you do either ar[0] << e or ar[1] << e you are
> appending elements to the same array.

That description is inconsistent and might confuse one or the other
reader: there are no copies made of the *object* (Array in this case)
but only of the *reference*!

irb(main):001:0> a = Array.new(3,[])
=> [[], [], []]
irb(main):002:0> a.map {|x| x.object_id}
=> [135000124, 135000124, 135000124]
irb(main):003:0> a.map {|x| x.object_id}.uniq
=> [135000124]

The idiom you describe when you say "copy" is the block form of Arra.new:

irb(main):004:0> a = Array.new(3) { [] }
=> [[], [], []]
irb(main):005:0> a.map {|x| x.object_id}
=> [135669212, 135669198, 135669184]
irb(main):006:0> a.map {|x| x.object_id}.uniq
=> [135669212, 135669198, 135669184]

> What I guess you want is to different objects, and you can do that by e.g
 | 
Pages: 1
Prev: Alarm Clock( )
Next: qtruby and pointers