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 8694529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:48:31+00:00 2026-06-13T00:48:31+00:00

I am using Ruby v1.9.2 and the Ruby on Rails v3.2.2 gem. I had

  • 0

I am using Ruby v1.9.2 and the Ruby on Rails v3.2.2 gem. I had the following module

module MyModule
  extend ActiveSupport::Concern

  included do
    def self.my_method(arg1, arg2)
      ...
    end
  end
end

and I wanted to alias the class method my_method. So, I stated the following (not working) code:

module MyModule
  extend ActiveSupport::Concern

  included do
    def self.my_method(arg1, arg2)
      ...
    end

    # Note: the following code doesn't work (it raises "NameError: undefined
    # local variable or method `new_name' for #<Class:0x00000101412b00>").
    def self.alias_class_method(new_name, old_name)
      class << self
        alias_method new_name, old_name
      end
    end

    alias_class_method :my_new_method, :my_method
  end
end

In other words, I thought to extend the Module class someway in order to add an alias_class_method method available throughout MyModule. However, I would like to make it to work and to be available in all my Ruby on Rails application.

  1. Where I should put the file related to the Ruby core extension of the Module class? Maybe in the Ruby on Rails lib directory?
  2. How should I properly “extend” the Module class in the core extension file?
  3. Is it the right way to proceed? That is, for example, should I “extend” another class (Object, BasicObject, Kernel, …) rather than Module? or, should I avoid implementing the mentioned core extension at all?

But, more important, is there a Ruby feature that makes what I am trying to accomplish so that I don’t have to extend its classes?

  • 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-13T00:48:32+00:00Added an answer on June 13, 2026 at 12:48 am

    You could use define_singleton_method to wrap your old method under a new name, like so:

    module MyModule
      def alias_class_method(new_name, old_name)
        define_singleton_method(new_name) { old_name }
      end
    end
    
    class MyClass
      def my_method
        puts "my method"
      end
    end
    
    MyClass.extend(MyModule)
    MyClass.alias_class_method(:my_new_method, :my_method)
    MyClass.my_new_method     # => "my method"
    

    Answering your comment, you wouldn’t have to extend every single class by hand. The define_singleton_method is implemented in the Object class. So you could simply extend the Object class, so every class should have the method available…

    Object.extend(MyModule)
    

    Put this in an initializer in your Rails app and you should be good to go…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following Spreadsheet gem to generate an excel sheet from ruby-on-rails..
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'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, if I do a gem help install a part of
I'm using Ruby on Rails and the paths_of_glory gem I need to access the
I am using Ruby on Rails 3.1.0 and the YARD 0.7.4 gem for documentation
I am using Ruby on Rails 3.1.0 and the YARD 0.7.4 gem for documentation

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.