Problem with array in hash

S

Sergey Sheypak

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...
 
J

Jesús Gabriel y Galán

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

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

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?

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)
=> [#<struct Task id=1>]
irb(main):006:0> lol[:arr] << Task.new(2)
=> [#<struct Task id=1>, #<struct Task id=2>]
irb(main):007:0> lol[:arr] << Task.new(3)
=> [#<struct Task id=1>, #<struct Task id=2>, #<struct Task id=3>]
irb(main):008:0> lol[:arr]
#<struct Task id=2> said:
!!!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...

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.
 

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,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top