When browsing the internet about ruby on rails, I see the word super
. Can someone tell what it is and what it can do?
What is `super` in Ruby?
20.4k views Asked by endyey Es At
4
There are 4 answers
0
On
when you are using inheritance if you want to call a parent class method from child class we use super
c2.1.6 :001 > class Parent
2.1.6 :002?> def test
2.1.6 :003?> puts "am in parent class"
2.1.6 :004?> end
2.1.6 :005?> end
=> :test_parent
2.1.6 :006 >
2.1.6 :007 > class Child < Parent
2.1.6 :008?> def test
2.1.6 :009?> super
2.1.6 :010?> end
2.1.6 :011?> end
=> :test_parent
2.1.6 :012 > Child.new.test
am in parent class
=> nil
2.1.6 :013 >
There are different ways we can use super(ex: super, super()).
0
On
It was used to implement super class implementation of the current method. Within the body of a method, calls to super acts just like a call to that original method. And The search for a method body starts in the superclass of the object that was found to contain the original method.
def url=(addr)
super (addr.blank? || addr.starts_with?('http')) ? addr : http://#{addr}
end
0
On
Ruby uses the super keyword to call the superclass implementation of the current method. Within the body of a method, calls to super acts just like a call to that original method. The search for a method body starts in the superclass of the object that was found to contain the original method.
def url=(addr)
super (addr.blank? || addr.starts_with?('http')) ? addr : http://#{addr}
end
super
method calls the parent class method.for example:
If it is not enough then you can study further from here