0
0
Rubyprogramming~10 mins

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

Choose your learning style9 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.