title: How to pass an instance variable to an RSpec shared example
body: <p>I'm using RSpec (2.10.1) to test validations on a model and have extracted some code to share with other model validations. The validations were first written on the Companies table, so the code looks like this:</p>
  
  <pre><code># support/shared_examples.rb
  shared_examples "a text field" do |field, fill, length|
    it "it should be long enough" do
      @company.send("#{field}=", fill * length)
      @company.should be_valid
    end
  
    etc...
  end
  </code></pre>
  
  <p>and the usage is:</p>
  
  <pre><code># company_spec.rb
  describe Company do
    before { @company = Company.new( init stuff here ) }
  
    describe "when address2" do
      it_behaves_like "a text field", "address2", "a", Company.address2.limit
    end
  
    etc...
  end
  </code></pre>
  
  <p>I'd like to pass the <code>@company</code> as a parameter to the shared example so I can reuse the code for different models, something like this:</p>
  
  <pre><code># support/shared_examples.rb
  shared_examples "a text field" do |model, field, fill, length|
    it "it should be long enough" do
      model.send("#{field}=", fill * length)
      model.should be_valid
    end
  
    etc...
  end
  </code></pre>
  
  <p>and the usage is:</p>
  
  <pre><code># company_spec.rb
  describe Company do
    before { @company = Company.new( init stuff here ) }
  
    describe "when address2" do
      it_behaves_like "a text field", @company, "address2", "a", Company.address2.limit
    end
  
    etc...
  end
  </code></pre>
  
  <p>However, when I do this I get <code>undefined method 'address2' for nil:NilClass</code>. It appears <code>@company</code> is not being passed (not in scope?) How do I get something like this to work?</p>