From: "David A. Black" Date: 2004-07-06T23:04:19+09:00 Subject: Re: Array::index and rindex operator Hi -- On Tue, 6 Jul 2004, Robert Klemme wrote: > True. The approach is betters suited to a method implementation: > > module Enumerable > def index_2(obj=nil, &b) > b = lambda {|x| obj == x} unless b > inject(0) {|i,el| return i if b.call(el); i+1} > nil > end > end I agree. My version, for what it's worth, was: module Enumerable def b_index each_with_index {|e,i| return i if yield(e) } nil end end I decided to make it orthogonal to #index, rather than a superset of it, on the theory that it would be good to avoid making it legal to give both an argument and a block. I also like #each_with_index here, rather than #inject, only because it does more of the work and eliminates the need for manually incrementing an index. I haven't benchmarked them though. David -- David A. Black dblack@wobblini.net