3 ms·
A minor nitpick: > And some of the other lessons, like "don't use method_missing if you can define a method instead", are well worth considering as well. I'd
by epidemian 11y ago
A minor nitpick:
> And some of the other lessons, like "don't use method_missing if you can define a method instead", are well worth considering as well.
I'd argue that this one also falls into the category of "simpler/more readable and, incidentally, more performant".
When defining methods you get the correct behaviour of respond_to? for free, whereas when overriding method_missing, you also have to take care of defining a corresponding respond_to?.
The main reason for choosing def/define_method over method_missing (when possible) should be that it is generally simpler to do so.
- eropple 11y agoDepends on which way you look at it, I think. Like, when building a DSL, I generally take my inputs and do the work up front to use define_method--but I know people who feel that it's simpler to just use method_missing and check against some bit of data here or there. The perf argument isn't going to change their minds overnight, obviously, but I think it helps to make sure you have all the information.