changing array values

A

Andrew Salveson

What I'd like is an array of objects, each of which contains an array of
integers, and each successive object in the array should contain a
slightly different array of integers than the previous object.

For some reason, when I run the code below:

class TEST
def initialize(args)
@args = args
end
def describe
return @args.join(", ")
end
end


def test
old = [0,0,0,0,0,0]
tests = []
5.times do |i|
new = old
new[rand(old.size)] = rand(5) # randomly changes one of the members
of the array to a number from 0 to 4
tests = TEST.new(new)
puts tests.describe
end
return tests
end

I get this output:

0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 4
0, 0, 0, 2, 0, 4
0, 0, 1, 2, 0, 4
[#<TEST:0x3f3d9f0 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d9d8 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d900 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d828 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d750 @args=[0, 0, 1, 2, 0, 4]>]

As you can see, each object in the object array contains the last array
of integers, even though the objects are being initialized with
different arrays of integers.

Strangely enough, if I use a single integer everywhere I here use an
array, everything works just fine.

I cannot for the life of me figure out why this is happening and would
be extremely grateful it if someone could enlighten me . . .
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]
For some reason, when I run the code below:

class TEST
def initialize(args)
@args = args
end
def describe
return @args.join(", ")
end
end


def test
old = [0,0,0,0,0,0]
tests = []
5.times do |i|
new = old
new[rand(old.size)] = rand(5) # randomly changes one of the members
of the array to a number from 0 to 4
tests = TEST.new(new)
puts tests.describe
end
return tests
end

I get this output:

0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 4
0, 0, 0, 2, 0, 4
0, 0, 1, 2, 0, 4
[#<TEST:0x3f3d9f0 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d9d8 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d900 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d828 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d750 @args=[0, 0, 1, 2, 0, 4]>]




When you set 'new = old', you're just creating another reference to the same
array. To copy the array, use 'new = old.dup'. (dup = duplicate)
 
A

Andrew Salveson

James said:
end
[#<TEST:0x3f3d9f0 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d9d8 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d900 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d828 @args=[0, 0, 1, 2, 0, 4]>,
#<TEST:0x3f3d750 @args=[0, 0, 1, 2, 0, 4]>]



When you set 'new = old', you're just creating another reference to the
same
array. To copy the array, use 'new = old.dup'. (dup = duplicate)


Ahh, fantastic! Thank you.
 

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
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top