Ruby error checking

Posted by Kerry Sun, 20 Aug 2006 09:19:00 GMT

Tim Bray is running a series of posts on an Atom Publishing Protocol client that tests out an APP implementation called APE, or Atom Protocol Exerciser.

In one of the series Tim describes how he uses Duck Typing to handle checking a URI for validity. My first thought was: That isn’t an example of Duck Typing. Sam Ruby has posted his thoughts on how it should be done, which is Ok, but I think I have something equally as good.

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

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

This way, you never need to check whether something is of a certain Type, and it doesn’t impact the readability much either. Some people might not like the Array return type however, each to his own.

This is how the xmlrpc library works when using the client.call2 api.

Comments

Leave a response

Comments