July 24th, 2007
Knowing Ruby and Perl
I came upon a startling realization today after reading some feedback from an anonymous source regarding my previous post:
Your complaint about being unable to write code the way you’re used to with Ruby when picking up Perl for a hours seemed pretty thoughtless. Of course it’s harder for you in Perl — you don’t know Perl nearly as well as you know Ruby! Claiming that this is somehow a fundamental flaw of Perl is just not right.
I meant to suggest in my previous post that the flaw was in me for no longer thinking in Perl when using Perl, though at the same time if Perl were to better support DSLs (domain specific languages) then I would have gotten a Get-Out-of-Jail-Free Card despite my mis-think. I hope that others reading this benefit from the clarification as well.
But this reply also made me think about my comparative knowledge in the two languages. Do I really know Ruby better than Perl like stated in the feedback? I do not feel like I do. I know a lot about both languages, and there are gaps in my knowledge of each language. In fact, other than those people that write Perl modules in C, I generally know as much or more about Perl than other Perl users I meet. It is a real nice language that has served me very well over the years. My problem with it is that I just do not alter my thinking to eliminate concepts alien to Perl when using it, or at least not in version one of whatever I am writing. 😉
My knowledge of Ruby is not shallow, but it is not as deep as I would really like, either. I can write Ruby modules in C, and I have used that knowledge to embed Ruby directly into a monolithic C program in an attempt to port it slowly, release by release. However, there are constantly things that crop up in Ruby that leave me scratching my head thinking, “wow, I can do that!?” I did not know until recently that a Hash object could be given a default value other than nil. Previously, if were using a Hash of Array objects I would have done this:
def my_set (hash, key, index, object) if not hash.has_key? key hash[key] = Array.new end hash[key][index] = object end h = Hash.new my_set(h, 'red', 1, 'one') my_set(h, 'blue', 2, 'two') p h => {"blue"=>[nil, nil, "two"], "red"=>[nil, "one"]}
Now I know can just define the Hash object differently to do the same thing:
h = Hash.new { |hash, key| hash[key] = Array.new } h['red'][1] = 'one' h['blue'][2] = 'two' p h => {"blue"=>[nil, nil, "two"], "red"=>[nil, "one"]}
It is pretty simple and makes for cleaner code, but I did not know it. I must not have paid enough attention during the Hash class.
Little things like this led me to think that perhaps I know Perl better than Ruby, but I think that is probably not true anymore. There is so much to know about both languages that one can easily not be certain, but since 2004 I have not been paying nearly as much attention to Perl as I have to Ruby. Over the course of these last three years it must be inevitable that my Ruby knowledge would surpass my Perl knowledge. I think it has happened, but I would never wager a bet on when. I just do not know.