[RAD stands for Ruby Ape Diaries, of which this is part IV.] That glob of letters in the title stands for “There’s More Than One Way To Do It”, and it comes from Perl culture. It is a distinguishing feature of Perl that if there’s something you need to do, not only does Perl have a way to do it, it has lots of different ways. Perl’s legion of evangelists, led by Larry Wall, argue that this is an advantage. Larry has a compelling argument by parallel with linguistics; natural languages usually have many ways of saying anything that’s worth saying. Not everyone agrees that this is a virtue; in particular, Pythonistas have historically grumbled. Ruby seems to fall into the Perl camp. And now, dear readers, I must confess to a bit of duplicity. When I wrote RAD III: Quack Like a URI, I knew that my example was perhaps not the purest possible example of duck typing, but my hidden agenda, namely gathering material for a TMTOWTDI essay, worked out well.

Ruby on Ruby · First off the mark was Sam Ruby, with Quack Squared, who argues convincingly for:

uri = AtomURI.check alleged_uri
return report_error(uri) unless uri.methods.include? :path
request = Net::HTTP::Get.new(uri.path)

But check his comments, where we find six other suggested approaches. The last is kind of startling:

uri = URI.new_with_app_check(alleged_uri) { |e| return report_error(e) }

I can see where they’re going, but something about it makes me nervous.

Burke on Ruby · Over at the ronin blog (cool URI BTW), Kerry Burke offers another elegant suggestion: return two values:

def some_method
    return [true, "actual return value here"]
end

ok, value = some_method
return "this didn't work" unless ok
puts value

Morel on Ruby · Xavier Morel, who doesn’t have a blog, wrote:

I just read your “Quack like a URI” article, and I had a few things to tell you about it:

I wrote back disagreeing with Xavier’s claim that there was anything incorrect about my class test; I wrote the atomURI.check method and I know that what’s coming back is either a URI or a String, and I absolutely don’t want it rooting around the whole inheritance tree. And Xavier, I said, TMTOWTDI.

Xavier wrote back:

And to quote Python, “There should be one—and preferably only one—obvious way to do it ... Although that way may not be obvious at first unless you’re Dutch.”


ongoing
software · G & M · Dad author · colophon · rights
picture of the day
Around August 20, 2006: RAD V: << !! · RAD VII: J?REXML · RAD VI: Java APIs from JRuby · RAD III: Quack Like a URI · RAD II: Back to Ruby

What?
· Technology (61 fragments)
· · Dynamic Languages (38 more)
· · Java (93 more)
· · Ruby (49 more)


Serif · Sans-Serif
I work at Sun Microsystems. The opinions expressed here are my own, and neither Sun nor any other party necessarily agrees with them.