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
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:
classMomPBTechniquedefcorner_cut(bread)forcornerinbreadcorner.cutcrustendreturnbreadendclassMyPBTechnique<MomPBTechnique# I like *one* corner to not be cut!defcorner_cutfirst_corner=self.bread[0]# Gets the first cornerself.bread=bread.slice(1,-1)# Sets the bread to only the last 3 cornerssuper# Calls the `corner_cut` method defined in `MomPBTechnique`returnbread.unshift(first_corner)# Places the un-cut first corner back in the breadendend
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.
classGreeterdefgreet(name=nil)ifnameputs"Hi, #{name}, nice to meet you!"elseputs"Hi, nice to meet you!"endendendGreeter.new.greet#=> Hi, nice to meet you!Greeter.new.greet'Jess'#=> Hi, Jess, nice to meet you!
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 the greet method from their parent class, Greeter:
classMarklarGreeter<Greeterdefgreetsuper'Marklar'endendMarklarGreeter.new.greet#=> Hi, Marklar, nice to meet youMarklarGreeter.new.greet'Jess'#=> ArgumentError: wrong number of arguments (given 1, expected 0)
Okay, we still need greet to take a name 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.
classMarklarGreeter<Greeterdefgreet(name=nil)# We accept a name parameter here, but we ignore it# because everyone's name is Marklarsuper'Marklar'endendMarklarGreeter.new.greet#=> Hi, Marklar, nice to meet you!MarklarGreeter.new.greet'Jess'#=> Hi, Marklar, nice to meet you!
Is it good or bad to chain method in super like in below activerecord(5.1.6) code,
# 'delivery_timings' is a db field attributeclassCountry<ApplicationRecord# expects array of times and converts to string ie. ['08:00-12:00', '14:00-16:00'] to "08:00-12:00,14:00-16:00"defdelivery_timings=(val)returnsuper(val)ifval.blank?super(val.reject(&:blank?).join(','))end# converts string value to array of times ie. "08:00-12:00,14:00-16:00" to ['08:00-12:00', '14:00-16:00']defdelivery_timingsreturnsuperifsuper.blank?super.split(',')endend
Although the code works, I'm smelling on code(may be wrong).
Martin is an enthusiastic software engineer building and operating microservices in the JVM stack using Kotlin. Currently working for Albert Heijn in the role of DevOps engineer.
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.