From: Robert Klemme Date: 2009-11-13T02:00:06+09:00 Subject: Re: Enumerable#find_yield, useful shortcut? better name? On 12.11.2009 17:41, Aaron Patterson wrote: > On Fri, Nov 13, 2009 at 01:07:41AM +0900, Intransition wrote: >> This method has long been suggested to me as #map_detect. At first I >> wasn't sure of it's usefulness. But now I think it's okay. (Your >> thoughts?). In any case, the name has to go. Currently I'm thinking >> #find_yield. Any better suggestions? > > I would call the method "unnecessary". Especially since we already have > a built in syntax which is more concise: > > [1,2,3].find { |_| false } # => nil > [1,2,3].find { |_| false } || 1 # => 1 I disagree. The difference between TMISFAN (the method in search for a name) and your solution is that TMISFAN returns the result of the block evaluation and not the element contained in the collection. I don't have frequent use for this but it occurred just a few days ago. This is what I did then - roughly: x = nil enum.any? {|e| e > 10 and x = e * 2} This is dummy code of course, I don't have the concrete example handy. The main point is that #any? will short circuit (like #find) and the special value is reserved via the variable. How about #find_map? This expresses what happens, first a search and then a mapping of the value - sort of. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/