Home » Technology » Why Ruby is cool – take 2

Subscribe to receive PuneTech updates updates in your email inbox or via RSS. And, if you are looking for special interest groups, Click here. See our About Page to find out more about what PuneTech is.

The best way to stay in touch with PuneTech and associated activities is to follow us on twitter

Why Ruby is cool – take 2

Official Ruby logo
Image via Wikipedia

Earlier today, we had an article by Nick Adams on why you need to learn Ruby and Ruby-on-Rails. That post attracted a detailed comment by  Ravindra Jaju which elaborated on some of the themes touched upon in the original article. Since it brings out nicely the power of Ruby, we decided to post the comment as a new article to ensure it reaches all the readers who don’t necessarily read the comments. (By the way, you should subscribe to PuneTech comments by email or via RSS.)

Those interested in Ruby should check out Ruby Fun Day in Pune on Saturday, 21st February. If you want to learn Ruby, take a look at RubyLearning.org, a site that provides free, online courses for Ruby, by Pune’s own Satish Talim. Also check out the Pune Ruby mailing list, which is reasonably active.

This “article” only makes sense in the context of the original article, so ensure that you’ve read it before you read this:

Depending on where you come from, the syntax 5.times… may appear either beautiful or plain weird. But whatever the initial impression, one starts appreciating it as the power unfolds. The syntax ‘5.times’ might look like something completely out of the blue at first glance, but once one recognizes that 5 (or any number for that matter) is a first-class object, things start falling in place.

Try playing with this further using ‘irb’ (the ‘interactive ruby’ command-line tool which is a part of the ruby distribution) to explore the object ‘5? further as follows: 5.class, 5.methods, 5.methods.sort [send message ‘methods’ to 5, which returns an ‘Array’ – further send a message ‘sort’ to this returned Array object if you like to see the methods ordered]. And if you haven’t guessed this already, do note that brackets to function calls are absolutely optional if the syntax is unambiguous (so, the above is equivalent to

5.times(){puts “Hello Pune!”}

which is equivalent to

5.times(){puts(”Hello Pune!”)}

What comes within the braces ({}) after ‘5.times’ is a block – ‘5.times’ is by itself uninteresting, unless you can do something 5 times, no? Blocks are a very powerful concept, which are defined at one place but executed elsewhere. They have access to the context where they are defined (closures). Use of blocks (and associated Proc objects) can lead to very interesting, succinct code!

And again, if you are a C/C++ or a Java programmer feeling wary about jumping into something new and have that feeling of your uber-cool libraries not being accessible from Ruby (and hence a new learning curve apart from the heart-burn) – fear not! Ruby, being an interpreted language, runs in a VM of its own. And the Ruby VM developers have made sure that access to your favourite libraries is as easy as possible. The ‘official’ Ruby VM is C code, but obviously the cool thing that the JVM is, you can run Ruby inside the JVM (Java Virtual Machine) too. Many have already made these libraries accessible via Ruby. I would term the Java VM as more powerful in this regard, giving you access to native libraries apart from all the Java goodies (look up ‘ruby+ffi’ on yahoo/google search).

Then again, metaprogramming takes Ruby fun to newer heights. Rails wouldn’t have been (easily) possible had it not been for Ruby’s abilities in this regard. How would you feel if told that there are database access methods named (for example) ‘find_by_name’ or ‘find_by_name_city’ that aren’t defined anywhere but still create appropriate SQL calls (’where name = …’ or ‘where name = … and city = …’) because you can intercept ‘function undefined’ events, look at the function name and arguments, and create the right SQL on the fly? Or even before that, a class mapped to a database table ‘knows’ which table it is supposed to represent in an object form, just by looking at its *own* name? The good news is that all the cool libraries developed as part of the Rails initiative can be used in plain Ruby code without all the rails framework overhead too.

Well, enough said for a comment. You’ve got to experience it to believe it!

(PS: Not saying here that Ruby is unique – Python, for example, is cool too – but a Python-junkie would be a better marketeer for it 😉 )

Reblog this post [with Zemanta]

If you liked this post, subscribe for updates by email or via RSS.

4 thoughts on “Why Ruby is cool – take 2”

  1. On ruby and coolness, here’s a small discussion I had on twitter (another cool ruby on rails app) a month or so back :

    Me : building cool sites and using a cool language are v. different. IMHO Ruby has the hottest cool quotient at the moment.
    Response : IMHO it’s not ruby it is ‘ruby on rails’ 🙂
    Me : fair enough. But number objects, functors, closures, code blocks, nice iterators, gr8 metaprogramming, prototypes – RubyIsCool

    And on python

    imo use php 2 get job done without fuss, use java 2 make it really robust/fast, use python 2 enjoy the ride n keep code compact.

  2. Mandar Vaze says:

    Python seems to be getting a lot of attention from Mozilla camp.

    Enter “Ruby” in the Search edit box at https://developer.mozilla.org/En

    Now try searching for Python

Leave a Reply

Your email address will not be published. Required fields are marked *