Created
July 18, 2017 09:44
-
-
Save igaiga/58abe028148ad1a7235c3c46265c5518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h = {} | |
["a", "a", "b"].each do |x| | |
h[x] = [] unless h[x] | |
h[x] << x | |
end | |
p h | |
#=> {"a"=>["a", "a"], "b"=>["b"]} |
default_proc
で代入すると意図していないところでも勝手に追加されるので、追加するところで代入するとか
h["a"] <<= "a"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
私は
h = Hash.new([].dup)
で書けるのかなと思って書いてみたのですが、思った通り動かなくて質問した、という流れでした。