instance method
Array#assoc
(untyped) -> ::Array[untyped]?
assoc(object) → found_array or nil
array.c
View on GitHub ↗
VALUE
rb_ary_assoc(VALUE ary, VALUE key)
{
long i;
VALUE v;
for (i = 0; i < RARRAY_LEN(ary); ++i) {
v = rb_check_array_type(RARRAY_AREF(ary, i));
if (!NIL_P(v) && RARRAY_LEN(v) > 0 &&
rb_equal(RARRAY_AREF(v, 0), key))
return v;
}
return Qnil;
}
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.