If you are not familiar with Ruby, it is a programming language that claims to be as easy to use as Perl and as Object Oriented as Python. It is perhabs the hottest language on the net right now. OK, maybe that title goes to javascript - but the second hottest title belongs to ruby.

I find learning ruby very easy. I already know many other languages and am proficient in three other main scripting languages(Perl,PHP and Tcl). So I got hang of the language rather easily. All you have to do is connect something in this language to something in a language you already know. For example, the if loop in ruby is like this...

if a == 5 then
    print "A is Five"
The same for perl is...
if ($a == 5) {
    print "A is Five";

After this connection is made, there is no more problems. All you have to do is make the connection for every thing in the language. This is very easy to do - just read some ruby scripts and within minutes you will get the hang of it.

The problem comes where there is something new in the language - something that is not there in the language what we already know. For example, in ruby every thing is an object - ruby has no primitive types. So a string is an object as is an integer. As a result, the perl code will not connect with the ruby code as shown by the following example.

#Perl - Finding the length of a string.
my $hello = "Hello World";
my $length = length($hello);
print "The number of '$hello' is : $length";

And the ruby code for the same is...

hello = "Hello World"
print "The number of chars in ",hello," is ",hello.length

The perl code calls a function with the string as the argument and it returns the number of chars in the string. But in ruby, the length is a property of the string variable. I solved this problem by making the connection with javascript. The example is...

var hello = "Hello World"
alert("The number of chars in "+hello+" is "+hello.length)

In javascript(as is in java), the connection is made perfectly. The hello.replace(/l/g,"L") in javascript becomes hello.gsub(/l/g,"L") in ruby. Everything is an object - yeah, I think I got that.

The next problem arises when there is a feature in ruby that is not in any language you know. For example, we will print every element of an array.

#Ruby code...
an_array = ["Julius Ceaser","Alexander","Binny"]
an_array.each { |name|
	print "This Item : " + name + "\n"

Yes, I know that the foreach loop in perl can do it - but this is different. A method of the object not only returns the values but it also acts like a foreach loop. If I knew the Smalltalk language, this would not be a problem as this syntax is taken from that language - but I don't know that language. I had to struggle with that concept for a few minutes - but after that there was no problems. Many other stuff in ruby uses the same principle. For example, the code for reading the data from a file is given below.

#Open File for reading
aFile ="FileName.txt", "r")

#Process each line
aFile.each_line { |line|
	print line

Here, a line from the file is printed per iteration of the 'each_line' loop. There are many other examples, but after you get the concept of one, the rest will be easy to understand.

Ruby Resources



Subscribe to Feed