Given a hash where each value is an array of values this gives a nice concise way of setting things up (even if it is a little obscure).
map = {}
(map[:key] ||= []) << :value
The above results in a hash containing a key value of :key with an array containing :value
2 comments:
Alternative:
map = Hash.new{|hash, key| hash[:key] = []}
There's a typo in my previous post. 'key' shouldn't be a symbol. It should be:
hash[key] = []
Post a Comment