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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:26:37+00:00 2026-05-27T23:26:37+00:00

In the code below, I would like to call the class method done of

  • 0

In the code below, I would like to call the class method done of the class that includes the module from inside self.hello

Explanation:

A::bonjour will call Mod::hello so will B::ciao

I would like to be able to detect the “calling class” (A or B) in Mod::hello in order to be able to call the A::done or B::done

module Mod
 def self.hello
  puts "saying hello..."
 end
end

class A
 include Mod

 def self.bonjour
  Mod::hello 
 end

 def self.done
  puts "fini"
 end
end

class B
 include Mod

 def self.ciao
  Mod::hello
 end

 def self.done
  puts "finitto"
 end
end
  • 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-27T23:26:38+00:00Added an answer on May 27, 2026 at 11:26 pm

    While (perhaps) not as clean as Niklas’ answer, it’s still easily doable, and IMO cleaner than the usage pattern shown in the OP which relies on knowing which module is mixed in.

    (I prefer not having to pass an argument to mixin methods like this when other means exist.)

    The output:

    pry(main)> A::bonjour
    saying hello...
    fini
    pry(main)> B::ciao
    saying hello...
    finitto
    

    The guts:

    module Mod
      module ClassMethods
        def hello
          puts "saying hello..."
          done
        end
      end
    
      def self.included(clazz)
        clazz.extend ClassMethods
      end
    end
    

    The modified class declarations, removing the explicit module reference:

    class A
      include Mod
    
      def self.bonjour
        hello
      end
    
      def self.done
        puts "fini"
      end
    end
    
    class B
      include Mod
    
      def self.ciao
        hello
      end
    
      def self.done
        puts "finitto"
      end
    end
    

    You may also supply a default implementation of done:

    module Mod
      module ModMethods
        def hello
          puts "saying hello..."
          done
        end
    
        def done
          throw "Missing implementation of 'done'"
        end
      end
    
      def self.included(clazz)
        clazz.extend ModMethods
      end
    end
    

    As a comment to this post points out, if the snippet in the OP is a faithful representation of the actual usecase, you might as well use extend (instead of include), leaving everything cleaner:

    module Mod
      def hello
        puts "saying hello..."
        done
      end
    
      def done
        raise NotImplementError("Missing implementation of 'done'")
      end
    end
    

    And the classes using extend:

    class A
      extend Mod
    
      def self.bonjour
        hello
      end
    
      def self.done
        puts "fini"
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like some feedback regarding the IService class listed below. From what I
I would like to implement a parallel version of the code below using threads
For the code below, I would like to append echo $_SERVER['QUERY_STRING']; to the end
I would like to know how to modify the below code to strip =20
I understand that the code below would result segmentation fault because at the cstr
Consider the Java code below, what would happen if there were no paintComponent method
Given the code below, how would you create/implement SR.h so that it produces the
I have a Moose class that i would like to store using Apache::Session::File. However,
The script below illustrates a capability of set and frozenset that I would like
I have an onClickListener that triggers a network call so I would like to

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.