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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:16:40+00:00 2026-06-10T08:16:40+00:00

Some code class Parent def print p Hi I’m the parent end end class

  • 0

Some code

class Parent

  def print
    p "Hi I'm the parent"
  end
end

class Child < Parent

  def initialize(num)
    @num = num
  end

  def print
    child_print
  end

  def child_print
    if @num == 1 
      #call parent.print 
    else
      p "I'm the child"
    end 
  end
end

c1 = Child.new(1)
c2 = Child.new(2)
c1.print
c2.print

Child is an instance of Parent. Print is the method exposed in the interface, and both classes define them. Child decides to do other things in a (possibly really complex) method, but will invoke its parent’s method under some condition.

I could just write

  def print
    if @num == 1 
      super
    else
      p "I'm the child"
    end 
  end

And that works, but what if it’s not just a simple one-liner comparison but instead is doing lots of complicated things that deserve to be separated into another method? It may have to do some calculations before deciding that the parent’s method should be called.

Or perhaps there is a different, better way to design it.

  • 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-10T08:16:42+00:00Added an answer on June 10, 2026 at 8:16 am
    Parent.instance_method(:print).bind(self).call
    

    This is already pretty readable, but here’s an explanation.

    1. Get the #print method of the Parent class
    2. Bind it to your current object
    3. Call it

    PS: You can even give arguments to #call and they will be relayed to the called method.

    PPS: That said, such code almost always hints at an issue in your class design. You should try to avoid it whenever possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is some code: class Person def initialize(age) @age = age end def age
Here is my code: class MyTestCase(Base): def setUp(self): #some code here def test_B(self): #some
Here's some example code: class Obj attr :c, true def == that p '=='
my code is following in python. class A(object): b = B() def d(self): print
I have written some code that uses attributes of an object: class Foo: def
I wrote following code: class node: def __init__(self, title, series, parent): self.series = series
I have some code where instances of classes have parent<->child references to each other,
I have following code. Parent Class: class Parent{ void a() { if(// some logic
I will first illustrate my issue with some code: class ExampleClass { private Vector2
Here is some code: var class = function(elem,div){ this.elem= elem; this.div = div; this.init

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.