2006-11-05

On nameservers and elegant linguistic features

After a server relocation this weekend, that Ruby script I wrote successfully warned me that my NS records were out of date. I have since rewritten the script tonight to be a bit more efficient and to use some of the features that Ruby provides.

This is not a post wherein I gush over Ruby.

I will, however, point out that I appreciate a language, Ruby, Perl, or Python being the big three that will have a function/feature/method/built-in that will solve my problem without a lot of huffing and puffing on my end.

Case in point: I have two hashes, h1 and h2:

h1 = {'ns1' => '1.2.3.4', 'ns2' => '3.4.5.6' }
h2 = {'host1' => '9.8.7.6', 'host2' => '3.4.5.6' }

These hashes have some overlap. They each have some unique values. My initial thinking is "if I want to get the keys in h1 that have values not found in h2, I'd have to iterate through some kind of loop. This isn't exactly hard, but you wind up building a nested foreach loop to compare everything to everything else.

In C, you gotta do all this. By hand. This is the 21st century.

In younger, less DIY kinds of languages, there are ways around this. Guido and Larry Wall and Matz have felt our pain and worked to abstract this kind of common activity into a more elegant kind of solution. In Perl, you'd type "man perlfaq4" (perlfaq4, you're a Godsend) or, after awhile, give in and buy a copy of Perl Cookbook. The tools are there in the language to automatically mash two hashes together and get a list of values you care about. In Ruby, this is even easier:

difference = h1.values - h2.values

I suspect this is due in large part to Ruby's insistence that absolutely everything be an object, no exceptions (except for the things that are special exceptions to that rule). This is why I'm fairly confident that Perl 6 will also have an elegant method to grab a subset of two lists. When it comes out, it will be awesome. 'til then, Ruby/Python/Perl5/PHP and friends are going to have to fight amongst themselves to provide the most elegant feature for solving my exact problem.

No comments: