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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:40:44+00:00 2026-06-13T16:40:44+00:00

I am using Ruby 1.9.2 and the Ruby on Rails v3.2.2 gem. After my

  • 0

I am using Ruby 1.9.2 and the Ruby on Rails v3.2.2 gem. After my previous question on how to “nest” the inclusion of modules when using the Ruby on Rails ActiveSupport::Concern feature, I would like to understand where I should state methods added to a class by including “nested” modules in order to make these instance methods of that class. That is, I have the following:

class MyClass < ActiveRecord::Base
  include MyModuleA
end

module MyModuleA
  extend ActiveSupport::Concern

  included do
    include MyModuleB
  end
end

module MyModuleB
  extend ActiveSupport::Concern

  included do
    # def my_method
    #   ...
    # end
  end

  # def my_method
  #   ...
  # end
end

Should I state def my_method ... end in the “body” / “context” / “scope” of MyModuleB or I should state that in the included do ... end block? What is the difference and what I should expect from that?

  • 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-13T16:40:45+00:00Added an answer on June 13, 2026 at 4:40 pm

    Methods in modules that get mixed into a class become instance methods on that class. While putting them in the included block would likely work, there’s no need to do it. This, by extension, works with modules, since you can include ModuleB in ModuleA and all its instance methods become instance methods on ModuleA, and once ModuleA is included on class Foo, all its instance methods (including those mixed in from B) become instance methods on Foo.

    A “traditional” mix-in looks like this:

    module Mixin
      def self.included(klass)
        klass.send :extend, ClassMethods
        klass.some_class_method
      end
    
      module ClassMethods
        def some_class_method
          puts "I am a class method on #{self.inspect}"
        end
      end
    
      def some_instance_method
        puts "I am an instance method on #{self.inspect}"
      end
    end
    
    class Foo
      include Mixin
    end
    
    Foo.new.some_instance_method
    
    # Output:
    # I am a class method on Foo
    # I am an instance method on #<Foo:0x00000002b337e0>
    

    ActiveSupport::Concern just pretties this up a bit by automatically including a module named ClassMethods and by running the included block in the context of the including class, so the equivalent is:

    module Mixin
      extend ActiveSupport::Concern
    
      included do
        some_class_method
      end
    
      module ClassMethods
        def some_class_method
          puts "I am a class method on #{self.inspect}"
        end
      end
    
      def some_instance_method
        puts "I am an instance method on #{self.inspect}"
      end
    end
    
    class Foo
      include Mixin
    end
    
    Foo.new.some_instance_method
    
    # Output:
    # I am a class method on Foo
    # I am an instance method on #<Foo:0x000000034d7cd8>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3.2.2 and the I18n 0.6.0 gem. I would
I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I would like
I am using Ruby on Rails 3.2.2 and Cucumber with the cucumber-rails gem. I
I am using Ruby on Rails 3.2.2 and I am experimenting the Squeel gem.
Using Ruby 1.8.7-p358, Rails 3.0.12, gem responder, gem simple-form; jquery_ujs.js I have a form
I am using the following Spreadsheet gem to generate an excel sheet from ruby-on-rails..
I'm using gem bundler (v.0.9.6) and Rails 2.3.5, rubygems 1.3.6 and ruby 1.8.7 (On
I am using Ruby on Rails, but I think this question could be applied
Using ubuntu 10.04, rvm. At first, I've installed ruby 1.9.2 with rvm, gem rails
I'm using RVM, Ruby 1.9.2 and Rails 3.0.3 installed. I installed the curb-fu gem

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.