Class Inheritance in Ruby
Ruby is an object oriented programming language and as such it supports class inheritance out of the box. In order to utilize class inheritance in Ruby, you need to create a subclass of the original class. The subclass overrides its parent methods and may add new methods as needed.
class MySubclass < String
def mixing
split(//).sort_by {rand}.join
end
def inspect
mixing.inspect
end
end
It is interesting to note though that Ruby developers in general don’t use subclasses and often times modify classes directly by adding methods to the classes defined in Ruby. Rails framework is one of such libraries that directly adds methods to core Ruby classes. There are dangerous with this approach. For instance, new method that you adding to a Class may have existing method already defined prior, so your method will simply override default method with unexpected consequences.
In general, if you think that your method will only be applied to an instance of the class, then create a subclass. If you think, you can create better version of existing method in the class, then use override method to gain efficiency.