Testing out Ruby and comparing to the great Python

A long while ago I checked out a Ruby on Rails class that a guy was giving out. I was doing PHP work and this Rails thing had quite the hubub going on about it. So like any good techie I bought the Rails book, the Ruby book, and went work work. A week later I shelved the whole thing and decided it was entirely too much Perl-ish magic for my tastes. Lesson: never try to learn a language and a framework at the same time. You're going to get lost. It's not all bad, after that the same guy had a Turbogears weekend class and I fell in love with Python and the rest is history. Of course, I feel like that Ruby experience is a bit of a failure in my trials and when I was sick this past week decided to check out Ruby itself. It's used in some interesting projects that I've been wanting to check out, Chef, Puppet, and Capistrano.

So I spent the weekend crash coursing through the books Everyday Scripting with Ruby a The Ruby Programming Language and I figured I'd share my initial impressions on the things that look kind of cool. I'm looking at this from the standpoint of possibly doing some Ruby for some quick scripts, but definitely not heading back into the Rails area or doing web development. I love me some Python for that stuff.

First up, perl-ish regex in the string object is going to be handy. When doing quick scripts, one of the things that make Perl such a powerhouse is how engrained regexes are. The ability to do complex matching, replacing, etc in a hurry while processing a file is just good stuff. So I'm definitely looking forward to using some:

[sourcecode lang="ruby"] matches = /Ruby/.match("Checking out some Ruby") [/sourcecode]

The next thing is a pretty small one, but I love the idea of ending methods with a ? to denote it returns a bool. Code like myobj.new? looks so much nicer than myobj.is_new(). I know, small one, but small things can count.

I'm always liking symbols. It's another small thing, but it's an extra character I don't have to hit (the closing quote) and it just reads nicer in the editor since hash keys are no longer strings, but their own syntax. It also makes a lot of sense from the optimization end since there aren't multiple instances of the same string taking up space. I can't imagine it's huge, so for me it's mainly the prettier look in the editor.

[sourcecode lang="ruby"] myhash = { :first => "one thing", :second => "second thing" } [/sourcecode]

I also love the idea of running quick shell commands with just some backticks. With Python I need to import subprocess and perform an a function call, setup stderr/stdout captures, etc. With Ruby I could just do:

[sourcecode lang="ruby"] file_list = `ls $home` [/sourcecode]

and that would definitely be handy in doing some quick shell scripts.

What I'm not so sure about are things like the string variable substitutions. You basically do things like:

[sourcecode lang="ruby"] one_var = 'testing'

puts "I'm #{one_var} ruby out" [/sourcecode]

So you have to predefine the variable and it's got to be in the namespace of the string you're doing the replacement in. You can also use % to do some replacement, but I'm missing something like Python's "".format() in Ruby. I'm sure there are probably libraries that help with it, but I've not run into them yet.

I'm also not sold on the repeated use of self. You can have self at the module level as well as the class level. And the fact that it's not a reference ot an instance, but the class level, makes it something that just rubs my brain the wrong way. Of course, since self is all over Python classes and refers to the current instance I admit that it's probably more my Python usage getting in my way.

Finally, I'm not sold on the gems yet. I know it's pretty much just like eggs from the Cheeseshop,but it just seems things are much more immature and harder to find. It might still be inexperience, and I'll pick things up as I go along, but initially it's a barrier trying to find libraries to help with things.

Anyway, I do like some parts of it and I look forward to trying it out for some scripts. I think it might be just the right mix of Python and Perl for that type of work.