0
0
Rubyprogramming~10 mins

Super keyword behavior in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Super keyword behavior
Call method in subclass
Use super keyword
Call same method in superclass
Execute superclass method
Return to subclass method
Continue subclass method execution
Return final result
The super keyword calls the same method from the superclass, allowing the subclass to extend or modify behavior.
Execution Sample
Ruby
class Parent
  def greet
    "Hello from Parent"
  end
end

class Child < Parent
  def greet
    super + " and Child"
  end
end

puts Child.new.greet
This code shows how super calls the greet method from Parent inside Child's greet method.
Execution Table
StepActionMethod CalledReturn ValueNotes
1Create Child instanceinitialize (default)Child instanceObject created
2Call greet on Child instanceChild#greetCalls superStarts Child greet method
3Inside Child#greet, call superParent#greet"Hello from Parent"Calls Parent greet method
4Return from Parent#greet to Child#greetChild#greet"Hello from Parent and Child"Concatenate strings
5Print resultMain"Hello from Parent and Child"Output to console
💡 Method execution completes after returning combined string from Child#greet
Variable Tracker
VariableStartAfter Step 3After Step 4Final
resultnil"Hello from Parent""Hello from Parent and Child""Hello from Parent and Child"
Key Moments - 3 Insights
Why does calling super inside Child#greet call Parent#greet?
Because super calls the method with the same name in the superclass, as shown in execution_table step 3.
What happens if super is called without parentheses?
It still calls the superclass method with the same arguments, as shown in step 3 where no arguments are passed.
Can super be called without a method of the same name in the superclass?
No, it will cause an error because super looks for the same method in the superclass, as implied by the flow in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value of Parent#greet at step 3?
Anil
B"Hello from Parent and Child"
C"Hello from Parent"
DAn error
💡 Hint
Check the 'Return Value' column in execution_table row for step 3.
At which step does the Child#greet method concatenate the strings?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Notes' column for the step where concatenation happens.
If super was not called in Child#greet, what would be the output?
A"Hello from Parent and Child"
Bnil
C"Hello from Parent"
DAn error
💡 Hint
Without super, Child#greet returns only what it explicitly returns; check variable_tracker for result changes.
Concept Snapshot
super keyword calls the same method in the superclass.
Used inside subclass methods to extend or reuse behavior.
Syntax: super or super(args).
Returns the result of the superclass method.
If no superclass method, calling super causes error.
Full Transcript
This visual execution shows how the super keyword works in Ruby. When a method in a subclass calls super, it runs the same method from its superclass. In the example, Child#greet calls super, which runs Parent#greet and returns "Hello from Parent". Then Child#greet adds " and Child" to that string and returns the combined result. The execution table traces each step: creating the object, calling Child#greet, calling Parent#greet via super, returning and concatenating strings, and finally printing the output. The variable tracker shows how the result variable changes after calling super and after concatenation. Key moments clarify why super calls the superclass method, what happens if called without parentheses, and that calling super without a matching method causes an error. The quiz tests understanding of return values, where concatenation happens, and what happens if super is omitted. The snapshot summarizes the main points about super keyword usage in Ruby.