title: Access variable from the block's scope inside a method call in Ruby
body: <p>I am trying to create a minitest assertion that is similiar to Rails <a href="https://github.com/rails/rails/blob/495722a95687e25114ae75608dd3107ac5d6611b/activesupport/lib/active_support/testing/assertions.rb#L63" rel="nofollow"><code>assert_difference</code></a> however it checks for any differences not just a numeric one. </p>
  
  <p>Here is my current implementation:</p>
  
  <pre><code>Minitest::Assertions.module_eval do
  
    def assert_changed(expression, &amp;block)
      unless expression.respond_to?(:call)
        expression = lambda{ eval(expression, block.binding) }
      end
      old = expression.call
      block.call
      refute_equal old, expression.call
    end
  
  end
  </code></pre>
  
  <p>Now in my test suite I call it by doing the following:</p>
  
  <pre><code># this is working 
  username = 'Jim'
  assert_changed lambda{ username } do
    username = 'Bob'
  end
  
  # this is not working
  username = 'Jim'
  assert_changed 'username' do
    username = 'Bob'
  end
  </code></pre>
  
  <p>The first call to <code>assert_changed</code> using a lambda (or a proc) works perfectly fine. The second call using a string with the variable name is not working.</p>
  
  <p>When it hits this line: <code>expression = lambda{ eval(expression, block.binding) }</code>
  I keep getting the error <code>TypeError: no implicit conversion of Proc into String</code>. Can someone please explain to me how to get this to work?</p>
  
  <p>Note: I got the <code>eval(expression, block.binding)</code> idea from the Rails <a href="https://github.com/rails/rails/blob/495722a95687e25114ae75608dd3107ac5d6611b/activesupport/lib/active_support/testing/assertions.rb#L63" rel="nofollow"><code>assert_difference</code></a> method.</p>