Method: Array#assoc

Defined in:
array.c

#assoc(object) ⇒ nil

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

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.

Returns:

  • (nil)


5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
# File 'array.c', line 5151

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;
}