Mega Code Archive

 
Categories / Ruby / Hash
 

If the key is not defined, return the successor of the key

plus1 = Hash.new {|hash, key| key.succ } plus1[1]      # 2 plus1["one"]  # "onf": see String.succ plus1.default_proc  # Returns the Proc that computes defaults plus1.default(10)   # => 11: default returned for key 10