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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:31:26+00:00 2026-06-14T08:31:26+00:00

I have a module lets call Calculator that I want to include in a

  • 0

I have a module lets call “Calculator” that I want to include in a class “Product”. Calculator will extend “Product” which will copy the class methods onto Product. One of these class methods is “memoize”. The idea being that I can do something like this:

module Calculator
  def self.extended(base)
    base.memoize :foo_bar
  end
end

With the purpose of memoizing the method (specifically a class method) :foo_bar. Inside of memoize I call the method “alias_method” which attempts to alias a class method to a different name (here :foo_bar). This fails. Memoize looks something like:

module Calculator (the extended module)
  def memoize(name)
    alias_method "memoized_#{name}", name
  end
end

When this is called via memoize :foo_bar, the alias_method line kicks an error saying Product has no method “name”.. my understanding is this is because alias_method will attempt to alias instance methods not class methods.. (I don’t know why but ok no big deal)..

I can reopen the eigenclass like so

module Calculator
  def memoize(name)
    class << self
      alias_method "memoized_#{name}", name
    end
   end
end

This would work but name is not available to the scope of the class << self definition. People have mentioned using self.class_eval and self.instance_eval but neither of these seem to work.. I’d like my cake and eat it too.. how can I keep alias_method dynamic but use it on class_methods?

  • 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-14T08:31:27+00:00Added an answer on June 14, 2026 at 8:31 am

    So just learned that this will do the trick:

    module Calculator
      def memoize
        define_singleton_method(name, method(name))
      end
    end
    

    Then when Calculator gets included in Product it will define the singleton method as I needed. I still don’t know why alias_method needs to only work on instance methods.. and I don’t know why class_eval or instance_eval didn’t solve the problem.. but at least I have a solution..

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

Sidebar

Related Questions

I have this module pattern that stores a bunch of vars. I want to
I have .so (shared library) written in C++, lets call it functionality.so in which
Currently I have a static class that I use as my logging module. I’ve
I'm really struggling with something here. I have a class module, let's call it
I have an abstract Base class. Let's call it Animal::Base . module Animal class
I have developed a module that lets admin to select template for a specified
I have software that first loads a .dylib lets call libFirst.dylib using the following
I am writing a module, let's call it base , which will show a
Let's say that I have a module that has a Queue in it. For
I have module application. When I run it, the main window of that app

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.