Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Array#assoc

(untyped object) -> ::Array[untyped]?
assoc(object) → found_array or nil

Returns the first element ele in self such that ele is an array and ele[0] == object:

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: Array#rassoc; see also Methods for Fetching.

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