Created
January 12, 2014 19:45
-
-
Save anonymous/8389455 to your computer and use it in GitHub Desktop.
Doubt with blocks & procs
This file contains 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
def foo | |
p = block_given? ? Proc.new : nil | |
end | |
obj = foo{ |x| x + 1} | |
obj.call(3) | |
#=> 4 | |
#How does this work? `Proc.new` in definition must have had the block as argument to give resulting Proc object. | |
#How does passed block to `foo` get passed in `Proc.new` ? | |
p = Proc.new | |
#ArgumentError: tried to create Proc object without a block |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment