Is `response_to_missing?` A second argument useful for something?

When using method_missing in Ruby is almost always a good idea to define respond_to_missing? .

respond_to_missing? accepts two arguments; the name of the method we are testing ( symbol ), and a Boolean value indicating whether private methods should be included in our test ( include_all ).

Now, what am I confused about: method_missing does not accept any arguments that could tell it whether it should call private methods or not, because respond_to_missing? . In addition, method_missing is called regardless of whether the original method was called in a public or private context, and regardless of what respond_to_missing? returns this method in the appropriate context. Thus, all method_missing operations are publicly available.

If so, then what purpose does the second respond_to_missing? argument make respond_to_missing? ( include_all )? Is this object a response to a missing method and cannot affect the context in which the missing method was called, why is there even no such argument?

+6
source share
2 answers

I think respond_to_missing? has a second argument for the same reason as respond_to? . In both cases, it allows the code to ask the object what methods it responds to in such a way that the confidentiality of the method is respected. If used correctly, it can help you encapsulate your objects better.

You indicated a missing feature in method_missing , namely that it should have an argument indicating whether the method was called in a public or private context. Maybe method_missing will have this function someday. Until then, all the functionality of an object that is implemented through method_missing will be effectively public, but can you stop people from using it in their documentation and respond_to_missing? .

+3
source

method_missing does not accept any arguments that could indicate whether it should call private methods or not

method_missing () does not call public or private methods. method_missing () is called at the very end of the method search path when the method cannot be found. Thus, comparing method_missing () and response_to * is insensitive. Why do you think they should act the same? response_to? () has never been symmetric with method_missing ().

0
source

Source: https://habr.com/ru/post/970815/


All Articles