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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:34:56+00:00 2026-06-01T13:34:56+00:00

module Test def self.model_method puts this is a module method end end class A

  • 0
module Test
  def self.model_method
    puts "this is a module method"
  end
end

class A
  include Test
end

A.model_method

this will be error with:

undefined method `model_method’ for A:Class (NoMethodError)

But when I use metaclass of A. it works:

module Test
  def model_method
    puts "this is a module method"
  end
end

class A
  class << self
    include Test
  end
end

A.model_method

Can someone explain this?

  • 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-01T13:34:57+00:00Added an answer on June 1, 2026 at 1:34 pm

    If you want to have both class methods and instance methods mixed into a class when including a module, you may follow the pattern:

    module YourModule
      module ClassMethods
        def a_class_method
          puts "I'm a class method"
        end
      end
    
      def an_instance_method
        puts "I'm an instance method"
      end
    
      def self.included(base)
        base.extend ClassMethods
      end
    end
    
    class Whatever
      include YourModule
    end
    
    Whatever.a_class_method
    # => I'm a class method
    
    Whatever.new.an_instance_method
    # => I'm an instance method
    

    Basically to over-simplify it, you extend to add class methods and you include to add instance methods. When a module is included, it’s #included method is invoked, with the actual class it was included in. From here you can extend the class with some class methods from another module. This is quite a common pattern.

    See also: http://api.rubyonrails.org/classes/ActiveSupport/Concern.html

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

Sidebar

Related Questions

I need some like this: module One def test; puts 'Test One'; end end
I'm trying to figure why this code: class BaseClass end module Extensions def self.included(base)
test_module.rb module MyModule def module_func_a puts module_func_a invoked private_b end module_function :module_func_a private def
I have the following program. module C def self.included(base) base.extend(ClassMethods) end module ClassMethods def
I have a module like this (but more complicated): module Aliasable def self.included(base) base.has_many
In the following code, module Test @connection = nil def self.included?(base) @connection = base
When creating a class in Python, I can simply make a def __len__(self): method
I`d like to know how I could unit-test the following module. def download_distribution(url, tempdir):
I have this code in my every model. Class people def before_validation @attributes.each do
I'm trying to use new-style properties declaration: class C(object): def __init__(self): self._x = 0

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.