To override a property on a subclass in Ruby, you can define a new property of the same name in the subclass, and then override the getter and/or setter methods to provide the desired behavior. Within the overridden getter or setter method, you can call super
to access the implementation of that method in the superclass.
Here's an example:
main.rb308 chars25 lines
In this example, we override the speak
method in the Cat
subclass to call super
to access the implementation of speak
in the Animal
superclass, and then add additional behavior to print the cat's name and the word "meow".
gistlibby LogSnag