Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Last active December 17, 2015 01:58
Show Gist options
  • Save bil-bas/5531970 to your computer and use it in GitHub Desktop.
Save bil-bas/5531970 to your computer and use it in GitHub Desktop.
module Enumerable
def map_find(default=nil)
return enum_for(:map_find, default) unless block_given?
each do |e|
mapped = yield e
return mapped if mapped
end
default
end
end
# Examples:
url = MAX_RETRIES.times.map_find { get_url }
n = 100.times.map_find {|x| x + 10 if x % 10 == 8 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment