Created
October 10, 2013 12:57
-
-
Save insoul/6917894 to your computer and use it in GitHub Desktop.
yield vs block.call vs instance_eval(&block)
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
class A | |
def yield_block | |
yield | |
end | |
def call_block(&block) | |
block.call | |
end | |
def eval_block(&block) | |
instance_eval(&block) | |
end | |
end | |
class B | |
def initialize | |
@a = A.new | |
end | |
def yield_test | |
@a.yield_block { puts self.class } | |
end | |
def call_test | |
@a.call_block { puts self.class } | |
end | |
def eval_test | |
@a.eval_block { puts self.class } | |
end | |
end | |
b = B.new | |
b.yield_test #=> B | |
b.call_test #=> B | |
b.eval_test #=> A | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment