super lets a subclass call the parent version of a method.

super Calling `super` reuses parent-class setup or behavior before adding subclass details.

Super Calls

tier
super_calls.rb
Replay: real traced execution (multi-file project)
class Account
  def initialize(owner)
    @owner = owner
  end

  def label
    "account: #{@owner}"
  end
end

class PremiumAccount < Account
  def initialize(owner, tier)
    super(owner)
    @tier = tier
  end

  def label
    "#{super} #{@tier}"
  end
end

tier = "gold"
account = PremiumAccount.new("Ada", tier)

puts account.label
class Account
  def initialize(owner)
    @owner = owner
  end

  def label
    "account: #{@owner}"
  end
end

class PremiumAccount < Account
  def initialize(owner, tier)
    super(owner)
    @tier = tier
  end

  def label
    "#{super} #{@tier}"
  end
end

tier = "silver"
account = PremiumAccount.new("Ada", tier)

puts account.label
class Account
  def initialize(owner)
    @owner = owner
  end

  def label
    "account: #{@owner}"
  end
end

class PremiumAccount < Account
  def initialize(owner, tier)
    super(owner)
    @tier = tier
  end

  def label
    "#{super} #{@tier}"
  end
end

tier = "platinum"
account = PremiumAccount.new("Ada", tier)

puts account.label
  1. tier ← gold

    22tier→ gold = "gold"  #@tier="silver", "platinum"23account = PremiumAccount.new("Ada", tiergold)
  2. def initialize(owner, tier)

    11class PremiumAccount < Account12  def initialize(ownerAda, tiergold)13    super(owner)14    @tier = tier
  3. @owner ← Ada

    1class Account2  def initialize(ownerAda)3    @owner→ Ada = ownerAda4  end
  4. @tier ← gold

    13  super(owner)14  @tier→ gold = tiergold15end
  5. account ← ⟨PremiumAccount A @owner="Ada", @tier="gold"⟩

    22tier = "gold"  #@tier="silver", "platinum"23account→ ⟨PremiumAccount A @owner="Ada", @tier="gold"⟩ = PremiumAccount.new("Ada", tiergold)2425puts account.labelaccount: Ada gold
  6. def label

    6def label7  "account: #{@ownerAda}"8end
  7. puts account.label

    25puts account.labelaccount: Ada gold
    outputaccount: Ada gold
  1. tier ← silver

    22tier→ silver = "silver"23account = PremiumAccount.new("Ada", tiersilver)
  2. def initialize(owner, tier)

    11class PremiumAccount < Account12  def initialize(ownerAda, tiersilver)13    super(owner)14    @tier = tier
  3. @owner ← Ada

    1class Account2  def initialize(ownerAda)3    @owner→ Ada = ownerAda4  end
  4. @tier ← silver

    13  super(owner)14  @tier→ silver = tiersilver15end
  5. account ← ⟨PremiumAccount A @owner="Ada", @tier="silver"⟩

    22tier = "silver"23account→ ⟨PremiumAccount A @owner="Ada", @tier="silver"⟩ = PremiumAccount.new("Ada", tiersilver)2425puts account.labelaccount: Ada silver
  6. def label

    6def label7  "account: #{@ownerAda}"8end
  7. puts account.label

    25puts account.labelaccount: Ada silver
    outputaccount: Ada silver
  1. tier ← platinum

    22tier→ platinum = "platinum"23account = PremiumAccount.new("Ada", tierplatinum)
  2. def initialize(owner, tier)

    11class PremiumAccount < Account12  def initialize(ownerAda, tierplatinum)13    super(owner)14    @tier = tier
  3. @owner ← Ada

    1class Account2  def initialize(ownerAda)3    @owner→ Ada = ownerAda4  end
  4. @tier ← platinum

    13  super(owner)14  @tier→ platinum = tierplatinum15end
  5. account ← ⟨PremiumAccount A @owner="Ada", @tier="platinum"⟩

    22tier = "platinum"23account→ ⟨PremiumAccount A @owner="Ada", @tier="platinum"⟩ = PremiumAccount.new("Ada", tierplatinum)2425puts account.labelaccount: Ada platinum
  6. def label

    6def label7  "account: #{@ownerAda}"8end
  7. puts account.label

    25puts account.labelaccount: Ada platinum
    outputaccount: Ada platinum