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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:02:21+00:00 2026-06-11T17:02:21+00:00

Let say we have classes A,B,C. A def self.inherited(sub) # meta programming goes here

  • 0

Let say we have classes A,B,C.

A
 def self.inherited(sub)
   # meta programming goes here
   # take class that has just inherited class A
   # and for foo classes inject prepare_foo() as 
   # first line of method then run rest of the code
 end

 def prepare_foo
   # => prepare_foo() needed here
   # some code
 end

end

B < A
  def foo
    # some code
  end
end

C < A
  def foo
    # => prepare_foo() needed here
    # some code
  end
end

As you can see I am trying to inject foo_prepare() call to each one of foo() methods.

How can that be done?

Also I have been thinking about overriding send class in class A that way I would run foo_prepare and than just let send (super) to do rest of the method.

What do you guys think, what is the best way to approach this problem?

  • 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-11T17:02:22+00:00Added an answer on June 11, 2026 at 5:02 pm

    Here’s a solution for you. Although it’s based on module inclusion and not inheriting from a class, I hope you will still find it useful.

    module Parent
      def self.included(child)
        child.class_eval do
          def prepare_for_work
            puts "preparing to do some work"
          end
      
          # back up method's name
          alias_method :old_work, :work
      
          # replace the old method with a new version, which has 'prepare' injected
          def work
            prepare_for_work
            old_work
          end
        end
      end
    end
    
    class FirstChild
      def work
        puts "doing some work"
      end
    
      include Parent # include in the end of class, so that work method is already defined.
    end
    
    fc = FirstChild.new
    fc.work
    # >> preparing to do some work
    # >> doing some work
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following classes set up: class Foo: def __init__(self, frob,
Let's say, I have the following two classes: class A(object): def __init__(self, i): self.i
Let's say I have a bunch of classes that look something like... class Foo{
Let's say I have a class: class Thing(object): cachedBar = None def __init__(self, foo):
Let's say I have this class: class ComponentLibrary def self.register(klass); ...; end end And
Let's say I have two classes that look like this: public class ByteFilter {
Let's say that I have classes like this: public class Parent { public int
Let say I have the following classes, Public Class Base Public Event PropertyChanged() Sub
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();

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.