Jano Svitok
11/3/2007 8:37:00 PM
On 11/3/07, Joshua Muheim <forum@josh.ch> wrote:
> Hi all
>
> PHP lets me easily create multidimensional hashes using the following
> syntax:
>
> x = array();
> x["bla"]["some_key"] = true;
>
> Is Ruby not capable of doing this?
>
> x = []
> x[:bla][:some_key] = true
>
> gives me a nil error!
As David said, if you want to create hashes, you have to create Hashes ;-)
Then, you can tell Hash.new what is the default value, so you can
create hash, that will contain by default empty hashes:
x = Hash.new { Hash.new }
This will add an "automatic" two level hash. I don't know quickly how
to make this indefinitly deep, you can at least repeat the pattern.
Please note that it is not enough to write Hash.new { {} } as the
inner will create one particular Hash instance, that all keys will
reference. You need Hash.new to create a new hash for each key.
Jano