Thursday, 16 May 2013

A little python vs ruby

Just a little comparison of python vs ruby which nicely sums up why I prefer ruby to python.

Coming from a perl background, parsing strings and manipulating data structures is the bread and butter of sysadmin scripting. Here we're going to split a string, add to the array, then join into a string again.

First, in python:

a = "a b c".split()
>>> a
['a', 'b', 'c']
>>> len(a)
>>> a.append("d")
>>> ",".join(a)

Ok, fair enough I guess. Now lets look at ruby:

a = "a b c".split
=> ["a", "b", "c"] 
=> 3 
a.push "d" 
=> ["a", "b", "c", "d"] 
a.join "," 
=> "a,b,c,d" 

Notice the difference? I'll try to summarise it in text.

For python, we split the string to get an array. We then run a global method to find the size of the array. We then call a method on array to append the new element. Finally we run a join method on a string and pass it the array.

For ruby, we split the string to get an array. We then run a method on the array to get the size (.length would also have worked). We then run a method on the array to add the element. We then run a method on the array to join to get our final string.

To me ruby is far more consistent. I'm not saying python is a bad language, I just find ruby to be particularly enjoyable to use.

Criticisms, suggestions and improvements welcome :)


  1. Your use case specifies sysadmin scripting. Python isn't really made for that. It's made for object-oriented development, and that philosophy explains the quirks you've highlighted.

    You may use object.__len__() instead of len(object) if you want, but it's not recommended.

    `join` is a method of the string object because it works on any iterable. This frees all the iterable classes from implementing their own `join` method.

    Both of your concerns are addressed here:

    and here

  2. Hi Scott,

    I totally agree with you. I dived into Python for a couple of projects, but since I discovered Ruby, there's no looking back :)

    I can be a huge times more productive in Ruby as a sysadmin or a dev because I can express my thoughts in it much easier. For me, no matter how much a language can do under the hood. The language is an interface for humans, this is their primary goal and therefore it should be easy - like easy for humans.

    I love Ruby because this is the language that's that closest to this requirement. I can write the code just as it is in my head. There's no need for an unnecessary conversion.

    Especially since Python 3.x, just look at what they did to the print command :) You have to use parenthesis and why? To make the job of the machine easier. It should be opposite! Time is precious for us, let the interpreter do the more work.. ;)

    I love Ruby for things like these:

    * choose 5 random number from an array:


    * sort the words of the sentence by their sizes:

    "this is a sample text to read for everybody"{|x|[x.size,x]}{|x|x[1]}

    I suggest people to write these down in Python and then they can see what Matz tried to do with Ruby.. :)

    1. Thanks for your thoughts. Your point about programming being an interface for humans certain sums up ruby :) It is just such a joy to code in.

      Don't get me wrong, I still use python for certain tasks. It has some excellent libraries etc that are hard to find elsewhere and most linux systems have python installed so for many things it is good choice. But it isn't as fun.