[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: hash of hashes
From: Emmanuel Touzery <emmanuel.touzery () wanadoo ! fr>
Date: 2003-10-03 11:07:24
[Download RAW message or body]
Paul Argentoff wrote:
>On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:
>
>
>
>>irb(main):001:0> a = {}
>>{}
>>irb(main):002:0> a["blue"] = {}
>>{}
>>irb(main):003:0> a["blue"]["red"] = 3
>>3
>>irb(main):004:0> a["blue"]
>>{"red"=>3}
>>irb(main):005:0> a["blue"]["red"]
>>3
>>irb(main):006:0>
>>
>>
>
>I need something like this:
>
>a = {{}} # this doesn't work, but i've just written it here to emph. the
> # subject
>...
># somewhere in the iteration
>a[foo][bar] = baz
>...
>
>
i think you need ruby 1.8's block form for creating hashes. i asked that
question recently, and here is the snippet from Guy Decoux:
svg% ruby -e 'has = Hash.new {|h,k| h[k]={}}; has["blue"]["red"] = 1; p has'
{"blue"=>{"red"=>1}}
svg%
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic