yield is applied to the block passed in the context of the transfer method. In your case, I suppose it depends on which irb method relies ( lib/ruby/2.0.0/irb/workspace.rb:86 evaluate if caller is something to be done).
If you wrap it in a function, it will work because you are changing the method context:
def do_stuff do_once = Proc.new { yield } do_once.call end do_stuff { puts 1 }
Note the absence of a block for do_once.call in the above example: yield is applied to the block passed to do_stuff , and not to the block passed to do_once .
Alternatively, declare the block explicitly to avoid using the lesson as a whole:
do_once = Proc.new { |&block| block.call } do_once.call { puts 1 }
source share