Initialize Hash with Default Value

Say you want a hash to return some programmatic value based on the key called without assigning the return value explicitly.

Passing a block to specifies what happens when you ask for a non-existent key.

# new {|hash, key| block } → new_hash
# it is the blocks responsibility to save the value into the hash, if necessary

default_hash ={|hash, key| key.to_i.even? ? "even!" : "odd!"}
default_hash #=> {}
default_hash[1] #=> "odd!"
default_hash #=> {} # did not save the call

default_hash ={|hash, key| value = (key.to_i.even? ? "even!" : "odd!"); hash[key] = value}
default_hash #=> {}
default_hash[1] #=> "odd!"
default_hash #=> {1 => "odd!"} # saved

More at: fun with ruby hashes
algorithmic fun with ruby hashes
solving antipatterns


Written on October 16, 2015