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

Catch error and warn if unable to remove a method #750

Merged
merged 1 commit into from
Mar 18, 2019

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Mar 18, 2019

In some cases there can be a method that's detected with
method_defined?, but which cannot be removed with remove_method,
even though it's on the same class. The only case so far that we've
noticed this is when a class is reopened for monkey patching like in
issue #749.

We'll still try to discourage this sort of use, but here we swallow the
error and issue a warning so at least the program doesn't crash.

Fixes #749.

r? @ob-stripe

In some cases there can be a method that's detected with
`method_defined?`, but which cannot be removed with `remove_method`,
even though it's on the same class. The only case so far that we've
noticed this is when a class is reopened for monkey patching like in
issue #749.

We'll still try to discourage this sort of use, but here we swallow the
error and issue a warning so at least the program doesn't crash.

Fixes #749.
@brandur-stripe
Copy link
Contributor

Unfortunately I didn't add tests here because I don't know of a way to reproduce the problem that doesn't have side effects. I tested that it works with a separate repro script though.

Copy link
Contributor

@ob-stripe ob-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

OOC, do you know what causes a method to have method_defined? == true but not be removable via remove_method?

@brandur-stripe
Copy link
Contributor

OOC, do you know what causes a method to have method_defined? == true but not be removable via remove_method?

Ugh, I wish I understood this better, but unfortunately — no, not completely.

The normal case where this is possible is if a method is defined in an ancestor class. In this case it'll be reported as method_defined? == true, but you can't remove the method (although you can undef_method it to accomplish something similar).

That's not the case here though because it's the same class. There must be some subtlety that causes a similar effect when a class is reopened, but I couldn't find anything from reading the documentation.

Anyway, thanks for the review!

@brandur-stripe brandur-stripe merged commit c1894e8 into master Mar 18, 2019
@brandur-stripe brandur-stripe deleted the brandur-warn-on-remove-error branch March 18, 2019 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants