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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:14:07+00:00 2026-05-25T23:14:07+00:00

Given a module with a singleton method like this: module Foo class << self

  • 0

Given a module with a singleton method like this:

module Foo
  class << self
    def bar
      puts "method bar from Foo"
    end
  end
end

How can I override Foo.bar using another module?

  • 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-05-25T23:14:08+00:00Added an answer on May 25, 2026 at 11:14 pm

    Extend and alias

    My problem was that I forgot to think through the inheritance chain. I was
    looking for a way to override the method by modifying the inheritance chain, but
    that’s not possible.

    The reason is that bar is defined on Foo itself, so it never looks up its inheritance chain for the method. Therefore, to change bar, I have to change it on Foo itself.

    While I could just re-open Foo, like this:

    module Foo
      def self.bar
        puts "new foo method"
      end
    end
    

    … I prefer a way to be able to wrap the original bar method, as though I
    were subclassing and could call super. I can achieve that by setting up an
    alias for the old method.

    module Foo
      class << self
        def bar
          "method bar from Foo"
        end
      end
    end
    
    puts Foo.bar # => "method bar from Foo"
    
    module FooEnhancement
    
      # Add a hook - whenever a class or module calls `extend FooEnhancement`,
      # run this code
      def self.extended(base)
    
        # In the context of the extending module or class 
        # (in this case, it will be Foo), do the following
        base.class_eval do
    
          # Define this new method
          def self.new_bar
            "#{old_bar} - now with more fiber!"
          end
    
          # Set up aliases. 
          # We're already in the context of the class, but there's no
          # `self.alias`, so we need to call `alias` inside this block
          class << self
    
            # We can call the original `bar` method with `old_bar`
            alias :old_bar :bar
    
            # If we call `bar`, now we'll get our `new_bar` method
            alias :bar :new_bar
          end
        end
    
      end
    
    end
    
    # This will fire off the self.extended hook in FooEnhancement
    Foo.extend FooEnhancement
    
    # Calls the enhanced version of `bar`
    puts Foo.bar # => 'method bar from Foo - now with more fiber!'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given module Foo def bar puts foobar end end I can do String.extend(Foo) and
Given the following module, module Foo def bar :baz end end def send_to_foo(method) #
Given a situation such as: module Extension def self.included(recipient) recipient.extend(ModelClassMethods) end module ModelClassMethods def
Given the following: class User; attr_accessor :roles; end module RegisteredUser def default_context Submission end
Given the following module: class Dummy(dict): def __init__(self, data): for key, value in data.iteritems():
In Python, given a module X and a class Y, how can I iterate
Given: def test_to_check_exception_is_thrown(self): # Arrange c = Class() # Act and Assert self.assertRaises(NameError, c.do_something)
In Ruby, how can I programmatically determine which class/module defines a method being called?
given a list of module names (e.g. mymods = ['numpy', 'scipy', ...]) how can
Given a perl module Foo.pm with methods aSub() and bSub() my $obj = Foo->new();

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.