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.

Advertisement

9 comments on “Thoughts on Ruby – I think it’s great…

  1. Pingback: Needing an IDE should be considered a language smell « Technikhil Writing

  2. I would need to read the Why’s book as well to really appreciate the post in it’s true intent as portrayed.

    My biggest hate against any programming language is when there are “n” no. of options given to you (in the first instance) and then experts spring up and outweigh one over the other (leave apart the usage scenarios). For this very reason I wouldn’t mind throwing stones at my present bread-winning platform. They have been the worst so far!

    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” – If there’s one, looking forward to read this dude!

    • Shine,
      Definitely read Why’s book but a caveat – you will need to persevere through the sometimes weird stuff with the cartoons. There is another book that is very good without the cartoons called Eloquent Ruby by a guy called Russ Olsen. Haven’t finished it yet – its a 400 page book – but it looks promising :-)
      My view on the “many ways to do it” problem is – well – that I embrace it. After all, the fact that there are indeed many ways to approach a problem is one of the reasons why programming is such an
      intensely creative activity and also why it’s so hard to do. I think the fact that there are numerous ways to do something does not take away from the principle that programmers should look for the most expressive and succinct way to express their solution to a problem.
      Whether you are writing a DSL or state machine – the intent of each statement should be clear and easily readable – experts may extoll the virtues of competing solutions but ultimately the one you should be fighting for is your client. Doing the simplest thing that will meet the customers requirement and writing your code in the simplest and clearest way possible – no more no less should be your goal…

  3. Oh forgot one thing. Surely you know the very interesting, diametric opposite of Why The Lucky Stiff – Mr Zed Shaw. He wrote the “Learn Python the hard way” book, which is excellent for beginners/intermediate people. He also wrote Mongrel/Mongrel2 and is a core contributor to Zero MQ.

  4. I totally get the “work with the language” part. Last year I decided to take up Scala, and initially things were moving at a glacial pace. But once i decided to use it for prototyping and for quick scripting, and finally, to finally write a proper program for work, things started moving fast.

    Scala, appears to have a similar objective – to be terse and clear. At its worst, its a syntactically better java, at its best, it becomes something that actually reduces both physical and mental efforts for a programmer.

    I chose scala because it was a JVM language and Java was what i was working with. (J)Ruby was the possible next choice though :)

    • If you like Scala – you should definitely check out Ruby – it as a larger cross platform base (like Python) and a native reference implementation to fall back on…

    • You are welcome Sumit – glad I could help… Definitely look at Why’s book – it’s definitely part of Ruby lore and a great guide to the Ruby way…

What are you thinking right now?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s