Skip to content

Instantly share code, notes, and snippets.

@svenfuchs
Forked from lifo/return.rb
Created June 18, 2009 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svenfuchs/132015 to your computer and use it in GitHub Desktop.
Save svenfuchs/132015 to your computer and use it in GitHub Desktop.
module Enumerable
def return(*)
if block_given?
each do |i|
value = yield(i)
return value if value
end
nil
else
super
end
end
end
h = {0 => 'a', 2 => 'b'}
[1,2,3].return {|x| h[x]} # => b
[4,5,6].return {|x| h[x]} # => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment