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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:57:12+00:00 2026-05-16T05:57:12+00:00

I have 2 classes whose object should act as partners. The first one is

  • 0

I have 2 classes whose object should act as “partners”. The first one is my Thing class, whose instances should act as Tree::TreeNodes of the gem RubyTree.

Basically, this delegation can be implemented using Forwardable:

class Thing < NoClassInheritancePlease
  extend Forwardable

  def initialize(title = "node")
    @node = Tree::TreeNode.new title

    # Collect node methods that should be delegated
    node_methods = @node.public_methods(false)
    node_methods += @node.protected_methods
    node_methods -= (public_methods(false) + protected_methods(false) + private_methods) # own methods should not been delegated

    # Set up delegation of specified node methods as singleton methods
    for method in node_methods
      Base.def_delegator :@node, method
    end
  end
end

Problem:
A number of TreeNode methods refer to self. For example:

def each(&block)             # :yields: node
  yield self
  children { |child| child.each(&block) }
end

Thus, my_thing.each {...} yields self, i.e. the Tree::TreeNode object that belongs to my_thing but not the Thing object itself.

Another example:

siblings = []
parent.children {|my_sibling| siblings << my_sibling if my_sibling != self}
siblings

parent.children returns an Array of Things and therefore the condition never evaluates to false as my_sibling is a Thing (which is fine) but self is a Tree::TreeNode

Question: How to evaluate the instance methods of a class (e.g. Tree::TreeNode) in the context of another class (e.g. Thing)? (“overwrite self”)

I tried with UnboundMethods, but you can only bind an instance of the original receiving class to an unbound method.

  • 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-16T05:57:13+00:00Added an answer on May 16, 2026 at 5:57 am

    If you really want, you could use evil-ruby to solve this.

    require 'evil'
    class A; def m; self; end; end
    class B; end
    A.instance_method(:m).force_bind(B.new).call
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to write a template, that would take as argument classes, whose constructors have
I have classes structured like this: Public MustInherit Class A ' several properties End
Say I have classes class A{ //code for class A } class B{ //code
If I have classes of Type A and B: public class A { public
I have a few classes that define sequences whose values must be available both
I have a whole bunch of child classes that inherit from a parent class
I am using DBContext and have two classes whose properties are all virtual. I
I have some code where instances of classes have parent<->child references to each other,
Guys I have a best practice question For example I have this classes: class
I have a C++ class(inside a dll project) whose member variables are boost::shared_ptrs to

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.