From: w_a_x_man Date: 2011-01-09T11:10:21+09:00 Subject: Re: Best way for Array#find+transform ? On Jan 8, 5:38 pm, "Jonas Pfenniger (zimbatm)" wrote: > There is a pattern that I'm using quite regularly, but I'm not > satisfied by my implementation. > > ------- > filename = "something" > paths = ["/usr", "/usr/local", "/opt"] > abs_file = nil > paths.each do |path| >   path = File.join(path, filename) >   File.exist?(path) >     abs_file = path >     break >   end > end > ------- > > I know I can come up with a new method on Array that would shorten this to: > > ------ > abs_file = paths.find_transform do |path| >   path = File.join(path, filename) >   File.exist?(path) && path > end > ------ > > But is there an existing method that I oversaw that does about the same ? > 1. Stop iterating if the element is found > 2. Return the transformed element > > Cheers, >   zimbatm filename = "something" paths = ["/usr", "/usr/local", "/opt"] abs_file = paths.each{|path| path = ( File.join( path, filename) ) break path if File.exist?(path) } If the file doesn't exist anywhere, abs_file will be equal to the paths array.