DEV Community

Discussion on: Explain the 'super' keyword in ruby like I'm five

Collapse
 
johand profile image
Johan • Edited

With super you can call a method on the parent class with the same name in the child class, for example:

class Foo
  def my_method
    'Hello there'
  end
end

class Bar < Foo
  def my_method
    super
  end
end

b = Bar.new
b.my_method

# => Hello there
Enter fullscreen mode Exit fullscreen mode

Ruby search a method #my_method in the ancestor chain, if the method not exist it will show an error NoMethodError

`my_method': super: no superclass method `my_method' for #<Bar:0x0000564740b32170> (NoMethodError)
Enter fullscreen mode Exit fullscreen mode