Skip to content

Instantly share code, notes, and snippets.

@raggi
Forked from lifo/return.rb
Created Jun 18, 2009
Embed
What would you like to do?
module Enumerable
def return
each do |i|
value = yield(i)
return value if value
end
nil
end
end
h = {0 => 'a', 2 => 'b'}
[1,2,3].return {|x| h[x]} # => b
[4,5,6].return {|x| h[x]} # => nil
# or
[1,2,3].each { |x| v = h[x]; break v if v } # => "b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment