Problem with array in hash

require ‘p’

lol = {}
lol[:arr] = []
puts “#{lol[:arr].class} obj_id=#{lol[:arr].object_id}”

lol[:arr] + [Task.new(:id => ‘id01’)]
pp lol
puts “#{lol[:arr].class} obj_id=#{lol[:arr].object_id}”

lol[:arr] + [Task.new(:id => ‘id02’)]
pp lol
puts “#{lol[:arr].class} obj_id=#{lol[:arr].object_id}”

lol[:arr] + [Task.new(:id => ‘id03’)]
pp lol
puts “#{lol[:arr].class} obj_id=#{lol[:arr].object_id}”

Produces:
Array obj_id=23293860
{:arr=>[]}
Array obj_id=23293860
{:arr=>[]}
Array obj_id=23293860
{:arr=>[]}
Array obj_id=23293860
—What does it mean? Nothin has been added

lol = {}
lol[:arr] = []
lol[:arr] << Task.new(:id => ‘id01’)
pp lol
lol[:arr] << Task.new(:id => ‘id02’)
pp lol
lol[:arr] << Task.new(:id => ‘id03’)
pp lol

Produces:
{:arr=>[Task[|id => id01|]]}
{:arr=>[Task[|id => id02|], Task[|id => id02|]]}
{:arr=>[Task[|id => id03|], Task[|id => id03|], Task[|id => id03|]]}
—Why does each insertion replaces each elem in array?

!!!but!!!
arr = []
arr << Task.new(:id => ‘id01’)
pp arr
arr << Task.new(:id => ‘id02’)
pp arr
arr << Task.new(:id => ‘id03’)
pp arr

produces:
[Task[|id => id01|]]
[Task[|id => id02|], Task[|id => id02|]]
[Task[|id => id03|], Task[|id => id03|], Task[|id => id03|]]
—it’s ok…

On Fri, Feb 12, 2010 at 10:50 AM, Sergey S.
[email protected] wrote:

lol[:arr] + [Task.new(:id => ‘id02’)]
Array obj_id=23293860
{:arr=>[]}
Array obj_id=23293860
{:arr=>[]}
Array obj_id=23293860
—What does it mean? Nothin has been added

This is because the + method of Array doesn’t modify the array, it
creates a new one:

irb(main):009:0> a = []
=> []
irb(main):010:0> a + [1,2,3]
=> [1, 2, 3]
irb(main):011:0> a
=> []
irb(main):012:0> a.object_id
=> -605891698
irb(main):013:0> (a + [1,2,3]).object_id
=> -605917598
irb(main):014:0> (a + [1,2,3]).object_id
=> -605928248

Produces:
{:arr=>[Task[|id => id01|]]}
{:arr=>[Task[|id => id02|], Task[|id => id02|]]}
{:arr=>[Task[|id => id03|], Task[|id => id03|], Task[|id => id03|]]}
—Why does each insertion replaces each elem in array?

I don’t know what happens in the Task constructor, because this works
for me:

irb(main):001:0> Task = Struct.new :id
=> Task
irb(main):002:0> lol = {}
=> {}
irb(main):003:0> lol[:arr] = []
=> []
irb(main):005:0> lol[:arr] << Task.new(1)
=> [#]
irb(main):006:0> lol[:arr] << Task.new(2)
=> [#, #]
irb(main):007:0> lol[:arr] << Task.new(3)
=> [#, #, #]
irb(main):008:0> lol[:arr]
=> [#, #, #]

produces:
[Task[|id => id01|]]
[Task[|id => id02|], Task[|id => id02|]]
[Task[|id => id03|], Task[|id => id03|], Task[|id => id03|]]
—it’s ok…

I don’t see it ok, I see that it happens exactly the same as when the
array is in the hash. You must be doing something in the Task class
that messes with the instances or something. Can you show us the Task
class? Or show us, what do you get when you execute this:

a = Task.new(:id => ‘id01’)
p a
b = Task.new(:id => ‘id02’)
p b
p [a,b]

Jesus.

Jesús Gabriel y Galán wrote:

Jesus.
The problem is here:
String to symbol - Ruby - Ruby-Forum
Thank you for explaining.