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

  • Home
  • SEARCH
  • 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 8316817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:26:38+00:00 2026-06-08T21:26:38+00:00

In Ruby, I can do this: module Foo end class Bar include Foo end

  • 0

In Ruby, I can do this:

module Foo
end

class Bar
  include Foo
end

module Foo
  def do_something_instancey
    puts "I'm an instance!"
  end
end

Then, if I instantiate a Bar object, I can call do_something_instancey on it:

b = Bar.new
b.do_something_instancey

However, if I do this…

module Foo
  def self.included(base)
    def base.do_something_classy do
      puts "I'm a class!"
    end
  end
end

My understanding is that because I included Foo in Bar before defining that class method, I cannot call Bar.do_something_classy because it never got “attached” to Bar.

I realize that might be slightly inaccurate/not really the right terminology. Regardless, is there a way, in the above example, to attach a class method to Bar from Foo after the module has already been included?

  • 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-08T21:26:39+00:00Added an answer on June 8, 2026 at 9:26 pm

    Here’s an example for both, class and instance methods:

    module Foo
      def self.included(base)
        base.extend(ClassMethods)
      end
    
       module ClassMethods
       end
    end
    
    class Bar
      include Foo
    end
    
    module Foo
      def do_something_instancey
        puts "I'm an instance!"
      end
    
      module ClassMethods
        def do_something_classy
          puts "I'm a class!"
        end
      end
    end
    
    b = Bar.new
    b.do_something_instancey
    # => I'm an instance!
    Bar.do_something_classy
    # => I'm a class!
    

    To add class methods to each class that has (already) included a specific module, you could traverse Ruby’s ObjectSpace:

    ObjectSpace.each_object(Class) do |klass|
      if klass.include? Foo
        klass.define_singleton_method(:do_something_classy) do
          puts "I'm a class!"
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ruby code like this: module Hello class Hi def initialize() puts self.module.name
In this Ruby code: Module M Class C < Struct.new(:param) def work M::helper(param) end
You can call include to mixin a module with a class in ruby, but
is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
How can I do this on Ruby? puts some_method(ò) # => &ograve; In other
I want to add a foo method to Ruby's Kernel module, so I can
In my class, I want to include multiple modules. Each module can define its
module CallableDict def method_missing method puts x[#{method}] end end The above code is my
This is a Ruby design problem. How can I make a reusable flat file
I am a Ruby newbie. How can I write better for this function? can

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.