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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:40:35+00:00 2026-05-29T06:40:35+00:00

module Imodule ??? end class Some include Imodule def self.imethod puts original end end

  • 0
module Imodule
  ???
end

class Some
  include Imodule

  def self.imethod
    puts "original"
  end
end

Some.imethod
# => "overrided"

How to create a module which will override static method?

This is an interview question for deep understanding ruby features. Don’t suggest another formulation of the problem 🙂

  • 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-29T06:40:36+00:00Added an answer on May 29, 2026 at 6:40 am

    Ok, here’s a working code. Note that you don’t even have to touch target class! 🙂

    class Klass
      def self.say
        puts 'class'
      end
    end
    
    module FooModule
      def self.included base
        base.instance_eval do
          def say
            puts "module"
          end
        end
      end
    end
    
    
    Klass.send(:include, FooModule)
    
    Klass.say
    

    Explanation

    Now classic way of mixing in class methods is this (and it doesn’t solve the problem, of course).

    module FooModule
      def self.included base
        base.extend ClassMethods
      end
    
      module ClassMethods
        def bar
          puts "module"
        end
      end
    end
    
    class Klass
      include FooModule
    
      def self.bar
        puts 'class'
      end
    end
    
    
    Klass.bar #=> class
    

    When modules are included or extended into a class, its methods are placed right above this class’ methods in inheritance chain. This means that if we were to call super in that class method, it would print “module”. But we don’t want to touch original class definition, we want to alter it from outside.

    So, can we do something?

    Good for us, ruby has a concept of “open classes”. This means that we can change virtually everything in the app, even some 3rd-party libraries. Every class can “opened” and new methods can be added to it, or old methods can be redefined. Let’s look how it works.

    class Klass
      def self.bar
        puts 'class'
      end
    end
    
    class Klass
      def self.bar
        puts 'class 2'
      end
    end
    
    Klass.bar #=> class 2
    

    The second class definition does not overwrite previous one, it opens and alters it. In this case, it happened to define a method with the same name. This resulted in old method being overwritten by the new one. This works with any classes, even base library classes.

    puts [1, 2, 3].to_s #=> [1, 2, 3]
    
    class Array
      def to_s
        "an array: #{join ', '}"
      end
    end
    
    puts [1, 2, 3].to_s #=> an array: 1, 2, 3
    

    Or the same code can be rewritten as

    puts [1, 2, 3].to_s #=> [1, 2, 3]
    
    Array.class_eval do
      def to_s
        "an array: #{join ', '}"
      end
    end
    
    puts [1, 2, 3].to_s #=> an array: 1, 2, 3
    

    Applying the knowledge

    Let’s start with simpler things, like overriding an instance method.

    class Klass
      def say
        puts 'class'
      end
    end
    
    module FooModule
      def self.included base
        base.class_eval do
          def say
            puts "module"
          end
        end
      end
    end
    
    
    Klass.send(:include, FooModule)
    
    Klass.new.say #=> module
    

    Modules have a special callback that gets called every time a module is included in a class. We can use that to call class_eval on that class and redefine a method.

    Replacing a class method is done in a similar way.

    class Klass
      def self.say
        puts 'class'
      end
    end
    
    module FooModule
      def self.included base
        base.instance_eval do
          def say
            puts "module"
          end
        end
      end
    end
    
    
    Klass.send(:include, FooModule)
    
    Klass.say #=> module
    

    The only difference here is that we call instance_eval instead of class_eval. This can be a very confusing part. In short, class_eval creates instance methods and instance_eval creates class methods.

    This is taken from my blog post.

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

Sidebar

Related Questions

module MyModule def my_method; 'hello'; end end class MyClass class << self include MyModule
module Hints module Designer def self.message Hello, World! end end end is there any
module Superpower # instance method def turn_invisible ... end # module method def Superpower.turn_into_toad
This is ModuleInit.cs in Products module public class ModuleInit : IModule { private readonly
module Hints module Designer def message Hello, World! end end end p Hints::Designer.message Why
This module is part of a simple todo app I made with Python... def
I'v got the following code : public class MyModule: IModule { private IRegionManager mRegionManager
How can I create an array of my class? I see it something like
I've got an interface interface IModule { public function Install(); } and some classes
I used SWIG to wrap my c++ class. Some methods have a const std::string&

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.