All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What does super do in Ruby?

This content has been automatically translated from Ukrainian.
In Ruby, the keyword "super" is used to call a parent method from a subclass. When you declare a subclass, it inherits all the methods of the parent class. In some cases, you may need to override the functionality of the parent method, but still want to call it to preserve certain logic. This is where the keyword "super" comes in handy.
Here is an example that demonstrates the use of "super" in Ruby:
class Parent
  def say_hello
    puts "Hello from the Parent class!"
  end
end

class Child < Parent
  def say_hello
    puts "Hello from the Child class!"
    super # Calling the parent method
  end
end

child = Child.new
child.say_hello
In this example, there are two classes: "Parent" (the parent) and "Child" (the subclass). The "Child" class inherits the "say_hello" method from the "Parent" class. In the "say_hello" method of the "Child" class, we first output the string "Hello from the Child class!", and then call "super", which will invoke the "say_hello" method from the parent class.
As a result, when we create an instance of the "Child" class and call the "say_hello" method, we get the following output:
Hello from the Child class!
Hello from the Parent class!
Thus, using "super" allows us to add functionality (extend functionality) in the subclass methods while preserving the logic of the parent class.

This post doesn't have any additions from the author yet.

22 May 16:26

What is the difference between <%, <%=, <%# and -%> in ERB templates (Ruby on Rails)?

meme code
meme code@memecode
23 May 06:57

What is debugging?

meme code
meme code@memecode
23 May 07:41

What are attr_accessor, attr_reader, and attr_writer in Ruby? What are they used for?

meme code
meme code@memecode
23 May 11:16

How does the has_many through association (many to many) work in Ruby on Rails?

meme code
meme code@memecode
24 May 18:53

What are joins in Ruby on Rails and how does it work?

meme code
meme code@memecode
02 Jun 06:42

Error adding people to Google Family. Failed to load the page.

meme code
meme code@memecode
04 Jun 21:19

How to clone a GitHub repository?

meme code
meme code@memecode
23 Jun 12:07

What is Ubuntu? What is it used for?

meme code
meme code@memecode
23 Jun 12:35

What is an ISO image? Where are software ISO images used?

meme code
meme code@memecode
23 Jun 12:39

Where to download the official ISO file (image) of Ubuntu?

meme code
meme code@memecode
23 Jun 12:48

What is the difference between 32-bit and 64-bit processor architecture? Why is 64-bit architecture more popular now?

meme code
meme code@memecode
23 Jun 15:58

What is RST in SSD disk settings? What is RST needed for?

meme code
meme code@memecode