Array in a Hash

  • Thread starter Damaris Fuentes
  • Start date
D

Damaris Fuentes

Hi you all,

I have the following code,where I have a hash whose entries are
initalized by an empty Array:

h = Hash.new(Array.new(0))
h["a"] << 0
h["b"] << 6
h["a"] << 5
h["b"] << 10
p h["a"]
p h["b"]

Both "p" prints the same: [0,6,5,10]
What am I doing wrong? (I want h["a"] to be [0,5] and h["b"]=[6,10]
 
J

James Gray

I have the following code,where I have a hash whose entries are
initalized by an empty Array:

h = Hash.new(Array.new(0))

h = Hash.new { |hash, key| hash[key] = Array.new }
h["a"] << 0
h["b"] << 6
h["a"] << 5
h["b"] << 10
p h["a"]
p h["b"]

Both "p" prints the same: [0,6,5,10]
What am I doing wrong? (I want h["a"] to be [0,5] and h["b"]=[6,10]

If you just pass an object to Hash.new() the exact same object is used
to initialize all entries. That's okay with something like a Fixnum
which will have to be replaced, but not intended with a mutable object
like Array. Switching to the block form as I did above forces Ruby to
run the code each time, getting us a new Array.

James Edward Gray II
 
M

Michael Libby

Hi you all,

I have the following code,where I have a hash whose entries are
initalized by an empty Array:

h = Hash.new(Array.new(0))
h["a"] << 0
h["b"] << 6
h["a"] << 5
h["b"] << 10
p h["a"]
p h["b"]

Both "p" prints the same: [0,6,5,10]
What am I doing wrong? (I want h["a"] to be [0,5] and h["b"]=[6,10]

That form of Hash.new does not create a different new Array for each
new hash key, it creates a single Array instance to which all new hash
keys refer. You might prefer the block form of Hash.new.

irb(main):001:0> h = Hash.new(Array.new(0))
=> {}
irb(main):002:0> h[:a] << 0
=> [0]
irb(main):003:0> h[:b] << 1
=> [0, 1]
irb(main):004:0> h[:a].object_id
=> 1073560370
irb(main):005:0> h[:b].object_id
=> 1073560370
irb(main):006:0> g = Hash.new{|hash, key| hash[key] = Array.new(0)}
=> {}
irb(main):007:0> g[:a] << 0
=> [0]
irb(main):008:0> g[:b] << 1
=> [1]
irb(main):009:0> g[:a].object_id
=> 1073475250
irb(main):010:0> g[:b].object_id
=> 1073464620
irb(main):011:0> g[:a] << 4
=> [0, 4]
irb(main):012:0> g[:b] << 5
=> [1, 5]

-Michael C. Libby
 

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,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top