I've used super
to initialize parent class but I cannot see any way of calling parent class from subclass methods.
I know PHP and other languages do have this feature but cannot find a good way to do this in Ruby.
What would one do in this situation?
Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).
If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method.
If the method is the same name, i.e. you're overriding a method you can simply use super
. Otherwise you can use an alias_method
or a binding.
class Parent def method end end class Child < Parent alias_method :parent_method, :method def method super end def other_method parent_method #OR Parent.instance_method(:method).bind(self).call end end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With