Skip to content

Instantly share code, notes, and snippets.

@insoul
Created October 10, 2013 12:57
Show Gist options
  • Save insoul/6917894 to your computer and use it in GitHub Desktop.
Save insoul/6917894 to your computer and use it in GitHub Desktop.
yield vs block.call vs instance_eval(&block)
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