Python vs Ruby

I just spent the last 8 hours taking notes on Python and Ruby. Most of my notes are from the excellent tutorials at Tutorials Point.

After 25 pages of notes where I took just what was needed to be good enough to write in the two languages, I’m going to have to say that neither is really a clear winner for me.

Overview:

OOP

Ruby is all OOP which is great. Python looks likes OOP was an afterthought. Case in point:
A Ruby object:

class Point
    def initialize(x,y)
        @x, @y = x, y
    end
    def doSomething(x)
        stmts
    end
end

Python:

class Point:
    "description"
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def doSomething(self, x):
        suite

The passing of self just looks like it was such an afterthought that there wasn’t a lot of effort put into making it OO. That isn’t a bad thing, if it works, it works, but it does raise some concerns for enterprise applications.

Ruby has method scoping (public, private, protected). I didn’t see that in Python (again, I’m a newbie, I may have missed it). If you want to write good black boxed api’s I consider this a must. That being said, Ruby’s implementation is hit or miss as well.

Ruby

class Class1
    def method1
    end
    private  # all methods from here down are private
    def privMethod1
    end
end

or

class Class1
    def method1
    end
    def privMethod1
    end
    private :privMethod1
end

Not the prettiest way (maybe defPrivate would have been better) but it does work and the ‘all private from here down’ is a nice shortcut.

Variable Manipulation

Both are pretty equal to be honest. Lets look at some basic String manipulation:

Ruby

str = 'HelloWorld'
puts str[1..3]    # 'ell'

Python

str = 'HelloWorld'
print str[1:3]    # 'ell'

Now, that’s a basic example but it shows that the designers of the language has the same things in mind, just a slightly different way of doing it.

Statements

Both have for, while, etc. Ruby goes a bit further and give you unless and until. unless and until have their place but the existence of them does not mean a win for me. You can do the same things with more standard loop statements.

They differ slightly in for loop execution but nothing too bad:

Ruby

for i in arr
    puts "Value: #{i}"
end

Python

for x in arr
    print "Value: ", x

The above example does show one nice thing about Ruby. Having the variables available for inserting into strings via #{} is very nice. I’m not sure about performance though as Python is probably more of a concatenation while Ruby is search/replace. I don’t think any developer would really notice the difference though unless you are processing very large amounts of data.

Conclusion

Let me first state again that I am a newbie to both of these languages. Also let me state that I really just touched the tip of the iceberg. There are many subtle differences between the two languages and as most developers would tell you, it’s not always the language, but the libraries that make the difference.

I think each has it’s place in the world. Ruby (especially with Rails) seems to have the libraries for web development. Python appears to have a good bit of libraries for more system oriented functions. From a language point of view though, its a tough call.

Me personally? I would use Python as a replacement for shell scripts or PERL because it just seems more suited for quick hacks and file manipulation. I would use Ruby for large applications and web development because of its more strict OO and many web libraries.

“But that’s just my opinion, I could be wrong” – Dennis Miller

About sseaman

Connect with me on Google+
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *