Mar 24, 2008

That was fun, but what now?

After seeing things like
(1..100).inject(&:+)
(link)
or
$<.each do |line|
$> << line unless line.strip.split(",")[2].gsub('"','').strip.empty?
end
(link)
I'm seeing why Ruby really has that appeal to hackers. Just like in C where you can write
while (*a++ = *b++);
to copy a string, Ruby allows you to whip out some really good one-liners. Even better than C though, Ruby gives you a whole whack of things for you to modify the way the language works and ignore some of the ugly things of the machine below. You don't have to worry about memory management, or pointers, or working within the static typing system. You can open up classes easily with reflection, or patch them up with open classes. There is so much to boast about.

Then why do I find myself going back to C++? Ruby makes my life a lot easier with a lot of things, like strings and arrays and hashes and dynamic typing and blah blah blah. In C++ I have to worry about deleting, *gasp* declaring my variables and I can't use functional style approaches (although this is changing, thanks to Boost). Yet when I start up a new random project, I am reluctant to use Ruby for it.

Originally I thought that it might have to do with C++ being my first language. Then again, it wasn't, I was using qBASIC before I'd ever seen a line of C. Then I used C for about a year before I did anything with C++. So this can't be the reason.

One reason would have to be that C++ is more easily portable than Ruby. Before the Ruby enthusiasts have me burned at the stake for such a comment, let me explain. Anything you write in Ruby, you must tack on an interpreter with it. This is easy for programmers, or Mac users, but for my target audience (my friends) this is not really an option. Windows doesn't come with Ruby, and telling them to install the Ruby interpreter is a pain in the ass. But for simplicity's sake, let's say that they do that. Now what? I need a windowing library. So now they have to install a windowing library. This would probably be GTK, which means I'd probably stick Glade in there too, adding on two more things they need to install. Easy if you're running Ubuntu, not so easy otherwise. With C++ I can just pack in a .dll with the install and ship it off. There's probably easy ways to deploy Ruby apps, but just the installing of the Ruby interpreter is enough for me to not want to do it.

Another reason: static typing. While I appreciate dynamic typing for things like web pages and small apps, the structure of static typing is much more reassuring than what I'd have with dynamic typing. I don't want to have to write tests to do things that the compiler should be doing (not that I enjoy writing tests in general).

Wait, a third reason: I like being close to the metal. Although I'm fully aware that the language isn't usually the speed bottleneck, I like writing fast code and unnecessary slow-downs tend to irk me. Even little things:
x = [1, 2, 3, 4] - [2]
When I think about what's required under the hood for this, I can't help but shudder.

Many of these things are handled by Java, with the added benefit of garbage collection. But when you have to write
try{
BufferedReader stdin = new BufferedReader(new FileReader(System.in));
}catch(Exception e){
System.out.println("Something is very wrong.");
}
In order to receive input from the keyboard, I decide that I have better things to do than write out class names that average on 10 characters a piece.

3 comments:

Guillaume Theoret said...

Ummm... Ruby2Script?

http://www.erikveen.dds.nl/rubyscript2exe/

That thing is amazing. It turns any ruby script into an executable file that includes all dependencies. That way they don't need to install ruby or any custom gems or whatever other libraries you might be using. You should check it out.

Rob Britton said...

I figured something like this existed.

However, this brings to mind the quote "When the only tool you have is a hammer, everything looks like a nail".

When using Ruby for desktop apps, I get the feeling that I'm using Ruby for something it isn't really meant to be used for.

I love Ruby, however I've found that developing anything besides websites or small scripts, I enjoy using C++ more - even though I have to manage my own memory, Visual Leak Detector is a great help here: http://dmoulding.googlepages.com/vld-1.9f-README.html.

Andre Holzner said...

quoting: "...I decide that I have better things to do than write out class names that average on 10 characters a piece. "

Have you tried 'dynamic-abbrev' in emacs (on alt slash) or 'Next matching word' (don't recall the standard key binding) in netbeans ? That saves a lot of typing (while still making verbose class names affordable and thus produce more readable code)