Method_missing requires responds_to?

   By chris on January 13th 2009 in /dev/ruby | 1,254 views

I believe this should be taken as dogma and followed religiously:

I reject the argument because I reject as buggy any code such that object o responds to method m but o.responds_to?(:m) => false. If you implement your own method_missing for a class, you should almost always implement your own responds_to? as well.

- Maybe

There is wisdom in them there words. Monkey-patching is (perhaps) ok, but voodoo should probably be avoided at all costs and an object that can act but won’t provide a means of letting you know without actually trying it is just plain ol’ voodoo. And programming voodoo is bad juju.

4 Responses to “Method_missing requires responds_to?”

  1. Julian Russell responded on 14 Jan 2009 at 8:31 pm #

    Word. I recently wrote a quick wrapper for a java API that rubyised most of the method calls using method_missing (underscore -> camelCase etc).

    I would have just felt dirty if I hadn’t done the responds_to? implementation as well.

  2. Antti Tarvainen responded on 15 Jan 2009 at 12:42 am #

    Maybe it’s just me, but…

    Shouldn’t the title be the other way around? “Method_missing requires responds_to?”

  3. coderrr responded on 15 Jan 2009 at 1:13 am #

  4. chris responded on 15 Jan 2009 at 8:45 am #

    Antti: you’re absolutely correct – thanks for catching that!