From: Robert Klemme Date: 2004-07-07T00:17:43+09:00 Subject: Re: Array::index and rindex operator "David A. Black" schrieb im Newsbeitrag news:Pine.LNX.4.44.0407060654550.23516-100000@wobblini... > 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. Might be best to just enhance #index with the block variant. > 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. True. I was blind for that because of my strong #inject affection... :-) Regards robert