title: how do use define_method on an instance of an object on objects of unknown type?
body: <p>So, I'm kind of wanting to do something similar to rspec / mocha's <code>mock</code>, but only for two objects, and not all of them.  This is what I have so far:</p>
  
  <pre><code>def mock(obj, method_to_mock, value)
      obj.class &lt;&lt; obj do
          define_method(method_to_mock) do
              return value
          end
      end
  end
  </code></pre>
  
  <p>I got the idea to write it like that from this post: <a href="https://stackoverflow.com/a/185969/356849">https://stackoverflow.com/a/185969/356849</a></p>
  
  <p>So then I can do things like: </p>
  
  <pre><code>mock(self.instantiated, :sections, sections)
  </code></pre>
  
  <p>and it would override the object I have stored in <code>self.instantiated</code>'s <code>sections</code> with my array of Section objects, <code>sections</code>. </p>
  
  <p>The reason why I'm doing this, is because I'm storing a serialized and encrypted version of of an object, and I want to be able to unencrypt and unserialize the object, and then restore all the relationships such that I can view that object in my views, as if it were being read from the database. but that's not important, and most of it's done.</p>
  
  <p>So, I want to be able to do this:</p>
  
  <p><code>mock(&lt;Instance of object&gt;, :&lt;method of object that is going to be overridden, to avoid db access&gt;, &lt;the stuff to return when the overridden method is invoked)</code></p>
  
  <p>CUrrently, I'm getting an error on the <code>obj.class &lt;&lt; obj do</code> line with this:</p>
  
  <pre><code>NoMethodError: undefined method `obj' for #&lt;MyObject::Encrypted:0x7f190eebcd18&gt;
  </code></pre>
  
  <p>ideas?</p>
  
  <hr>
  
  <p>UPDATE</p>
  
  <p>changed the second line to <code>class &lt;&lt; obj</code> which now infinite loops.</p>
  
  <pre><code>from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `retrieve_connection_pool'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `retrieve_connection_pool'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `retrieve_connection'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:1305:in `columns'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:1318:in `column_names'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/ordering.rb:35:in `ordering_condition_details'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/ordering.rb:26:in `method_missing'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/or_conditions.rb:28:in `method_missing'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:2002:in `method_missing_without_paginate'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/will_paginate-2.3.16/lib/will_paginate/finder.rb:170:in `method_missing_without_attr_encrypted'
  from /home/me/.rvm/gems/ruby-1.8.7-p371@project/bundler/gems/attr_encrypted-a4b25f01d137/lib/attr_encrypted/adapters/active_record.rb:50:in `method_missing'
  from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:119:in `mock'
  from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:79:in `instantiate'
  from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:58:in `each'
  from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:58:in `instantiate'
  </code></pre>