From: Robert Klemme Date: 2011-01-12T01:20:43+09:00 Subject: Re: Best way for Array#find+transform ? On Tue, Jan 11, 2011 at 5:02 PM, botp wrote: > On Tue, Jan 11, 2011 at 2:49 PM, David J. Hamilton wrote: >> ...wanted to avoid having to write the transformation twice.  See his previous >> example earlier in this thread where he uses find to get the value, and then has >> to transform (again) the result. > > ah, ok (slaps my dummy head after whole thread :-) > > in that case, i'd still prefer something like > >  paths.map{|path| File.join(path,filename)}.find{|path| File.exists?(path)} >  #=> "/opt/jruby" Nothing built in but one could do MapEnum = Struct.new :enum, :args, :map do include Enumerable def each enum.send(*args) do |*x| yield map[*x] end end end class Object def map_enum_for(*a, &conv) MapEnum.new(self, a, conv) end end filename = "ls" p ["/usr", "/usr/local", "/opt", "/usr/bin"].map_enum_for(:each) {|x| File.join(x,filename)}.find {|x| File.exist? x} Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/