Last active
December 16, 2015 09:19
-
-
Save tobiashm/5412134 to your computer and use it in GitHub Desktop.
Enumerable#try_find - inspired by PrototypeJS Try.these
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enumerable | |
def try_find(rescue_type = StandardError) | |
each do |element| | |
begin | |
candidate = element.respond_to?(:call) ? element.call : element | |
return candidate if candidate | |
rescue rescue_type | |
next | |
end | |
end | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: