title: how to define singleton method within a ruby method?
body: <p>In ruby 1.9, one could do this:</p>
  
  <pre><code>def foo
    val = "a string"
    def val.big
      self.upcase
    end
    val
  end
  </code></pre>
  
  <p>and one could call foo, with "a string" returned, or foo.big and get "A STRING" returned.</p>
  
  <p>However this is no longer possible in Ruby 2.1, an error "cannot define singleton method" is thrown. How would the methods be implemented in 2.1 for foo and foo.big?</p>
  
  <p>(Obviously this is a simplified example, what I need to do is implement a singleton method on the value returned by another method).</p>
  
  <p>UPDATE: it appears that this IS possible in ruby 2.1, but NOT in the context of an ActiveRecord class!</p>
  
  <p>SECOND UPDATE: When val is a string, this does work. My the problem surfaces when one tries to define a singleton class on a number like this:</p>
  
  <pre><code>def foo
    val = 42
    def val.smaller
       self - 11
    end
    val
  end
  </code></pre>
  
  <p>this throws the error "TypeError: can't define singleton"</p>