Initializing a Ruby Hash

Researching in the Hash ruby class, I needed to initialize values to 0. It is really easy, passing default value to construnctor:

store = Hash.new(0)
store[:candles] +=3

Then we can accumulate candles from the creation of the key without the “undefined ‘+’ for nil:NilClass” error.

Leave a Reply

Your email address will not be published. Required fields are marked *