Ruby/Hash/default proc

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

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