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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:01:29+00:00 2026-06-04T07:01:29+00:00

It is known that in Ruby, class methods get inherited: class P def self.mm;

  • 0

It is known that in Ruby, class methods get inherited:

class P
  def self.mm; puts 'abc' end
end
class Q < P; end
Q.mm # works

However, it comes as a surprise to me that it does not work with mixins:

module M
  def self.mm; puts 'mixin' end
end
class N; include M end
M.mm # works
N.mm # does not work!

I know that #extend method can do this:

module X; def mm; puts 'extender' end end
Y = Class.new.extend X
X.mm # works

But I am writing a mixin (or, rather, would like to write) containing both instance methods and class methods:

module Common
  def self.class_method; puts "class method here" end
  def instance_method; puts "instance method here" end
end

Now what I would like to do is this:

class A; include Common
  # custom part for A
end
class B; include Common
  # custom part for B
end

I want A, B inherit both instance and class methods from Common module. But, of course, that does not work. So, isn’t there a secret way of making this inheritance work from a single module?

It seems inelegant to me to split this into two different modules, one to include, the other to extend. Another possible solution would be to use a class Common instead of a module. But this is just a workaround. (What if there are two sets of common functionalities Common1 and Common2 and we really need to have mixins?) Is there any deep reason why class method inheritance does not work from mixins?

  • 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-04T07:01:30+00:00Added an answer on June 4, 2026 at 7:01 am

    A common idiom is to use included hook and inject class methods from there.

    module Foo
      def self.included base
        base.send :include, InstanceMethods
        base.extend ClassMethods
      end
    
      module InstanceMethods
        def bar1
          'bar1'
        end
      end
    
      module ClassMethods
        def bar2
          'bar2'
        end
      end
    end
    
    class Test
      include Foo
    end
    
    Test.new.bar1 # => "bar1"
    Test.bar2 # => "bar2"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby, is there a way to get the name of the class that
Does anyone know of a library that works with Ruby 1.9.1 on Rails 3
I have the following: module Thing def self.included(base) base.send :extend, ClassMethods end module ClassMethods
I've inherited a large pile of Ruby code that's, frankly, close to impossible to
I know that I can dynamically define methods on a class using define_method ,
I didn't know that installing ruby is such a pain After lot of trouble
So I know in ruby that x.nil? will test if x is null. What
Some of you may know the method in Ruby that allows you to reverse
I known that with subversion you can attach arbitrary properties to each files. In
Its known that Java ArrayList is implemented using arrays and initializes with capacity of

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.