Method: Weak::Map::WeakKeysWithDelete#fetch

Defined in:
lib/weak/map/weak_keys_with_delete.rb

#fetch(key, default = UNDEFINED) {|key| ... } ⇒ Object

Note:

Set does not test member equality with == or eql?. Instead, it always checks strict object equality, so that, e.g., different strings are not considered equal, even if they may contain the same string content.

Returns a value from the hash for the given key. If the key can't be found, there are several options: With no other arguments, it will raise a KeyError exception; if default is given, then that value will be returned; if the optional code block is specified, then it will be called and its result returned.

Parameters:

  • key (Object)

    the key for the requested value

  • default (Object) (defaults to: UNDEFINED)

    a value to return if there is no value at key in the hash

Yields:

  • (key)

    if no value was set at key, no default value was given, and a block was given, we call the block and return its value

Yield Parameters:

  • key (String)

    the given key

Returns:

  • (Object)

    the value for the given key if present in the map. If the key was not found, we return the default value or the value of the given block.

Raises:

  • (KeyError)

    if the key can not be found and no block or default value was provided



94
95
96
97
98
# File 'lib/weak/map/weak_keys_with_delete.rb', line 94

def fetch(key, default = UNDEFINED, &block)
  value = @map[key]
  value = _fetch_default(key, default, &block) if value.nil? && !@map.key?(key)
  value
end