Also good to remember to be careful if you're trying to set your default value to an array, string, or hash. The default value passed in is just reused and mutated
Note that I had to use <<= to assign the value to the key, if you just do <<, h would still be an empty hash.
If I want the default value to be an array, I usually use the third option and pass a block Hash.new { [] }, which will give me a new array for each key.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Also good to remember to be careful if you're trying to set your default value to an array, string, or hash. The default value passed in is just reused and mutated
Note that I had to use
<<=
to assign the value to the key, if you just do<<
,h
would still be an empty hash.If I want the default value to be an array, I usually use the third option and pass a block
Hash.new { [] }
, which will give me a new array for each key.