I'm using devise for user management. When I was playing with the controller, there was a method with only super
in the method, so I was wondering and examined it.
rails g devise:controllers user
Is generating a controller.
user/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
(Omitted)
def update
super
end
(Omitted)
end
Apparently, it inherits from the Devise :: RegistrationsController
class.
There is a super
in some methods.
All in all, super is a method that can call the inherited method .
Taking the above controller as an example,
The update method is also described in Devise :: RegistrationsController
,
This means that you can do the same with Users :: RegistrationsController
.
The following are the explanations I often see during my research.
class Tennis
def ball
puts "Tennisball"
end
end
class Sports < Tennis
def ball
super
puts "balls"
end
end
sports = Sports.new
sports.ball
#=>Tennisball
sports = Sports.new
sports.ball
#=>balls
If I made it myself as an analogy, it would be meaningless code ... The point is that even if you create a method with the same name after inheritance, you can use super to call the process before overriding.
Recommended Posts