Created
August 31, 2015 16:04
-
-
Save Hal9000/28cfca6641c748fc7b98 to your computer and use it in GitHub Desktop.
Returning from a Proc versus a lambda...
This file contains hidden or 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
| # 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