Friday, March 04, 2005

Closures And Iterators

This is one of the best features that I like about Ruby (and believe me there are lots of them to choose from).

Here, one can pass a piece or a block of code to a method just like an arguement.

For Example :
There is a log file with log messages, messages in turn consists of comma separated fields.

Below, to analyse the logs better, I read the log file and then split each of the messages into separate tokens and then display them in a column.

IO.foreach("file") {line line.split(",").each() {token puts token.strip }

What, all this in just 1 line of Code. This is the beauty of Ruby. Java Geeks, try writing the same stuff in Java and see.

After so many years of programming in Java, i find programming in Ruby literaly liberating. I feel Ruby has given me wings to fly.

No comments: