Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:38:38+00:00 2026-06-13T03:38:38+00:00

I am using Ruby 1.9.2 and the Ruby on Rails v3.2.2 gem. I am

  • 0

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 instance method in the included do ... end block provided by the RoR ActiveSupport::Concern module:

module MyModule
  extend ActiveSupport::Concern

  included do
    # Builds the instance method name.
    my_method_name = build_method_name.to_sym # => :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 << self; self end
    singleton_class.send(:alias_method, :my_new_method, my_method_name)        
  end
end

“Newbiely” speaking, with a search on the Web I came up with the singleton_class = class << self; self end statement and I used that (instead of the class << self ... end block) in order to scope the my_method_name variable, making the aliasing generated dynamically.

I would like to understand exactly why and how the singleton_class 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:38:39+00:00Added an answer on June 13, 2026 at 3:38 am

    I recommend Yehuda Katz’s post on metaprogamming on Ruby’s self. Here’s my humble summary in response to your question:

    In Ruby, all objects have a singleton class (also known as metaclass). Objects inherit first from their singleton class invisibly, then from their explicit class. Ruby classes themselves have their own singleton classes since classes are objects as well. The class << idiom is simply Ruby’s syntax for accessing the scope of an object’s singleton class.

     class Person
       class << self
         # self in this scope is Person's singleton class
       end
     end
    
     person = Person.new
     person_singleton_class = class << person; self; end
    

    Your version of Rails actually provides singleton_class as a shortcut. Since singleton_class is an available method, you don’t need to assign it to a variable in the expression singleton_class = class << self; self end:

    Person.singleton_class 
    
    person = Person.new
    person.singleton_class
    

    Since a class inherits directly from its singleton class, this is where we want to add class methods dynamically when metaprogramming. Ruby provides a few ways to open up the scope of an object while maintaining access to the surrounding scope: class_eval and instance_eval. There are subtle differences in the way these behave (Yehuda’s post explains this), but you may use either to enter the scope of your singleton class, resolve methods on the singleton class as self and still have access to my_method_name from the surrounding scope.

    All that said, you could make a few small changes to your module:

    module MyModule
      extend ActiveSupport::Concern
    
      included do
        # Builds the instance method name.
        my_method_name = build_method_name.to_sym # => :my_method
    
        # Defines the :my_method instance method in the including class of MyModule.
        define_singleton_method(my_method_name) do |*args|
          # ...
        end
    
        singleton_class.class_eval do
          # method resolution in scope of singleton class
          alias_method :my_new_method, my_method_name
        end
    
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3.2.2 and Cucumber with the cucumber-rails gem. I
I am using Ruby on Rails 3.2.2 and I am experimenting the Squeel gem.
I am using Ruby on Rails 3.2.2 and the I18n 0.6.0 gem. I would
Using Ruby 1.8.7-p358, Rails 3.0.12, gem responder, gem simple-form; jquery_ujs.js I have a form
I am using the following Spreadsheet gem to generate an excel sheet from ruby-on-rails..
I'm using gem bundler (v.0.9.6) and Rails 2.3.5, rubygems 1.3.6 and ruby 1.8.7 (On
Using Ruby on Rails. I'm trying to sort a query by number (saved as
I am using Ruby on Rails, but I think this question could be applied
I'm trying to implement the rails-settings gem (https://github.com/100hz/rails-settings) into my Rails 3 project using
I'm using the acts_as_taggable_on gem on a Rails (Ruby 1.9.3) project. I provide a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.