onsdag 4 mars 2009

Why you love and hate Ruby

What strikes me when programming Ruby is how simple everything is! You've got utility functions for almost everything, and writing an application doesn't take more then a few lines of code. No need to setup a project, no need to think about types. You just write code!

The downside of this, of course, is that the code easily becomes bad written, and as there are very few rules in Ruby, it isn't very easy reading others' code. This is why Ruby sometimes is classified as a "hard to lean language". There are simply too many ways of writing the same code.

I don't agree that this makes the language more hard to learn, but perhaps makes it take longer to learn the language fully. For instance, I just recently learned you can write code in the following way:

puts "Hello my friend" if userIsFriendly

I've never seen code like that before, but it really opens up for some nice syntax!

Another feature of Ruby is that Classes are open, and everything is an object, meaning you can extend/modify classes on the fly. Even classes are objects! For example:

class Fixnum
def minutes
return Timespan.new(self)
end
end
class Timespan
def ago
return Time.now - self
end
end

5.minutes.ago


The syntax is almost perfect! How would you otherwise say "5 minutes ago"? That's what people often talk about when they describe Ruby. "The only truly object oriented language".

Why Ruby isn't the perfect language, IMO, is because it's too "open". There are too many ways of writing code. Writing smaller applications, or better scripts, won't take you more than a few minutes, but when you need to work on a larger project, I would definitely go with a strictly typed language, such as C#, where you have better control over your code. Also, not surprisingly, no IDE can help you writing your code, as the code is dynamically typed, and you really can't say what functions are available on an object until you actually run the code.

So, thumb up for this language when working on smaller applications. But for larger projects, I would recommend going for another one.

2 kommentarer:

Peterh sa...

If you like the idea that classes are objects (They are in java anc C3 as well?!) you should try Lisp... That's modifying code on the fly.

Christian Genne sa...

Lisp is in the list of "languages I plan to learn in the future..."!

Java and C# are not truly object oriented, as classes and built in types have a special meaning. In Ruby, when you write "class Hej", that really instantiates a new object named Hej, with the properties of a class. When you construct a new instance of the type Hej, you write "Hej.new", but what that really does is calling the function new on the object Hej. Magic, isn't it? :)