iteration question

F

felix chang

Dear all:

This is my code:

data = Array.new(5,[])
0.upto(4) do |i|
data.push(i)
end

p data


the output is :
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0,
1, 2, 3, 4]]

I am so confused why the output is not that showed below
[[0],[1],[2],[3],[4]]
 
R

Rimantas Liubertas

data = Array.new(5,[])
This gives you array with five references to the same array object:

ruby-1.9.2-p0 :036 > data = Array.new(5,[])
=> [[], [], [], [], []]

ruby-1.9.2-p0 :037 > data[0].object_id
=> 2156232780
ruby-1.9.2-p0 :038 > data[1].object_id
=> 2156232780
0.upto(4) do |i|
data.push(i)

no matter where you push you are still pushing to the same single array

What you want probably is:

data = Array.new(5) {[]}
0.upto(4) do |i|
data.push(i)
end
p data


In this case it is the same as

data = Array.new(5) {|index| [index]}
=> [[0], [1], [2], [3], [4]]



Regards,
Rimantas
 
B

bbiker

data = Array.new(5,[])
This gives you array with five references to the same array object:

ruby-1.9.2-p0 :036 > data = Array.new(5,[])
=> [[], [], [], [], []]

ruby-1.9.2-p0 :037 > data[0].object_id
=> 2156232780
ruby-1.9.2-p0 :038 > data[1].object_id
=> 2156232780
0.upto(4) do |i|
data.push(i)


no matter where you push you are still pushing to the same single array

What you want probably is:

data = Array.new(5) {[]}
0.upto(4) do |i|
data.push(i)
end
p data

In this case it is the same as

data = Array.new(5) {|index| [index]}
=> [[0], [1], [2], [3], [4]]

Regards,
Rimantas
--http://rimantas.com/


Alternate way of doing the same thing:

data = []
0.upto(4) { |i| data << }

regards,
bbiker
 
R

Robert Klemme

data =3D Array.new(5,[])
This gives you array with five references to the same array object:

ruby-1.9.2-p0 :036 > data =3D Array.new(5,[])
=3D> [[], [], [], [], []]

ruby-1.9.2-p0 :037 > data[0].object_id
=3D> 2156232780
ruby-1.9.2-p0 :038 > data[1].object_id
=3D> 2156232780
0.upto(4) do |i|
data.push(i)


no matter where you push you are still pushing to the same single array

What you want probably is:

data =3D Array.new(5) {[]}
0.upto(4) do |i|
data.push(i)
end
p data

In this case it is the same as

data =3D Array.new(5) {|index| [index]}
=3D> [[0], [1], [2], [3], [4]]

Regards,
Rimantas
--http://rimantas.com/


Alternate way of doing the same thing:

data =3D []
0.upto(4) { |i| data << }


Even simpler

irb(main):001:0> data =3D Array.new(5) {|i| }
=3D> [[0], [1], [2], [3], [4]]
irb(main):002:0> data =3D Array.new(5) {|*a| a}
=3D> [[0], [1], [2], [3], [4]]
irb(main):003:0> data =3D 5.times.map {|i| }
=3D> [[0], [1], [2], [3], [4]]

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,784
Members
47,342
Latest member
KelseyK737

Latest Threads

Top