[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

tricky hash initialization

Ray Pereda

8/14/2008 9:16:00 PM

h = Hash.new {|h,k| h[k] = {} }

This is one level deep defaults to a fresh new hash.

h = Hash.new {|h,k| h[k] = Hash.new{ |h2,k2| h2[k2] = {} } }

This is two levels deep initialization to a fresh new hash.

How do I this for arbitrarily deep hashes?

h["a"]["b"]["c"] = 3 #=> {"c"=>3}

I'm using this to build very clean implementations of state machine
building algorithms.

Your help is very much appreciated.

-Ray
--
Posted via http://www.ruby-....

5 Answers

Sebastian Hungerecker

8/14/2008 9:20:00 PM

0

Ray Pereda wrote:
> How do I this for arbitrarily deep hashes?

Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}

HTH,
Sebastian
--
NP: Disbelief - Lost In Time
Jabber: sepp2k@jabber.org
ICQ: 205544826

Ray Pereda

8/14/2008 11:33:00 PM

0

Sebastian Hungerecker wrote:
> Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}

Thanks for the attempt. Seems on the right track but not quite.

>> h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
=> {}
>> h["a"]["b"]["c"]
=> {}
>> h
=> {"c"=>{}}

After h["a"]["b"]["c"]
what needs to happen is h is equal to
{"a"=>{"b"=>{"c"=>{}}}}

Appreciating the Ruby Community's Help,
Ray

--
Posted via http://www.ruby-....

Mikael Høilund

8/14/2008 11:51:00 PM

0

On Aug 15, 2008, at 1:32, Ray Pereda wrote:

>>> h =3D Hash.new {|h,k| h[k] =3D Hash.new(&h.default_proc)}

Don't do that. The h inside the block is conflicting with the h =20
outside. Make sure the variables don't collide, and it works:

>> the_hash =3D Hash.new {|h,k| h[k] =3D Hash.new(&h.default_proc)}
=3D> {}
>> the_hash[:a][:b][:c]
=3D> {}
>> the_hash
=3D> {:a=3D>{:b=3D>{:c=3D>{}}}}

--=20
Mikael H=F8ilund
http://ho...


David A. Black

8/14/2008 11:53:00 PM

0

Hi --

On Fri, 15 Aug 2008, Ray Pereda wrote:

> Sebastian Hungerecker wrote:
>> Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
>
> Thanks for the attempt. Seems on the right track but not quite.
>
>>> h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
> => {}
>>> h["a"]["b"]["c"]
> => {}
>>> h
> => {"c"=>{}}
>
> After h["a"]["b"]["c"]
> what needs to happen is h is equal to
> {"a"=>{"b"=>{"c"=>{}}}}

I think it's a variable clobbering issue. Try this:

hash = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc) }


David

--
Rails training from David A. Black and Ruby Power and Light:
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.r... for details and updates!

David A. Black

8/14/2008 11:59:00 PM

0

I should add: the same-variable-name version works in Ruby 1.9.


David

On Fri, 15 Aug 2008, David A. Black wrote:

> Hi --
>
> On Fri, 15 Aug 2008, Ray Pereda wrote:
>
>> Sebastian Hungerecker wrote:
>>> Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
>>
>> Thanks for the attempt. Seems on the right track but not quite.
>>
>>>> h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
>> => {}
>>>> h["a"]["b"]["c"]
>> => {}
>>>> h
>> => {"c"=>{}}
>>
>> After h["a"]["b"]["c"]
>> what needs to happen is h is equal to
>> {"a"=>{"b"=>{"c"=>{}}}}
>
> I think it's a variable clobbering issue. Try this:
>
> hash = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc) }
>
>
> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
> * Advancing With Rails August 18-21 Edison, NJ
> * Co-taught by D.A. Black and Erik Kastner
> See http://www.r... for details and updates!
>

--
Rails training from David A. Black and Ruby Power and Light:
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.r... for details and updates!