title: ruby - evalulate code returned from another method
body: <p>ruby 2 and rails 4</p>
  
  <p>Consider this chunk of code:</p>
  
  <pre><code>Post.where{
    (created_by == user) &amp; 
    (
      (group_id.in [1,2,3,4]) | (event_id.in [5,6,7,8])
    )
  }
  </code></pre>
  
  <p>Which works fine on its own.</p>
  
  <p>What I would like to do is take <code>(group_id.in [1,2,3,4]) | (event_id.in [5,6,7,8])</code> and have this come from a local variable, or method, or block or something.</p>
  
  <p>I have many queries and would like this bit of code contained in a single spot.</p>
  
  <p>I have tried several things, like using a block with yield, Proc and lambas. I seem to keep getting stuck on: <code>NameError (undefined local variable or method 'group_id')</code></p>
  
  <p>Note: I am using squeel, which contains its own DSL which uses <code>instance_eval</code> for its blocks. Which should be fine because I can access via <code>my{&lt;method_name_here&gt;}</code>.</p>
  
  <p>Here is what I have tried:</p>
  
  <pre><code>viewable_areas = lambda { (group_id.in [1,2,3,4]) | (event_id.in [5,6,7,8]) }
  
  Post.where{
    (created_by == user) &amp; 
    (
      my{viewable_areas.call}
    )
  }
  </code></pre>
  
  <p>The best I can get is the: <code>NameError (undefined local variable or method 'group_id')</code></p>
  
  <p>I know there must be a way of handling this, its just not coming to mind right now.</p>
  
  <p>Also the real code I am working with is much more complex than what I am describing here but I have simplified it for this example.</p>