P
Peter Hicks
All,
When iterating over an Array of Hashes repeatedly, the 'each' method
appear to pass a reference to - not a copy of - each element in the
array.
Is there a way to take a *copy* of the data, rather than a reference to
the data, so that any changes to each hash aren't?
Explaining it another way - I have code similar to the following:
a = [ '2011-01-01', '2011-01-02', '2011-01-03' ]
b = [ { :x => "foo" }, { :x => "bar" }, { :x => "baz" } ]
a.each do |a_item|
puts a_item
b.each do |b_item|
b_item[:date] = a_item
# DO SOMETHING WITH B
end
end
What I want to do within the b.each loop is work on a *copy* of each
element of b, such that if I mess around with it, the changes are lost
when when the loop exits.
What is the magic I am missing?
Peter
When iterating over an Array of Hashes repeatedly, the 'each' method
appear to pass a reference to - not a copy of - each element in the
array.
Is there a way to take a *copy* of the data, rather than a reference to
the data, so that any changes to each hash aren't?
Explaining it another way - I have code similar to the following:
a = [ '2011-01-01', '2011-01-02', '2011-01-03' ]
b = [ { :x => "foo" }, { :x => "bar" }, { :x => "baz" } ]
a.each do |a_item|
puts a_item
b.each do |b_item|
b_item[:date] = a_item
# DO SOMETHING WITH B
end
end
What I want to do within the b.each loop is work on a *copy* of each
element of b, such that if I mess around with it, the changes are lost
when when the loop exits.
What is the magic I am missing?
Peter