Thoughts on Ruby – I think it’s great…

So it’s the middle of the year and like last year I am in the middle of a new language. This time however there are some differences from last year.

The biggest one is that this year I am actually working on the language – Ruby – while I am learning it and I think this makes a big difference. Though I know that one can learn a language using sample exercises and Code Katas – somehow I think the experience of working with a language everyday – gives one a real appreciation of what it is like to live with a language. When you are learning a language by choice – in your own time – you get to choose the problems and the Katas. When you have to work with the language and you use it daily on problems thrust upon you by seemingly unreasonable and sometime crazy requirements – you get a better understanding of the warts and wrinkles of the language along with the pretty bits.

I actually had a cursory look at Ruby sometime back, I had downloaded the language and looked at a tutorial on the web. I solved a couple of basic Project Euler questions with it. I remember thinking at the time – “Hmmmm, not really all that different from Python…” and deciding that learning it was not going to get me much. I was looking to exploring languages like F# and Erlang at the time, since they were based on different ways of solving programming problems.

So, when I started on this Ruby project this year, I came with certain preconceptions – but then something happened that changed how I approached Ruby completely. I read the online book – Why’s (Poignant) Guide to Ruby and I realized that the way I had coded Ruby thus far was not the way Ruby was intended to be coded. Before I go a little further I think I will digress a little give my impression of Why’s (Poignant) Guide to Ruby

This is not the first time I had heard of Why and his book.

_why's self-portrait from Why's (poignant) Gui...

Image via Wikipedia

Indeed one would be hard pressed indeed to spend any time in the Ruby community and not have heard about Why the lucky stiff, his book and his sudden disappearance from the internet (another digression – I really am amazed at the fact this person was able to keep his virtual life so compartmentalized from his non-virtual one that he was able to disappear so completely!). My first impression after reading the first few pages was – Wow!! This guy must have a good collection of hallucinogens and Pink Floyd albums – to create those weird cartoons…

Why's foxes

Image via Wikipedia

But, happily I persevered and was rewarded with a real appreciation of the beauty of Ruby. This book is not only a guide to the Ruby language – it is a guide to the Ruby philosophy – The Ruby Way. I would definitely recommend this book to all developers that are looking to learn Ruby, especially if you are coming from a background of working on the “big iron” languages like C# – this book will provide that mental shift in the way you code, that will let you really appreciate Ruby.

Well, now that the digression is out of the way, lets come back to the question coding style in Ruby. Rubyists tend to favor terse code that conveys its intent clearly and concisely. The Ruby culture uses idiomatic syntax and encourage succinctness in code. I really came to appreciate this when I saw the Ruby approach to a basic programming structure – the loop.

The canonical implementation of a for loop is in the form –

for <counter> <condition>
<begin block>
	<do something>
<end block>

In C it is –

for(i = 0; i < 10; i++)
{

}

In VB it is –

for i = 0 to 10

next

or in the case of iterating through an array

for each <item> in <group>
  <do something>
next

In Ruby the for loop can be expressed in a variety of ways –
The vanilla for loop is available –

for i in 0..10

end 

for  in 

end

But there is also –

5.times { <do something> }

1.upto(10) (<do something)

<group>.each {<do something>}

<group>.map
  <do something>
end .
.
.

Now it can be argued that functions like map are available in other mainstream programming languages as well – but the point I’m trying to make is – in the Ruby way of programming they become the usual way of doing things.

Ruby is a language designed for developers – but in order to fully appreciate and utilize this one must write (and read) Ruby the way Ruby is meant to be written. Once you embrace “The Ruby Way” you will see in Ruby a language that stays out of the way of your thinking. Instead of grappling with or worrying about syntax, you spend more of you time working on your solution. You can see the syntax is very easy to read. In fact the expressiveness of Ruby makes it easy to get to the intent of the code without getting hung up on the syntax. Reading code after you left it for awhile becomes a snap and you find yourself getting back to where you left of more easily.

I had written a post earlier regarding “the coding zone” – Ruby gets me into the coding zone more easily and lets me sustain it for longer than with any other language I know. It is liberating to be able to conceive of and code entire features in the time it took me to look up the idiosyncrasies of the syntax and battle the compiler of other languages. I find coding in Ruby to be a pleasant experience, not the least because of the incredible community around it. The Ruby community in a very real sense is one of the strengths of the language that put it ahead of Python in my book.

So (as you can probably tell from this long post) – I am really enjoying my latest foray into Ruby. That’s not to say Ruby is perfect – even if you ignore its real performance disadvantage – Ruby has some things that can definitely be improved – but with its great community I think it has a real chance of moving forward and fixing these things.

Advertisements