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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:58:34+00:00 2026-06-10T00:58:34+00:00

Given a class A and a module B, mixin the instance methods of B

  • 0

Given a class A and a module B, mixin the instance methods of B so that it overrides the correspnding instance methods of A.

module B
  def method1
    "B\#method1"
  end

  def method2
    "B\#method2"
  end
end

class A
  def method1
    "A\#method1"
  end

  def method2
    "A\#method2"
  end

  # include B    does not override instance methods!
  #              (module gets mixed into the superclass)
end

puts A.new.method1   # want it to print out "B#method1"
puts A.new.method2   # want it to print out "B#method2"
  • 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-10T00:58:36+00:00Added an answer on June 10, 2026 at 12:58 am

    You could remove each of B‘s methods from A before including B.

    class A
      def method1
        "A\#method1"
      end
    
      def method2
        "A\#method2"
      end
    
      B.instance_methods(false).each { |method|
        remove_method(method) if instance_methods(false).include?(method)
      }
      include B
    end
    

    Or from within B:

    module B
      def method1
        "B\#method1"
      end
    
      def method2
        "B\#method2"
      end
    
      def self.append_features(mod)
        instance_methods(false).each { |method|
          mod.send(:remove_method, method) if mod.instance_methods(false).include?(method)
        }
        super
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have method that returns module path of given class name def findModulePath(path, className):
Given the following: class User; attr_accessor :roles; end module RegisteredUser def default_context Submission end
Given a situation such as: module Extension def self.included(recipient) recipient.extend(ModelClassMethods) end module ModelClassMethods def
given module Foo def bar puts foobar end end I can do String.extend(Foo) and
Given the following module: class Dummy(dict): def __init__(self, data): for key, value in data.iteritems():
I understand that CoCreateInstance finds the COM server for the given class id, creates
This is what I have so far: def get_concrete_name_of_class(klass): Given a class return the
Given a custom, new-style python class instance, what is a good way to hash
I want to be able to have methods in a module that are not
Given the following models: class Module(models.Model): pass class Content(models.Model): module = models.ForeignKey(Module, related_name='contents') class

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.