Array

From RubySpec

Jump to: navigation, search

Array#index

From ri:

------------------------------------------------------------ Array#index
     array.index(obj)   ->  int or nil
------------------------------------------------------------------------
     Returns the index of the first object in _self_ such that is +==+
     to _obj_. Returns +nil+ if no match is found.

        a = [ "a", "b", "c" ]
        a.index("b")   #=> 1
        a.index("z")   #=> nil

Note: It's important to note here that during the index search, == should be invoked against the element in the array, not against the argument to index. This is to allow list elements to override == for such searches.

Array#rindex

----------------------------------------------------------- Array#rindex
     array.rindex(obj)    ->  int or nil
------------------------------------------------------------------------
     Returns the index of the last object in _array_ +==+ to _obj_.
     Returns +nil+ if no match is found.

        a = [ "a", "b", "b", "b", "c" ]
        a.rindex("b")   #=> 3
        a.rindex("z")   #=> nil

See the note on #Array#index as well.

Personal tools