Saturday, June 9, 2012

Discovering Ruby

As far as spripting languages are concerned, I have been a Python guy for a long time. Then my work required me to learn Ruby and it's been a strange experience. It is said that one of the primary concept in Ruby design is the principle of least astonishment. But, in my learning experience, there was no shortage for astonishments. Ruby provides a lot of shorthands to make the programming much more concise and natural language-like and, in my opinion, this also confuses the programmers coming to the Ruby from other languages. For a simple example, I can point out that, in Ruby, return keyword is optional; whatever value returned by the last statement of a method will be the method's return value.

It took me only a few hours to learn Python (still more weeks to master it.) I see Python as the best mixture of C and Java – for example, if you want to open a file in Python, it follows C's style (a simple open() statement, compared to Java's nested stream opening), but for classes like strings, lists, and dictionaries, it favors Java style. If you know C/C++ and Java, learning Python is a breeze. But, learning Ruby reminded me of learning Perl; while it was simple enough, some Ruby-specific syntax kept me puzzled for stretches of time. For example the ||= operator, which is generally used to assign default values.

One problem I had with Python was that it is space-sensitive (or indentation-sensitive.) While it makes Python source codes super-readable, it creates confusion and bugs when you copy-paste stuffs from one area to another. With Ruby, I happily forgo that problem. Ruby also has many other nice features, like blocks. In summary, once you have got used to its rough edges, Ruby will be real sparkly in your hand.

No comments: