Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Learning Ruby

As of today, I officially 'know' Ruby. 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. I am learning this language right now. I have written a couple of programs in Ruby - so now I know ruby - at least the basics of it.

I have heard about ruby for a long time now - but till now I did not have the motivation to learn it. But then came the 'Ruby on Rails' framework for web development. And since I am in web development field, this is almost impossible to ignore. As a result here I am, trying to learn 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"
end
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.

something.new

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...

#Ruby
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...

//JavaScript
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 = File.new("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.

Another problem is that numbers and strings are not interchangeable. This one still gives me hell.

# Perl code
my $strnum = "Forty Two";
my $num = 42;
print "In perl, " . $strnum . " is " . $num . ".";

The numbers will be concatenated to the string to be printed. No problems here. But when we come to ruby...

strnum = "Forty Two"
num = 42
print "In Ruby, " + strnum + " is " + num.to_s + "."

NOTE: In Ruby the concatenation operator is '+' and not '.'.

See the code num.to_s? That means that the 'num' variable is to be converted to a string type before printing. If we try the normal route, we will get an error - like this...

#Code...
print "In Ruby, " + strnum + " is " + num + "."

#Error...
Temp.rb:3:in `+': cannot convert Fixnum into String (TypeError)

I still can't come to terms with this issue. I understand it, but can't bring myself to appending a '.to_s' at the end of every integer. I try to get over the problem with this code...

print "In Ruby, ",strnum," is ",num,"."

Give me some more time and I will append '.to_s' at the end - but presently, I just can't do it.

Ruby Resources

Ruby

Tutorials

Filed Under...

0 Comments: