Bird
Raised Fist0
Rubyprogramming~10 mins

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

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

Practice

(1/5)
1. What does the super keyword do in Ruby?
easy
A. Creates a new instance of the parent class
B. Overrides the parent class's method permanently
C. Calls the parent class's method with the same name
D. Deletes the parent class's method

Solution

  1. Step 1: Understand the role of super

    The super keyword is used inside a child class method to call the method with the same name from its parent class. It does not create, override, or delete methods but simply calls the parent method to reuse or extend its behavior.
  2. Final Answer:

    Calls the parent class's method with the same name -> Option C
  3. Quick Check:

    super calls parent method [OK]
Hint: Remember: super calls parent method with same name [OK]
Common Mistakes:
  • Thinking super creates or deletes methods
  • Confusing super with instance creation
  • Assuming super overrides methods permanently
2. Which of the following is the correct syntax to call the parent method with no arguments using super?
easy
A. super()
B. super[]
C. super
D. super{}

Solution

  1. Step 1: Understand super with parentheses

    Using super() calls the parent method explicitly with no arguments, ignoring any current method arguments. Only super() uses parentheses correctly to pass no arguments; other forms are invalid syntax.
  2. Final Answer:

    super() -> Option A
  3. Quick Check:

    super() means no arguments [OK]
Hint: Use parentheses to call parent with no args: super() [OK]
Common Mistakes:
  • Using super without parentheses to mean no arguments
  • Using brackets or braces instead of parentheses
  • Confusing super() with super without parentheses
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 + " and Child"
  end
end

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

Solution

  1. Step 1: Trace the method call in Child#greet

    The Child's greet method calls super without parentheses, so it passes the argument "Alice" to Parent#greet. Parent#greet returns "Hello, Alice from Parent". Child appends " and Child" to this string.
  2. Final Answer:

    Hello, Alice from Parent and Child -> Option A
  3. Quick Check:

    super passes args, output combines strings [OK]
Hint: super without () passes all args automatically [OK]
Common Mistakes:
  • Assuming super passes no arguments by default
  • Expecting an error due to argument mismatch
  • Ignoring the appended string in Child method
4. Identify the error in this Ruby code:
class Parent
  def initialize(age)
    @age = age
  end
end

class Child < Parent
  def initialize
    super
    @name = "Child"
  end
end

Child.new
medium
A. NameError for @name variable
B. No error, code runs fine
C. SyntaxError due to missing parentheses
D. ArgumentError: wrong number of arguments for super

Solution

  1. Step 1: Check Parent initialize method

    Parent's initialize requires one argument age. Child's initialize calls super without arguments, so it tries to call Parent's initialize with zero arguments, causing an ArgumentError.
  2. Final Answer:

    ArgumentError: wrong number of arguments for super -> Option D
  3. Quick Check:

    super must match parent args or use super() [OK]
Hint: super without args passes current args; mismatch causes error [OK]
Common Mistakes:
  • Assuming super() passes parent's required args automatically
  • Ignoring that Parent requires an argument
  • Thinking missing parentheses cause syntax error here
5. Given this code, what will Child.new.greet output?
class Parent
  def greet(name = "Guest")
    "Hello, #{name} from Parent"
  end
end

class Child < Parent
  def greet(name = nil)
    if name
      super(name.upcase)
    else
      super()
    end
  end
end

puts Child.new.greet
puts Child.new.greet("alice")
hard
A. Hello, from Parent Hello, alice from Parent
B. Hello, Guest from Parent Hello, ALICE from Parent
C. Hello, Guest from Parent Hello, alice from Parent
D. Error due to wrong arguments in super calls

Solution

  1. Step 1: Analyze Child#greet with no argument

    When called without argument, name is nil, so super() calls Parent#greet with no arguments, using default "Guest". When called with "alice", name.upcase is "ALICE" passed to super(name.upcase), so Parent#greet returns "Hello, ALICE from Parent".
  2. Final Answer:

    Hello, Guest from Parent Hello, ALICE from Parent -> Option B
  3. Quick Check:

    super() calls parent with defaults; super(args) passes modified args [OK]
Hint: Use super() for no args, super(args) to pass specific args [OK]
Common Mistakes:
  • Assuming super() passes current args
  • Ignoring default parameter in Parent#greet
  • Not uppercasing the argument before passing
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