Skip to content

Instantly share code, notes, and snippets.

@Hal9000
Created August 31, 2015 16:04
Show Gist options
  • Select an option

  • Save Hal9000/28cfca6641c748fc7b98 to your computer and use it in GitHub Desktop.

Select an option

Save Hal9000/28cfca6641c748fc7b98 to your computer and use it in GitHub Desktop.
Returning from a Proc versus a lambda...
# Posting to clarify...
# There is some misleading code in _The Ruby Way_ 3rd edition, section 11.2.5
# return inside a lambda (return from the enclosing context)
def call_lambda
code = lambda { return }
code.call
puts "Here we are!"
end
call_lambda # Prints: Here we are!
# return inside Proc (return from the immediate context)
def call_proc
puts "About to call the Proc..."
code = Proc.new { return }
code.call
puts "We never get here!"
end
call_proc # Prints: About to call the Proc...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment