Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#assoc

array.assoc(obj) → found_array or nil

Returns the first element in self that is an Array whose first element == obj:

Example
a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]]
a.assoc(4) # => [4, 5, 6]

Returns nil if no such element is found.

Related: rassoc.

See also #append #at #any? #bsearch also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0