Bird
Raised Fist0
Rubyprogramming~10 mins

Accessing parent methods in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Accessing parent methods
Define Parent Class with method
↓
Define Child Class inheriting Parent
↓
Create Child Object
↓
Call method on Child
↓
Inside Child method, call super
↓
Parent method runs
↓
Return to Child method
↓
Finish Child method
This flow shows how a child class method calls its parent's method using 'super' and then continues execution.
Execution Sample
Ruby
class Parent
  def greet
    puts "Hello from Parent"
  end
end

class Child < Parent
  def greet
    super
    puts "Hello from Child"
  end
end

Child.new.greet
This code shows a child class calling its parent's greet method using 'super' and then printing its own message.
Execution Table
StepActionEvaluationOutput
1Create Child objectChild object created
2Call greet on Child objectEnter Child#greet
3Call super inside Child#greetCalls Parent#greet
4Execute Parent#greetPrints 'Hello from Parent'Hello from Parent
5Return to Child#greet after superContinue Child#greet
6Print 'Hello from Child'Output messageHello from Child
7End Child#greetMethod finishes
💡 Method finishes after Child#greet completes all steps
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
objectnilChild instanceChild instanceChild instanceChild instanceChild instance
Key Moments - 2 Insights
Why do we use 'super' inside the child method?
Using 'super' calls the parent class's method with the same name, as shown in execution_table step 3 and 4, allowing the child to reuse or extend the parent's behavior.
What happens if we omit 'super' in the child method?
If 'super' is omitted, the parent's method is not called. The child method runs alone, so only the child's output appears, unlike steps 3 and 4 where the parent method runs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 4?
A"Hello from Parent"
B"Hello from Child"
C"Hello from Parent and Child"
DNothing is printed
💡 Hint
Check the Output column at step 4 in execution_table
At which step does the child method resume after calling the parent method?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the Action and Evaluation columns around steps 4 and 5 in execution_table
If we remove 'super' from the child greet method, what changes in the output?
ABoth messages are printed as before
BOnly 'Hello from Child' is printed
COnly 'Hello from Parent' is printed
DNo output is printed
💡 Hint
Refer to key_moments explanation about omitting 'super'
Concept Snapshot
Access parent methods in Ruby using 'super'.
Inside a child method, call 'super' to run the parent's method.
This lets you add to or modify behavior.
Without 'super', only the child's method runs.
Use 'super' carefully to control method chaining.
Full Transcript
This example shows how a child class in Ruby can call a method from its parent class using the keyword 'super'. First, a Parent class defines a greet method that prints a message. Then, a Child class inherits from Parent and defines its own greet method. Inside the child's greet, it calls 'super' to run the parent's greet method, printing the parent's message. After that, the child prints its own message. The execution table traces each step: creating the child object, calling greet, calling super, printing messages, and finishing. The variable tracker shows the child object remains the same throughout. Key moments clarify why 'super' is used and what happens if it is omitted. The visual quiz tests understanding of output and flow. The snapshot summarizes the concept simply.

Practice

(1/5)
1. In Ruby, how do you call a method from a parent class inside a child class method?
easy
A. Use parent.method_name
B. Call the parent method by its name directly
C. Use this.parent.method_name
D. Use super inside the child method

Solution

  1. Step 1: Understand method inheritance in Ruby

    Child classes inherit methods from parent classes but to call the parent's version inside an overridden method, you use super.
  2. Step 2: Identify the correct syntax for calling parent method

    Using super inside the child method calls the parent method with the same name.
  3. Final Answer:

    Use super inside the child method -> Option D
  4. Quick Check:

    Calling parent method = super [OK]
Hint: Use super to call parent method inside child [OK]
Common Mistakes:
  • Trying to call parent method by name directly
  • Using non-existent syntax like parent.method_name
  • Confusing this with Ruby's self
2. Which of the following is the correct syntax to call a parent class method with arguments in Ruby?
easy
A. super arg1, arg2
B. super(arg1 arg2)
C. super[arg1, arg2]
D. super{arg1, arg2}

Solution

  1. Step 1: Recall Ruby method call syntax

    In Ruby, method arguments are passed separated by commas without brackets unless parentheses are used.
  2. Step 2: Identify correct super usage with arguments

    The correct way is super arg1, arg2 or super(arg1, arg2). super arg1, arg2 uses the correct syntax without parentheses.
  3. Final Answer:

    super arg1, arg2 -> Option A
  4. Quick Check:

    Pass args to super with commas [OK]
Hint: Pass arguments to super separated by commas [OK]
Common Mistakes:
  • Using brackets [] or braces {} instead of parentheses or commas
  • Omitting commas between arguments
  • Using incorrect syntax like super(arg1 arg2)
3. What will be the output of this Ruby code?
class Parent
  def greet(name)
    "Hello, #{name} from Parent"
  end
end

class Child < Parent
  def greet(name)
    super(name) + " and Child"
  end
end

puts Child.new.greet("Alice")
medium
A. "Hello, Alice from Child"
B. "Hello, Alice from Parent and Child"
C. "Hello, Alice from Parent"
D. Error: wrong number of arguments

Solution

  1. Step 1: Understand method overriding and super call

    The Child#greet method calls super(name), which calls Parent#greet(name) and returns "Hello, Alice from Parent".
  2. Step 2: Concatenate the returned string

    The child method adds " and Child" to the parent's returned string, resulting in "Hello, Alice from Parent and Child".
  3. Final Answer:

    "Hello, Alice from Parent and Child" -> Option B
  4. Quick Check:

    super returns parent's string + child's addition [OK]
Hint: super returns parent's result; child adds more [OK]
Common Mistakes:
  • Thinking super calls child's method again causing recursion
  • Ignoring the concatenation after super call
  • Assuming super without arguments forwards none
4. Identify the error in this Ruby code snippet:
class Parent
  def info
    "Parent info"
  end
end

class Child < Parent
  def info
    super()
    puts "Child info"
  end
end

Child.new.info
medium
A. super() returns value but is ignored; only child info prints
B. Runtime error: super() called without arguments
C. Syntax error: super() should not have parentheses
D. No error; outputs both parent and child info

Solution

  1. Step 1: Analyze the super call with parentheses

    Calling super() calls the parent method with no arguments and returns "Parent info" but does not print it.
  2. Step 2: Check what is printed

    The child method prints only "Child info" because the parent's return value is ignored.
  3. Final Answer:

    super() returns value but is ignored; only child info prints -> Option A
  4. Quick Check:

    super() returns but doesn't print [OK]
Hint: super() returns value; print it to see output [OK]
Common Mistakes:
  • Assuming super() prints parent's return value automatically
  • Thinking super() without args causes error
  • Confusing syntax error with runtime behavior
5. Given this Ruby code, what will be the output?
class A
  def message
    "A"
  end
end

class B < A
  def message
    "B" + super
  end
end

class C < B
  def message
    super + "C"
  end
end

puts C.new.message
hard
A. "ABC"
B. "BCA"
C. "BAC"
D. "CAB"

Solution

  1. Step 1: Trace method calls from C to A

    C#message calls super which is B#message. B#message returns "B" + super which calls A#message returning "A".
  2. Step 2: Combine returned strings step-by-step

    B#message returns "B" + "A" = "BA". Then C#message returns "BA" + "C" = "BAC".
  3. Final Answer:

    "BAC" -> Option C
  4. Quick Check:

    super chains add strings in order [OK]
Hint: super calls chain up; combine results in call order [OK]
Common Mistakes:
  • Mixing order of concatenation
  • Assuming super calls only immediate parent
  • Ignoring that super returns parent's result
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard