title: How do I give one method invoked in a block the ability to see variables set in the context of the calling function?
body: <p>I have a module that defines a method, <code>logged</code>, which will add a prefix to the result of a different method, <code>say</code>:</p>
  
  <pre><code>module M
    def logged(&amp;block)
      str = "logged: "
      block.call
    end
  
    def say(value)
      puts str + value
    end
  end
  </code></pre>
  
  <p>I'd like to be able to use <code>M</code> inside the body of a <code>class</code> block, like so:</p>
  
  <pre><code>class C
    extend M
  
    logged {
      say "one"
      say "two"
    }
  end
  </code></pre>
  
  <p>and get:</p>
  
  <pre><code># =&gt; "logged: one"
  # =&gt; "logged: two"
  </code></pre>
  
  <p>Essentially, I want <code>say</code> to be able to access values in <code>logged</code> that are only in <code>logged</code>'s calling context. So, I'd prefer to avoid instance variables, global variables, et cetera.</p>
  
  <p>What's the right way to pull this off?</p>