Catch error and warn if unable to remove a method

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.
This commit is contained in:
Brandur 2019-03-18 11:45:57 -07:00
parent df8c141bb8
commit 8702e714bc

View File

@ -298,7 +298,25 @@ module Stripe
# Remove methods for the accessor's reader and writer.
[k, :"#{k}=", :"#{k}?"].each do |method_name|
remove_method(method_name) if method_defined?(method_name)
next unless method_defined?(method_name)
begin
remove_method(method_name)
rescue NameError
# 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:
#
# https://github.com/stripe/stripe-ruby/issues/749
#
# Here we swallow that error and issue a warning so at least
# the program doesn't crash.
$stderr.puts("WARNING: Unable to remove method `#{method_name}`; " \
"if custom, please consider renaming to a name that doesn't " \
"collide with an API property name.")
end
end
end
end