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

The Archive Base Latest Questions

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

I want to merge two Ruby modules without breaking the lookup chain. Basically I

  • 0

I want to merge two Ruby modules without breaking the lookup chain. Basically I want the behavior of BothAnB to be exactly as if I concatenated the textual source code from A and B and the new foo replaces the old. The problem occurs when MRO linearizes an inheritance diamond.

module O
  def foo; puts "O" end
end

module A
  include O
  def foo; puts "A"; super end
  def aaa; puts "aaa" end
end

module B
  include O
  def foo; puts "B"; super end
  def bbb; puts "bbb" end
end

module BothAnB
  #insert magic here such that a class C that includes BothAnB:
  # C.new.foo => B O
  # C.new.aaa => aaa
  # C.new.bbb => bbb
end

module JustA
  #insert magic here such that a class C that includes JustA:
  # C.new.foo => A O
  # C.new.aaa => aaa
  # C.new.bbb => FAIL
end
#and similarly JustB

A and B are fairly complex modules that can have deep inheritance chains (this is for a meta-programming framework that allows programmers to do just that).

Include B, A doesn’t work because instead of the lookup BothAnB->B->A->O, I need it to be BothAnB->B->O(and optionally ->A).
I’ve gotten close by:

  • deep cloning entire inheritance tree of A (to remove diamond)
  • undef_method on the A’s clone to remove methods found in B
  • making a new method for Module to reflect this behavior

Is there a better solution than this? I would ideally want to keep at least some of the modules recognizable when calling BothAnB.ancestors.

[Note: I completely changed the question after getting two answers based on Phrogz’s feedback, so if they seem irrelevant they are]

  • 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-10T02:34:24+00:00Added an answer on June 10, 2026 at 2:34 am

    Here’s another possible trick:

    module BothAnB
      include A
      include O.clone
      include B
    end
    
    class C
      include BothAnB
    end
    
    C.new.foo
    C.new.aaa
    C.new.bbb
    
    # output:
    B
    O
    aaa
    bbb
    

    Here we make super in B#foo to point at O#foo instead of A#foo.

    If O is complex and includes other stuff, it may require more of such magic:

    module O
      # don't do this:
      # include Whatever
    
      # do this instead:
      def self.included(base)
        base.send(:include, Whatever.clone)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically i want to merge two Iqueryable to one Iqueryable and then return the
I want to merge two arrays and replace the text with strtr function. I
I want to merge two branches that have been separated for a while and
I want to merge two cells in excel using pyExcelerator , ws.write_merge(r1=0,r2=1,c1=0, c2=0, label='test1',
What i'm trying to do is pretty simple.I want to merge two maps. Say
I'm using classic ASP in my project. I want to merge two XMLs together.
I have to array and I want to merge this two array with a
I want to merge all data in two arraylist in c#. Some data in
I have two objects and I want to merge them: public class Foo {
I have two mysql tables and i want to merge the results of these

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.