Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent definitions for respond_to? #533

Merged
merged 6 commits into from
Aug 20, 2022

Conversation

floehopper
Copy link
Member

No description provided.

Although this is a unit test, I think it's a bit simpler to rely on the
built-in implementation of Object#respond_to? rather than redefining
that method in some of these tests.

Also the real implementation of Object#respond_to? accepts an optional
2nd argument. We could change the relevant tests to allow such an
argument in the redefined versions of the method in these tests.
However, this doesn't work in Ruby v1.8.6, because blocks can't have
default values for arguments. The new approach means that this is a
non-issue.
As far as I can see, Object#respond_to? has supported an optional 2nd
argument since at least Ruby v1.8.6 and perhaps forever. The only thing
thing that changed was that in Ruby v2 onwards the optional 2nd argument
changed its name from include_private to include_all. I'm not sure
whether this was just a clarification about the behaviour for protected
methods or whether prior to v2 protected methods were never checked.

In any case, it seems to make sense to assume that a respond_to? method
always supports an optional 2nd argument and avoid the arity check in
Mock#respond_to_missing?. My suspicion is that this check was only added
to avoid having to fix some tests in MockTest which I've since resolved
in an earlier commit.
This method delegates respond_to_missing? behaviour to the responder
object. As far as I can see Object#respond_to_missing? has required the
2nd argument since it was introduced in Ruby v1.9.2. So it's more
consistent for Mock#respond_to_missing? to do the same.
The argument name used for Object#respond_to? in earlier Ruby versions
is include_private and so it's clearer to use that everywhere.
With Object#respond_to? which supports an optional 2nd argument called
include_private.
Since Ruby v2, the 2nd optional argument to Object#respond_to? has been
named include_all. Since we're likely to drop support for pre-v2
versions of Ruby in the not too distant future, I think making this
change should make things clearer.
floehopper added a commit that referenced this pull request Aug 15, 2022
@floehopper floehopper marked this pull request as ready for review August 20, 2022 18:57
@floehopper floehopper merged commit 1ade384 into main Aug 20, 2022
@floehopper floehopper deleted the consistent-definitions-for-respond-to branch August 20, 2022 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant