What is the difference between calling "super" and calling "super()"
What is the difference between calling "super" and calling "super()"
In Ruby, the difference between calling super
and super()
pertains to how arguments are passed to the method in the superclass.
super
When you use super
without parentheses and without specifying any arguments, Ruby automatically forwards the arguments that were passed to the method from which super
is called. This means that super
implicitly passes all the received arguments to the method in the superclass that has the same name as the method from which it is called. This behavior is useful when you want the method in the superclass to receive exactly the same arguments as the method in the subclass[1][2][3][4][5][6].
super()
On the other hand, when you use super()
with parentheses and do not include any arguments inside the parentheses, it calls the method in the superclass with the same name but does not pass any arguments. This is useful when the superclass method does not require any arguments or when you explicitly want to ensure no arguments are passed. Using super()
and specifying no arguments can prevent unintended data from being passed to the superclass method, which might be crucial especially if the superclass method has different expectations for input or operates differ...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào