Method: Immutable::Hash#assoc

Defined in:
lib/immutable/hash.rb

#assoc(obj) ⇒ Array

Searches through the Hash, comparing obj with each key (using ‘#==`). When a matching key is found, return the `[key, value]` pair as an array. Return nil if no match is found.

Examples:

Immutable::Hash["A" => 1, "B" => 2, "C" => 3].assoc("B")  # => ["B", 2]

Parameters:

  • obj (Object)

    The key to search for (using #==)

Returns:

  • (Array)


701
702
703
704
# File 'lib/immutable/hash.rb', line 701

def assoc(obj)
  each { |entry| return entry if obj == entry[0] }
  nil
end