From: Daniel Schierbeck Date: 2006-08-15T02:05:07+09:00 Subject: Re: Multiple assignment in a hash Kevin Olbrich wrote: > Anyone know of any ruby shortcuts for multiply assigning elements in a hash? > > What I need to do is set up a hash like this... > > { "one" => "A", "two"=>"A", "three" => "A" } > > What I would like to do is something like this.... > > { "one","two","three" => "A" } (this doesn't work) Perhaps this? class Hash def []= (*keys) value = keys.pop keys.each{|key| store(key, value) } end end hsh = {} hsh[:a, :b, :c] = 42 hsh #=> {:a => 42, :b => 42, :c => 42} Do remember that all keys will have the *same* value, so: hsh[:a, :b] = "foo" hsh[:a].upcase! hsh[:b] #=> "FOO" Cheers, Daniel