Method: Tool::EqualityMap#fetch

Defined in:
lib/tool/equality_map.rb

#fetch(*key) { ... } ⇒ Object

Returns value stored in map or result of block.

Parameters:

  • key (Array<#hash>)

    for caching

Yields:

  • block that will be called to populate entry if missing (has to be idempotent)

Returns:

  • value stored in map or result of block



32
33
34
35
36
37
38
39
# File 'lib/tool/equality_map.rb', line 32

def fetch(*key)
  identity = @keys[key.hash]
  key      = identity == key ? identity : key

  # it is ok that this is not thread-safe, worst case it has double cost in
  # generating, object equality is not guaranteed anyways
  @map[key] ||= track(key, yield)
end