Can someone explain this syntax in new?

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
 
S

Sebastian Hungerecker

Robb said:
people =3D Hash.new{ |me,key| me[ key ] =3D {} }
[...]
=C2=A0Is this overwriting new()?

No, it calls new and passes the above block to it.
The block will then be stored and executed every time a key is accessed tha=
t=20
does not exist (with the hash object and the requested key as arguments).

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
S

Stefano Crocco

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

If you look at the ri documentation for Hash#new, you'll see that it can be
used in three form: the first is Hash.new() which returns an hash with a
default value of nil; the second is Hash.new(obj) which returns an hash with a
default value of obj. This means that whenever you try to access an element
using a key which is not in the hash, the method will return obj. The third
form of new takes a block. Whenever you try to access a nonexistent key, the
block is called with two arguments: the hash itself and the key. In the block
you can do whatever you want, including computing a value from the key and
storing it in the hash.

In the code you posted, the block simply stores a new, empty hash under the
key key of the hash.

I hope this helps

Stefano
 
R

Robb Shecter

Sebastian said:
No, it calls new and passes the above block to it.
The block will then be stored and executed every time a key is accessed
that
does not exist (with the hash object and the requested key as
arguments).

Ah hah. I see; this is a particular feature of the Hash class -- one of
the three ways to invoke new(). Thanks!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top