R
Robb Shecter
I found this in an old post:
"Here's a Hash that automatically creates new hashes for each key you
try to access (specifically 1-level deep for a '2-dimensional' hash):
people = Hash.new{ |me,key| me[ key ] = {} }
people[ :gavin ][ :age ] = 34
people[ :gavin ][ :sex ] = :male
people[ :fido ][ :species ] = :dog
p people
#=> {:gavin=>{:age=>34, :sex=>:male}, :fido=>{:species=>:dog}}
...but there was no further explanation. Could somebody explain the
syntax in the first line? Is this overwriting new()?
Thanks,
Robb
"Here's a Hash that automatically creates new hashes for each key you
try to access (specifically 1-level deep for a '2-dimensional' hash):
people = Hash.new{ |me,key| me[ key ] = {} }
people[ :gavin ][ :age ] = 34
people[ :gavin ][ :sex ] = :male
people[ :fido ][ :species ] = :dog
p people
#=> {:gavin=>{:age=>34, :sex=>:male}, :fido=>{:species=>:dog}}
...but there was no further explanation. Could somebody explain the
syntax in the first line? Is this overwriting new()?
Thanks,
Robb