ruby adding to hash with array value

The problem is that you aren't actually assigning anything to the hash keys, you're just using the << operator to modify the existing contents of a default value. Since you don't assign anything to the hash key, it is not added. In fact, you'll notice the default value is the one modified:

h = Hash.new []
p h[0]           # []
h[0] << "Hello"
p h              # {}
p h[0]           # ["Hello"]
p h[1]           # ["Hello"]

This is because the same Array object is maintained as the default value. You can fix it by using the + operator, though it may be less efficient:

map = Hash.new []
strings = ["abc", "def", "four", "five"]

strings.each do |word|
    map[word.length] += [word]
end

And now it works as expected.


All being said, check Enumerable#group_by:

["abc", "def", "four", "five"].group_by(&:length)
#=> {3=>["abc", "def"], 4=>["four", "five"]}

Tags:

Ruby

Hash