ZetCode

Ruby super Keyword

last modified October 18, 2026

This tutorial explains how to use Ruby's super keyword in method inheritance. The super keyword calls a method with the same name in a superclass or included module.

The super keyword invokes the next method with the same name in the ancestor chain. It's essential for method overriding while preserving existing behavior.

A bare super forwards the current method's arguments and block. Use super() to pass no arguments, or list the arguments explicitly. Any form still forwards the current block unless another block is given or &nil is passed.

Basic super Usage

This example shows the simplest use of super to extend a parent class method. The child class adds behavior while keeping the parent's.

basic_super.rb
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.

super With Arguments

When overriding methods with parameters, a bare super forwards all arguments automatically.

super_with_args.rb
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)

The child class enhances the method with logging while preserving the original calculation.

super With Explicit Arguments

Sometimes you need to modify arguments before passing them to the parent. This example shows explicit argument passing with super.

super_explicit_args.rb
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 the parent's initialize. The speak method combines parent and child behavior using super. Use super() when the parent method should receive no arguments even though the current method has some; a bare super would forward them all.

super With No Parent Method

Calling super when no parent method exists raises NoMethodError. This example shows how to handle that case.

super_no_parent.rb
class Base
  # No example method defined
end

class Derived < Base
  def example
    begin
      super
    rescue NoMethodError
      puts "Parent has no example method"
    end
    puts "Child method continues"
  end
end

Derived.new.example

Calling super with no matching parent method raises NoMethodError. The rescue clause catches it and lets the method continue. Rescuing NoMethodError can also hide unrelated errors, so prefer ensuring that the parent method exists.

super With Modules

super works with modules in the inheritance chain. This example shows method lookup through included modules.

super_with_modules.rb
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

Because Invoice includes Auditable, method lookup goes from Invoice to Auditable to Document. The super in Auditable therefore calls Document#save.

super With Block

Methods accepting blocks can use super to pass the block to the parent. This example demonstrates block forwarding.

super_with_block.rb
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 block given to super is used by the parent method. The parent yields Base value to that block, which in turn yields Enhanced Base value to the caller's block.

super in Singleton Methods

super works in singleton methods too. This advanced example shows singleton method inheritance.

super_singleton.rb
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.

Source

Ruby Classes and Modules Documentation

This tutorial covered Ruby's super keyword with examples showing method inheritance, argument handling, and advanced usage patterns.

Author

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.