Robert Klemme
1/25/2007 10:02:00 AM
On 25.01.2007 03:29, Daniel Martin wrote:
> Vincent Fourmond <vincent.fourmond@9online.fr> writes:
>
>> Actually, I've got shorter ;-):
>>
>> h = {}
>> for k,v in hash
>> [*k].each {|l| h[l] = v}
>> end
>> hash = h
>
> Well, if we're golfing:
>
> h={};hash.map{|k,v|[*k].map{|t|h[t]=v}};h
Not really shorter but I though there should be at least one solution
with #inject:
>> hash.inject({}){|h,(k,v)| k.to_a.each {|x| h[x]=v};h}
=> {"new"=>"manage_all", "edit"=>"manage_one", "destroy"=>"manage_all",
"create"=>"manage_all", "index"=>"list", "update"=>"manage_one"}
>> hash.inject({}){|h,(k,v)| k.each {|x| h[x]=v} rescue h[k]=v;h}
=> {"new"=>"manage_all", "edit"=>"manage_one", "destroy"=>"manage_all",
"create"=>"manage_all", "index"=>"list", "update"=>"manage_one"}
:-)
robert