tags:

views:

259

answers:

4

Matz, who invented Ruby, said that he designed the language to be more object-oriented than Python. How is Ruby more object-oriented than Python?

+3  A: 
Anurag
*A number of Python functions are procedural*: I assume this means functions such as `len` or `<`; those are implemented as methods (`__len__` and `__lt__` respectively).
Beau Martínez
+6  A: 

One example that's commonly given is len, which in Python is a built-in function. You may implement a special __len__ method in your objects which will be called by len, but len is still a function. In Ruby, objects just have the .length property/method so it appears more object oriented when you say obj.length rather than len(obj) although deep under the hood pretty much the same thing happens.

That said, over the years Python have moved towards more object-orientation. Currently all objects (and implicitly user-defined objects) inherit from the object class. Meta-classes have also been added, and many of the built-in and core library classes have been organized into hierarchies with the help of ABCs (Abstract Base Classes).

In my heavy usage of Python I have never found it lacking in the OO department. It can do everything I want it to do with objects. True, Ruby feels somewhat more purely OO, but at least in my experience this hasn't been a really practical concern.

Eli Bendersky
I find the 'len' example misleading. In that sense operator overloading is not object oriented either, because `a + b` does not look like a method call. The important thing is that it gets implemented as a method.
nikow
@nikow: the point is that `len` is an actual function. Operator overloading is just syntactic sugar - the compiler actually generates a method call. This is not the case with Python's `len`
Eli Bendersky
@Eli: As you wrote yourself `len` generally calls the `__len__` method. Having `len` as a function is syntactic sugar, just like operator overloading.
nikow
@nikow: I still insist it's subtly different. If you disassemble some Python code that uses `len` you'll see an *actual call* to a function. True, this function will find `__len__` (after a couple of levels of indirection, by the way).
Eli Bendersky
+7  A: 

If you take the Python from 1993 and compare it with Ruby then the later is more object oriented. However, after the overhaul in Python 2.2 this is no longer true. I would say that modern Python is as object oriented as it gets.

nikow
+1. This is very important. Matz announced Ruby on February, 23, 1993. His survey of existing languages that led him to start working on Ruby would obviosuly have been even before that. So, you have to take historical context into account, you can't just compare with Python 3.2, you need to look at whatever the widely available version was in 1992/93. Also, you have to take "cultural" context into account: matz is very familiar with Smalltalk. Anybody who has ever used Smalltalk usually has much stronger views on OO than people who haven't. E.g. Alan Kay says that all state must be private.
Jörg W Mittag
A: 

It's simple, nearly everything in Ruby (including numbers) is an object; there are no scalar values.

agentbanks217
...As is the case in Python.
Beau Martínez
@Beau, AFAIK, in python, you cannot inject your functions into the built-in classes (Integer,String)...so by that respect, Ruby is more OOP.
st0le
@st0le: I don't think this makes Ruby _more_ object oriented. Just because Ruby can re-open all classes to add more methods is not (IMHO) a measure of OO-ness. Java cannot "add methods to classes". [Can't believe I ever used Java as an exemplar!]. Being able to Monkey-Patch/Duck-Punch is more about dynamism than OO-ness.
Matthew Schinckel
Ruby is not more OOP because it can monkey patch classes (I agree it's more about dynamism), but because it was designed from the ground up to be a pure OOP language; everything is an object.
agentbanks217
@agentbanks217: Same for modern Python, everything is an object.
nikow