title: instance variable in block when using define_method
body: <p>I'm trying to do a DSL, in which the user can pass a block and expect an instance variable <code>@arg</code> to be defined. This is a full example with a unit test failing:</p>
  
  <pre class="lang-rb prettyprint-override"><code># Implementation
  class Filter
    def initialize
      @arg = 'foo'
    end
  
    def self.filters &amp;block
      define_method :filter do |els|
        els.select &amp;block
      end
    end
  end
  
  # Usage
  class Foo &lt; Filter
    filters {|el| el == @arg}
  end
  
  # Expected behavior
  describe 'filters created with the DSL' do
    subject { Foo.new }
    it 'can use @arg in the filters block' do
      els = %w[notthearg  either  foo  other]
      expect(subject.filter els).to be_eql(['foo'])
    end
  end
  </code></pre>
  
  <p>Using <code>pry</code> or putting <code>puts</code> statements inside the block, I can see that <code>@arg</code> is nil. But <code>Foo.new.instance_variable_get :@arg</code> correctly outputs <code>foo</code>, so it must be related to some scoping rules.</p>
  
  <p>What do I need to change in the implementation to make the test pass and the DSL to work?</p>