On 5/22/06, Paul D. Kraus <paul.kraus / gmail.com> wrote:
> >
> >
> > h = Hash.new { |hash, key| hash[key] = Array.new }
>
>
> Newbie here but what exactly does this like do.
> Can you someone break it down for me thanks.

This creates a new hash with a "default block" which will be called
whenever someone tries to get a value for a non-existent key. When the
block is called a new Array is created for the given key value and
stored in the hash. Therefore you can do things like the following and
they will just magically work:

h[:people] << "Paul"
h[:people] << "Douglas"
h[:people] << "Simon"
h[:animals] << "Dog"
h[:animals] << "Cat"
h[:animals] << "Armadillo"

Ryan