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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:04:35+00:00 2026-05-16T00:04:35+00:00

I am wondering, why is an included module’s methods mixed in to any subsequent

  • 0

I am wondering, why is an included module’s methods mixed in to any subsequent class definitions (as if the class included it in itself)?

module Foo
  def bar
    print "#{self}\n"
  end
end

class Bar
end

begin
  Bar.bar
rescue NoMethodError
  puts "There is no Bar.bar\n"
end

include Foo

bar
Bar.bar
Bar.new.bar

prints:

There is no Bar.bar
main
Bar
#<Bar:0xb73f2048>

Is this the expected behavior? Why?

  • 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-16T00:04:36+00:00Added an answer on May 16, 2026 at 12:04 am

    When you include Foo in your program but outside of any class or method then it is included in to the current scope which is the main object.

    You could test this by modifying your bar method to the following

      def bar
        print "InBar class: #{self.class} value: #{self}\n"
      end
    

    And then adding the following 2 lines at the end

    2.bar
    Fixnum.bar
    

    This would give you the following output

    There is no Bar.bar
    InBar class: Object value: main
    InBar class: Class value: Bar
    InBar class: Bar value: #<Bar:0x21ecec>
    InBar class: Fixnum value: 2
    InBar class: Class value: Fixnum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Ruby script, in which class Foo includes module Baz, module
I've been wondering about the way to get both class and instance methods into
I am wondering how javascripts get included in a jsp - can we put
Wondering if there is any way to get the lambda expressions that result from
I'm wondering if there is a way to print out all accessible variables in
I'm basically wondering what is included in each error level, as found here in
I'm wondering if mono.net is included in the default installation of Ubuntu, Kubuntu, and
I'm wondering, I have a flash map included in my site usually like <object...
I was just wondering what valid characters can be included as the value for
Wondering if you could help me with a simple SQL query. I have included

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.