2 ms·
Try: [].respond_to?(:exec, true) # => true respond_to? doesn't include private methods by default but can be made to do so with the second argument. I didn
by petercooper 16y ago
Try:
[].respond_to?(:exec, true) # => true
respond_to? doesn't include private methods by default but can be made to do so with the second argument. I didn't know this either till I just looked it up.
This made sense to me since I'd assume respond_to? would only yield true if it were possible to make the call without error. Yet, this logic does not hold true:
class X; def y; respond_to? :exec; end; end
X.new.y # => FALSE
So it seems it doesn't matter if the private method is callable or not, respond_to? won't give you a true for any private method, accessible in the current scope or not, unless you pass the extra flag.
(I'm not really writing all of this to give you an education, more to give me one. I was totally unaware of all this and found it interesting :-))
- steveklabnik 16y agoNo, don't feel bad. That's awesome. I knew that thing about adding true, I just didn't realize that exec was private.