Friday, December 1, 2006

First meeting with Ruby

Few days ago i found some time in the evening to get familiar with language Ruby.

It was nice to write few demo application on this language. They looks very short-spoken, but contains, but very informative.

First setting i saw, was the following code:
googol = 10**100
as you can see, ruby supports integrated exp function. It's not very important in daily development practice but it's nice to have such functionality.

At the second feature i can highlight the type converting:
object1 =10
object.to_s # return a string that represent number, in .NET ToString() method.
object2 = "12"
object1 = object2 #not valid statement , because type of object2 is string
object1 = object2.to_i #valid, it will parse a string and return the int value.

Next feature:
member encapsulation is required in ruby, it's not possible to create a public member.
It seems like a very nice feature. Also there is a good syntax for accessors (looks similar with .NET ones). The solution for that in Java looks very ugly (setA,getA).

And the last one is iterations:

friends = {"Melissa" =>"test", "Jeff"=>"test2", "Ashley"=>"test3", "Rob"=>"test4"}
friends.each do |key,value|
print key+"\t"+value+"\n";
end

where:
friends is a Hashtable
and there is an iterator below, that print key-value to the screen.

The C# code will looks like:

Dictionary friends = Dictionary();
friends.Add("Melissa","test");
...
friends.Add("Jeff","test2");
foreach(string key in friends.Keys){
Console.WriteLine(string.Format("{0} {1}"),key,friends[key]);
}

as you can see the ruby one looks much better except string.Format.

So i am going to learn this progressive language and also, i am sure that would help me to build faster applications for our customers.

P.S.
We have a few Ruby Gurus in our company that develop ruby solutions for many time.
If you visit our site you see that half of our projects was done using Ruby and RoR

1 comment:

Vladimir Gurgov said...

Sometimes its hard to understand where Ruby code stops and your words star, Sasha ;)