title: Referencing a local variable from a Proc defined in another scope
body: <p>I want to create an instance method which varies its behaviour with return value of another method depending on a implementation of its overwrites in a polymorphic manner.</p>
  
  <p>For example, the following class is assumed to be extended and <code>pricing_rule</code> is supposed to change depending on a product.</p>
  
  <pre><code>class Purchase
    def discount_price
      prices = [100, 200, 300]
      pricing_rule.call
    end
    protected
      def pricing_rule
        Proc.new do
          rate =  prices.size &gt; 2 ? 0.8 : 1
          total = prices.inject(0){|sum, v| sum += v}
          total * rate
        end
      end
  end
  Purchase.new.discount_price 
  #=&gt; undefined local variable or method `prices' for #&lt;Purchase:0xb6fea8c4&gt;
  </code></pre>
  
  <p>But, I'm getting an undefined local variable error when I run this. Though I understand that the instance of Proc refers to an instance of Purchase, I sometimes encountered similar situations I need to place <code>prices</code> variable into discount_price method. Is there any smarter way to refer to local variable in a caller of a Proc?</p>