News for hackers. Stuff that matters.
Re:Do you know Ruby’s ‘Chainsaw’ method? by jhickner on Wednesday October 06, 2010 @10:28PM (#1766630)
There's actually a fairly safe way to use method_missing. Just remember to call super if none of your conditions are met, that way you'll still get errors when commands are misspelled. You really shouldn't ever use method_missing without that.

He probably also should be checking if @desk actually has a method with the provided name (with @desk.respond_to?(method_name)) before attempting to call it.

Re:Do you know Ruby’s ‘Chainsaw’ method? by tomafro on Thursday October 07, 2010 @02:09AM (#1767004)
He should also define respond_to? on the DoNotDisturb class. Relying on method_missing to pass calls to respond_to? to the proxied class will not work, as it is defined in Object. Responding to a method when respond_to? returns false breaks the class contract.
Re:Do you know Ruby’s ‘Chainsaw’ method? by nusco on Thursday October 07, 2010 @06:25AM (#1767383)
From the OP: I agree with you guys (or gals) on both calling super and redefining respond_to?(). Those are two of the "caveats" I mentioned at the end of the original post. I considered mentioning them explicitly, but the post is long enough already.
Re:Do you know Ruby’s ‘Chainsaw’ method? by epochwolf on Wednesday October 06, 2010 @10:44PM (#1766667)
When using method_missing in a proxy why bother checking? Just call the method and let wrapped object handle the errors. The stack trace will show you where to look for the error. The only reason I can see to check before calling is if you have different failure mode than the wrapped object. Otherwise you are duplicating logic.
Re:Do you know Ruby’s ‘Chainsaw’ method? by jhickner on Wednesday October 06, 2010 @10:55PM (#1766684)
It's a small thing, I admit, but I like getting a slightly cleaner stack trace since the exception happens in the wrapper module (where the error is) instead of getting all the way to @desk.
Re:Do you know Ruby’s ‘Chainsaw’ method? by epochwolf on Wednesday October 06, 2010 @11:03PM (#1766694)
After I've spent the last 2~3 years reading funky rails stacktraces something like that doesn't bother me a bit. (I get more worried about older versions of jruby deciding to puke on internal ruby<->java mismatches at work) :)