Moving from Ruby to Python

01 Apr 2009

I've been using Ruby ever since a good friend told me to jump on the Rails bandwagon when it was back in beta, around 2005. After having only programmed production code in PHP, Ruby was like, way hard, but I had to learn it because of peer pressure.

4 years later, after struggling needlessly through coursework and projects using Ruby (where I could have been using Java instead), I'm finally ready to ditch the beast. I've lost coding competitions because of Ruby's speed. I've stared at code for seconds and not been able to figure it out. I even tried to implement data structures in Ruby, and ended up doing them in C. People make fun of me on a daily basis when I utter the famous last words: "Ruby's not that slow..."

Anyways, it's 2009 and a good time as any to switch. I've been learning Haskell on the side, but it's not production ready. In my search for the ultimate Ruby replacement language, I stumbled on one called Python that seems to have some momentum going for it. I think some big companies are even using it in production! Anyways, here are some of the pros vs Ruby:

  • Whitespace matters. Having to "end" everything in Ruby really sucks, especially since my code is always correctly aligned.
  • No ternary if. Seriously, I have to type this kind of crap in Ruby all the time:
    v = a.empty? ? a.shift : nil
    
    In Python this would be:
       if len(a) > 0:
                v = a[0]
                a = a[1:]
        else:
                v = None
    
    This is clearly less error-prone and more readable than Ruby's.
  • Better built-in methods. For example, you can simply do sum([1,2,3]) in Python, whereas you would have to do [1,2,3].inject(0) { |sum, i| sum += i } in Ruby. Seriously????? Why is it called inject anyways. Ruby's authors were clearly not well at English.
  • Faster. Because Python is so much simpler than Ruby, its VM is almost an order of magnitude faster to make up.
  • More production ready. I read somewhere that Google is using Python.
  • Better commenting. In Python you can do:
       def getTime():
            '''Returns a string timestamp in the form: 'Time hh:mm:ss' '''
                ...
    
    but in Ruby you're stuck with
     def get_time
            =begin doc
                Returns a string timestamp in the form: 'Time hh:mm:ss'
            =end
            ...
    
    Look at how much space we're saving with Python. LOC matters, people.

These are just some of the reasons that I'm moving over. Look forward to code samples and solutions in Python from me in the future!