On Nov 26, 2006, at 9:53 AM, dblack / wobblini.net wrote:

> On Mon, 27 Nov 2006, James Edward Gray II wrote:
>
>> On Nov 26, 2006, at 8:54 AM, Olivier wrote:
>>
>>> Le dimanche 26 novembre 2006 15:00, Josselin a ñÄrit :
>>>>
>>>> array.index(array.detect {|x| x > 0})    => 15
>>> c = 0
>>> array.each{|v| break if not v.zero?; c += 1}
>>> puts c # => 15
>>
>>>> result = array.enum_with_index.find { |n, i| n.nonzero? }.last  
>>>> rescue nil
>
> That seems kind of like a reinvention of Array#index, though.  It also
> has the usual problem with rescue, i.e., that you might rescue the
> wrong thing (if nonzero? is mistyped or whatever).  Do you see an
> advantage to doing it this way, rather than the index/detect way?
> (I'm being lazy and not benchmarking them....)

Well, it only walks the Array once.  The other way walks it once for  
detect() and again for index().  I agree that it's not sexy code though.

I believe there has been talk in the past of having index() take a  
block for matching.  That would solve this problem ideally.  I can  
submit an RCR if people think it's worth it, but I'm pretty sure Matz  
said it was OK last time it came up...  (Correct me if I am wrong!)

James Edward Gray II