For further actions, you may consider blocking this person and/or reporting abuse
Read next
A Comprehensive Guide to Multithreading in Ruby
Davide Santangelo -
Rails Designer v1.8: Ready for Rails 8 🫶
Rails Designer -
Minitest Advantages: Simple Testing for Rails Projects
JetThoughts Dev -
Ruby on Rails Case Study: How JetThoughts Improved OpenApply's Code and UX
JetThoughts Dev -
Top comments (8)
So lets say your mom makes super smacking pb &j's using her super smacking pb & j technique (all she does is remove the crust). But today mom had to hurry to work so brother B is making breakfast. Your not sure if b can make pb & j's like mom but he assures you that he's used ( super ) to get moms skill passed down to him so he's qualified. Using super B made you some super smacking pb & j's
class Mom-PB-Technique
def corner-cut(bread)
for corner in bread
corner.cut crust
return bread
end
class B-PB-Technique < Mom-PB-Technique
def corner-cut
super
end
Doesn't doing
B-PB-Technique < Mom-PB-Tecnhnique
already give you all of mom's methods?Yes, it does.
super
is most useful when over-riding the behavior from a method provided in the ancestor chain. I would revise the example to the following:This more clearly demonstrates how super can be used to re-use-but-still-change behavior in the class hierarchy.
When you inherit from a parent class, sometimes you want the child's method to behave slightly differently but still use the parent method's behavior.
Let's say you have a Greeter, and their job is to say hi to everyone they meet. If they have the person's name, they'll greet them by name.
Now suppose we hire a new greeter from the planet Marklar, who has a religious belief that everyone's name is Marklar. When their
greet
method calls super, it calls thegreet
method from their parent class,Greeter
:Okay, we still need
greet
to take aname
parameter, so that we can use a MarklarGreeter anywhere we use a Greeter. This is called the Liskov Substitution Principle. I can't find a link that explains that like you're five, but it basically says an instance of a child class should be usable anywhere you can use an instance of the parent class.More info about Ruby's super here.
With super you can call a method on the parent class with the same name in the child class, for example:
Ruby search a method #my_method in the ancestor chain, if the method not exist it will show an error NoMethodError
IMHO, super is alias of father or mother.
Is it good or bad to chain method in super like in below activerecord(5.1.6) code,
Although the code works, I'm smelling on code(may be wrong).
super
is your parent.