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

  • Home
  • SEARCH
  • 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 5977381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:21:45+00:00 2026-05-22T21:21:45+00:00

Possible Duplicate: Overriding method by another defined in module Here’s some code: class Foo

  • 0

Possible Duplicate:
Overriding method by another defined in module

Here’s some code:

class Foo
  def bar
    puts "Original bar"
  end
end

module M
  def bar
    puts "Called M::bar"    
  end
end

Foo.send(:include,M)
Foo.new.bar
# => Original bar

Does ruby prevent overriding a previously defined method when a method of the same name is “included”?

  • 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-22T21:21:45+00:00Added an answer on May 22, 2026 at 9:21 pm

    I don’t quite understand your question. What, exactly, do you think is “prevented” here, and by whom?

    This is precisely how it is supposed to work. Module#include mixes in the module as the direct superclass of whatever class it is being mixed into. M is a superclass of Foo, so Foo#bar overrides M#bar, because that’s how inheritance works: subclasses override superclasses, not the other way around. Nothing is being “prevented” here, of course you can still override Foo#bar in a subclass of Foo.

    You can clearly see the ancestry:

    class FooS; end
    module M; end
    class Foo < FooS; include M end
    
    Foo.ancestors # => [Foo, M, FooS, Object, Kernel, BasicObject]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class
Possible Duplicate: Overriding vs Virtual In C++, whether you choose to use virtual or
Possible Duplicate: How to copy part of an array to another array in C#
Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: Why is it important to override GetHashCode when Equals method is overridden?
Possible Duplicate: What's the difference between IEquatable and just overriding Object.Equals() ? I know
Possible Duplicate: zebra stripes over riding my highlighting color I am using this code
Possible Duplicate: Why do I see a double variable initialized to some value like
Possible Duplicate: Create Generic method constraining T to an Enum Is there any reason
Possible Duplicate: PHP class instantiation. To use or not to use the parenthesis? I'm

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.