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.
Ruby has the keyword "super"is used to call the 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 replace the functionality of the parent method, but still want to call it to save some logic. Here the keyword "super" comes to the rescue.
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 # We call the parent method
  end
end

child = Child.new
child.say_hello
In this example, there are two classes: "Parent" (parent) and "Child" (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 call the "say_hello" method from the parent class.
As a result, when we instantiate the "Child" class and call the "say_hello" method, we get the following result:
Hello from the Child class!
Hello from the Parent class!
Thus, the use of "super"allows us to add functionality (extend functionality) to subclass methods while maintaining parent class logic.

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

22 May 16:26

В чому різниця <%, <%=, <%# та -%> у ERB шаблонах (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? Why are they needed?

meme code
meme code@memecode
23 May 11:16

Як працює has_many through зв'язок (many to many) у Ruby on Rails?

meme code
meme code@memecode
24 May 18:53

Що таке joins у Ruby on Rails та як це працює?

meme code
meme code@memecode
02 Jun 06:42

Error adding people to Google Family. The page could not be loaded.

meme code
meme code@memecode
04 Jun 21:19

Як клонувати GitHub репозиторій?

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 ISO images of software used?

meme code
meme code@memecode
23 Jun 12:39

Звідки завантажити офіційний ISO-файл (образ) з 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 settings? Why do you need RST?

meme code
meme code@memecode