Search code examples
rubyoopmultiple-inheritanceoverloading

Is Ruby a pure object oriented programming language even though it doesn't support multiple inheritance?


I know that to an extent substitutes the lack of multiple inheritance by allowing one to include multiple modules within a class.

Also, I'm not sure of all of the prerequisites of a pure OOP Language. From this article, they mention

a Ruby class can have only one method with a given name (if you define a method with the same name twice, the latter method definition prevails..

So does it mean that Ruby doesn't support overloading methods? If so, it still can qualify as a pure OOP language? What is the reason behind this as well?


Solution

  • There are several different families of object-oriented languages. If you're thinking of multiple inheritance and method overloading, you're probably coming from a C++ mindset where these things are taken for granted. These conventions came from earlier languages that C++ is heavily influenced by.

    Ruby doesn't concern itself with the type of objects, but rather the methods they're capable of responding to. This is called duck typing and is what separates Smalltalk-inspired languages like Ruby from the more formal Simula or ALGOL influenced languages like C++.

    Using modules it's possible to "mix in" methods from various sources and have a sort of multiple inheritance, but strictly speaking it's not possible for a class to have more than one immediate parent class. In practice this is generally not a big deal, as inheritance is not the only way of adding methods.

    Method overloading is largely irrelevant in Ruby because of duck-typing. In C++ you might have various methods for handling string, int or float types, but in Ruby you'd have one that calls to_f on whatever comes in and manipulates it accordingly. In this sense, Ruby methods are a lot more like C++ templates.