Method: Immutable::Vector#assoc

Defined in:
lib/immutable/vector.rb

#assoc(obj) ⇒ Object

Assumes all elements are nested, indexable collections, and searches through them, comparing obj with the first element of each nested collection. Return the first nested collection which matches, or nil if none is found. Behaviour is undefined when elements do not meet assumptions (i.e. are not indexable collections).

Examples:

v = Immutable::Vector[["A", 10], ["B", 20], ["C", 30]]
v.assoc("B")  # => ["B", 20]

Parameters:

  • obj (Object)

    The object to search for

Returns:

  • (Object)


1263
1264
1265
1266
1267
1268
1269
# File 'lib/immutable/vector.rb', line 1263

def assoc(obj)
  each do |array|
    next if !array.respond_to?(:[])
    return array if obj == array[0]
  end
  nil
end