title: Changing the scope of a captured block in Ruby
body: <p>I want to capture blocks (with an associated name), but without any change in the scope in which they are written. The code below has two ways of capturing a block (<code>capt_a</code> and <code>capt_b</code>). <code>capt_a</code> works fine and I'd like <code>capt_b</code> to work the same way. Is it possible to modify <code>capt_b</code> so that the effect is the same as <code>capt_a</code>?</p>
  
  <pre><code>class Capturer
    attr_reader :method, :block
  
    def capt_a
      yield self
      self
    end
  
    def capt_b(&amp;block)
      instance_eval(&amp;block)
      self
    end
  
    def method_missing(method, &amp;block)
      @method = method
      @block = block
    end
  end
  
  # Example:
  a = Capturer.new.capt_a{|capt| capt.foo{self} }.block
  b = Capturer.new.capt_b{ foo{self} }.block
  
  a.call # =&gt; main
  b.call # =&gt; #&lt;Capturer:0x000001008fb5c8 @method=:foo, @block=#&lt;Proc:0x000001008fb500@temp.rb:23&gt;&gt;
         # I would like 'main'
  </code></pre>