method
find_index
find_index(...)
public
Returns the index of the first object in self such that is
to obj. If a block is given instead of an
argument, returns first object for which block is true. Returns nil if no match is found.
a = [ "a", "b", "c" ] a.index("b") #=> 1 a.index("z") #=> nil a.index{|x|x=="b"} #=> 1
This is an alias of #find_index.
Register or
log in
to add new notes.
Mange -
July 8, 2009 - (<= v1_8_7_72)
5 thanks
Using block version in Ruby < 1.8.7
The block usage was added in 1.8.7, so to get the same functionality in an earlier version of Ruby, you need to utilize the find method.
Here is a quick example:
match = list.find { |l| l.owner == myself } match_index = list.index(match)
If you do some gymnastics, you can have it on one line without extra variables:
match_index = list.index(list.find { |l| l.owner == myself })


