In Ruby, you can override a getter method in a subclass by simply defining a new method with the same name as the getter in the superclass. Here's an example:
main.rb385 chars26 linesIn the example above, we have a superclass Animal with a getter method name. We then define a subclass Cat and override the name method to add some additional text to the output. We also define a speak method in the Cat subclass.
When we create a new Cat instance and call name on it, we get the overridden behavior that we defined in the Cat subclass. Similarly, when we call speak on the Cat instance, we get the speak method that we defined in the Cat subclass, which overrides the speak method in the Animal superclass.
Note that if you don't define a getter method in the subclass, the getter method from the superclass will still work as expected. In the example above, if we had not overridden the name method in the Cat subclass, calling my_cat.name would have returned just "Fluffy".
gistlibby LogSnag