Ruby

Most IT managers know about the precious stone ruby and not the programming language Ruby. Its a sad state of affairs where career IT managers are so far disconnected from what is happening in the technology world. Not much you and I can do about that…so let me do some Ruby”ing” here.

The point I want to emphasize in this blog is that Ruby reduces a lot of noise in languages such as Java.

Executing the above using the Ruby interpreter gets us:

To print ‘Hello World’ to the console

Create Methods

To define a new method

Creates a new function, which we then call passing it the name value. If you want to provide default values for function parameters then

Creating Classes

We create a class User to hold some basic information. The method initialize is the constructor. You refer to class variables as @fullname. Keyword attr_accessor allows us to access the value of the member variable like e.userName. Without this keyword we would not be able to access the member variable.

Arrays/Lists

Declares a variable named ‘greetings’ with a list of strings. You do not need to specify types for your variables. Ruby figures out that based on the value you assign it. You can even change the type later by assigning a different value to varaible.

Closures

Support for closures, which are blocks of code that you can define and pass around. Look at the code sample below

The class variable @otherNames is a list of strings. Note for the java programmer. Everything in Ruby is an object. So your variable is an object that inherits various methods from base classes in Ruby. All this happens auto-magically for you in Ruby. If the variable is an array then you have this method named ‘each’ which can be used to go over each and every item in the array. In the above example we go over each element and apply a block of code to execute against that element. The |i| is a temporary variable that holds the current element.

In Conclusion

What attracts me to Ruby is that it removes a lot of the noise out of my code. I am not giving up on Java anytime soon, but its definitely worth every techie’s time to do some Ruby or any other dynamic language (try jython…though I hate the indentation approach). There is also JRuby which is a pure java implementation of the Ruby language.

Having more languages ported to run on the VM is a good thing….and of course Sun has to do that since Microsoft is doing this for their languages on top of CLI.  For the Java platform we already have Jython, JRuby and then there is Groovy.

But in the end of the day, if I were starting a brand new web project why not just develop using Ruby and Ruby On Rails. Sure we could use Groovy and Grails but for many applications tight integration with current J2EE libraries is not needed. In future we can use Jython 2.5 and Django too. I think the bigger challenge is finding enough good people to write in these new languages. Once that is achieved….we need another set to maintain them. Ruby code being so compact can sometimes be difficult to read. But then massive code bases in Java can be just as hard.