J
Jonas Pfenniger (zimbatm)
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
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