Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Hash#default

(?::Hash::_Key key) -> V?
default → object
default(key) → object

Returns the default value for the given key. The returned value will be determined either by the default proc or by the default value. See Hash Default.

With no argument, returns the current default value:

Example
h = {}
h.default # => nil

If key is given, returns the default value for key, regardless of whether that key exists:

Example
h = Hash.new { |hash, key| hash[key] = "No key #{key}"}
h[:foo] = "Hello"
h.default(:foo) # => "No key foo"