public Method

Array.rassoc(p1)

array.rassoc(key)  an_array or nil

Searches through the array whose elements are also arrays. Compares key with the second element of each contained array using ==. Returns the first contained array that matches. See also Array#assoc.

a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
a.rassoc("two")    #=> [2, "two"]
a.rassoc("four")   #=> nil

Source Code

/*
*  call-seq:
*     array.rassoc(key) -> an_array or nil
*  
*  Searches through the array whose elements are also arrays. Compares
*  <em>key</em> with the second element of each contained array using
*  <code>==</code>. Returns the first contained array that matches. See
*  also <code>Array#assoc</code>.
*     
*     a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
*     a.rassoc("two")    #=> [2, "two"]
*     a.rassoc("four")   #=> nil
*/

VALUE
rb_ary_rassoc(ary, value)
   VALUE ary, value;
{
   long i;
   VALUE v;

   for (i = 0; i < RARRAY(ary)->len; ++i) {
       v = RARRAY(ary)->ptr[i];
       if (TYPE(v) == T_ARRAY &&
           RARRAY(v)->len > 1 &&
           rb_equal(RARRAY(v)->ptr[1], value))
           return v;
   }
   return Qnil;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.