If you imagine Ruby to be entirely implemented in Ruby, we can
(simplistically) say, that those methods are defined as following:
methods.include?(name) || respond_to_missing?(name)
Then, in your code, if you implement method_missing, you typically redefine
respond_to_missing? (fallback method) only, which is simpler.
If you overriding just respond_to_missing?, you can be as simple as:
If you'd override respond_to? instead, you'll need to not forget to call
super, and be ready for performance implications. If you'll be to naive,
# You forgot to return true for method defined the "normal" way
...then some other code may break unexpectedly, which just tried to check
whether you object had #to_s (to write it in logs, for example).
Hope this helps.
сб, 19 янв. 2019 г. в 13:41, Greg Navis <email@example.com>:
I was wondering why do we need #respond_to_missing? as a separate method.
I tried searching for a rationale in the Ruby bug tracker but couldn't find
I don't understand how it's different from overriding #respond_to? to
match dynamic methods and calling super on other matches.
The answer I'm NOT looking for is that #method wouldn't work, etc. as this
is an answer to a different question ("Why do I need to override
Why was Ruby designed in a way that introduces a distinction between
#respond_to? and #respond_to_missing? It feels like I'm overlooking