title: Understanding the singleton class when aliasing a instance method
body: <p>I am using Ruby 1.9.2 and the Ruby on Rails v3.2.2 gem. I am trying to learn Metaprogramming "the right way" and at this time I am aliasing an <em>instance</em> method in the <code>included do ... end</code> block provided by the RoR <a href="http://api.rubyonrails.org/classes/ActiveSupport/Concern.html" rel="nofollow"><code>ActiveSupport::Concern</code></a> module:</p>
  
  <pre><code>module MyModule
    extend ActiveSupport::Concern
  
    included do
      # Builds the instance method name.
      my_method_name = build_method_name.to_sym # =&gt; :my_method
  
      # Defines the :my_method instance method in the including class of MyModule.
      define_singleton_method(my_method_name) do |*args|
        # ...
      end
  
      # Aliases the :my_method instance method in the including class of MyModule.
      singleton_class = class &lt;&lt; self; self end
      singleton_class.send(:alias_method, :my_new_method, my_method_name)        
    end
  end
  </code></pre>
  
  <p>"Newbiely" speaking, with a search on the Web I came up with the <code>singleton_class = class &lt;&lt; self; self end</code> statement and I used that (instead of the <code>class &lt;&lt; self ... end</code> block) in order to <em>scope</em> the <code>my_method_name</code> variable, making the aliasing generated dynamically.</p>
  
  <p>I would like to understand exactly <em>why</em> and <em>how</em> the <code>singleton_class</code> works in the above code and if there is a better way (maybe, a more maintainable and performant one) to implement the same (aliasing, defining the singleton method and so on), but "the right way" since I think it isn't so.</p>