From: botp Date: 2011-01-11T11:48:14+09:00 Subject: Re: Best way for Array#find+transform ? On Mon, Jan 10, 2011 at 11:14 PM, Jonas Pfenniger (zimbatm) wrote: > 2011/1/10 David J. Hamilton : >> My version is slightly verbose.  As w_a_x_man@yahoo.com implicitly pointed out >> in their version, the false in the block is actually unnecessary, since the >> conditional will evaluate to nil, thus allowing the iteration to continue. > > Right, now we have the perfect version :) > >> Break is indeed nice.  And of course you can sometimes do the same thing with >> return, although I think it's usually much cleaner to break out of blocks rather >> than returning out of them (and the method in which they're evaluated). > > Yeah I agree, return is less clean because it doesn't express the > intent of exiting that particular algorithm. no on all. on your case, one does not need break nor return. #find will shortcircuit and returns the first find, or nil otherwise. iow, the ff should do, paths.find{|path| File.exists?(File.join(path,filename))} in fact, find is quite flexible, it allows you to exec/return something (override) the returned nil by passing a lambda/proc. eg, nothing= ->{puts "nothing here, passing a null string instead"; ""} #=> # paths.find(nothing){|path| File.exists?(File.join(path,filename))} nothing here, passing a null string instead #=> "" best regards -botp