last modified April 27, 2025
This tutorial explains how to use Ruby's super keyword in method
inheritance. The super keyword calls parent class methods from
child classes.
The super keyword invokes a method with the same name in the parent class. It's essential for method overriding while preserving parent behavior.
super can be used with or without arguments. It automatically
forwards arguments when called without parentheses. This makes inheritance
flexible.
This example shows the simplest use of super to extend a parent
class method. The child class adds behavior while keeping the parent's.
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
The super call executes the parent's greet method.
Then the child adds its own message. This pattern extends functionality.
When overriding methods with parameters, super can forward
arguments automatically or explicitly. This example demonstrates both approaches.
class Calculator
def add(x, y)
x + y
end
end
class ScientificCalculator < Calculator
def add(x, y)
result = super
puts "Calculation result: #{result}"
result
end
end
puts ScientificCalculator.new.add(5, 3)
super without parentheses forwards all arguments. The child class
enhances the method with logging while preserving the original calculation.
Sometimes you need to modify arguments before passing them to the parent.
This example shows explicit argument passing with super.
class Animal
def initialize(name)
@name = name
end
def speak
"#{@name} makes a sound"
end
end
class Dog < Animal
def initialize(name, breed)
super(name)
@breed = breed
end
def speak
"#{super} and barks loudly"
end
end
dog = Dog.new("Rex", "Labrador")
puts dog.speak
The child class passes only name to parent's initialize. The
speak method combines parent and child behavior using super.
Calling super when no parent method exists raises an error. This
example shows how to handle such cases safely.
class Base # No method defined end class Derived < Base def example super rescue puts "Parent has no example method" puts "Child method continues" end end Derived.new.example
The rescue clause prevents the program from crashing when super
finds no parent method. This defensive programming handles edge cases.
super works with modules in the inheritance chain. This example
shows method lookup through included modules.
module Auditable def save puts "Audit log created" super end end class Document def save puts "Document saved" end end class Invoice < Document include Auditable end Invoice.new.save
The super in Auditable calls Document's
save. Ruby's method lookup finds the next available implementation.
Methods accepting blocks can use super to pass the block to the
parent. This example demonstrates block forwarding.
class Generator
def generate
yield "Base value"
end
end
class EnhancedGenerator < Generator
def generate
super do |value|
yield "Enhanced #{value}"
end
end
end
EnhancedGenerator.new.generate { |v| puts "Received: #{v}" }
The child class modifies the block's input while preserving the parent's
generation pattern. super handles the block seamlessly.
super works in singleton methods too. This advanced example shows
singleton method inheritance.
class Person
def name
"John Doe"
end
end
person = Person.new
def person.name
"Mr. #{super}"
end
puts person.name
The singleton method calls the original instance method via super.
This pattern is useful for per-object customization.
Ruby Classes and Modules Documentation
This tutorial covered Ruby's super keyword with examples showing
method inheritance, argument handling, and advanced usage patterns.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Ruby tutorials.