assoc(p1)
public
Searches through an array whose elements are also arrays comparing
obj with the first element of each contained array using obj.==. Returns the
first contained array that matches (that
is, the first associated array), or
nil if no match is found. See also Array#rassoc.
s1 = [ "colors", "red", "blue", "green" ]
s2 = [ "letters", "a", "b", "c" ]
s3 = "foo"
a = [ s1, s2, s3 ]
a.assoc("letters")
a.assoc("foo")
Show source
/*
* call-seq:
* array.assoc(obj) -> an_array or nil
*
* Searches through an array whose elements are also arrays
* comparing _obj_ with the first element of each contained array
* using obj.==.
* Returns the first contained array that matches (that
* is, the first associated array),
* or +nil+ if no match is found.
* See also <code>Array#rassoc</code>.
*
* s1 = [ "colors", "red", "blue", "green" ]
* s2 = [ "letters", "a", "b", "c" ]
* s3 = "foo"
* a = [ s1, s2, s3 ]
* a.assoc("letters")
* a.assoc("foo")
*/
VALUE
rb_ary_assoc(ary, key)
VALUE ary, key;
{
long i;
VALUE v;
for (i = 0; i < RARRAY(ary)->len; ++i) {
v = rb_check_array_type(RARRAY(ary)->ptr[i]);
if (!NIL_P(v) && RARRAY(v)->len > 0 &&
rb_equal(RARRAY(v)->ptr[0], key))
return v;
}
return Qnil;
}