Skip to content

Commit 3f39457

Browse files
committed
Fix removing class methods between 1.8 and 1.9
#instance_methods in 1.8 returns strings, not symbols. Most recommend using method_defined? instead of #instance_methods.include?, however, #method_defined? doesn't allow filtering of inherited methods. So, we switch back to using #method_defined? which means we need to handle the 'method not defined on class' error another way... Use #undef_method in place of #remove_method which will undefine inherited methods as well. This is what we should have done all along. The parent class method is still defined as expected. #undef_method only hides that implementation from the subclass so it doesn't attempt to defer to the parent class. This allows a safe #define_method on the subclass.
1 parent dbc5039 commit 3f39457

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/gimme/spies_on_class_methods.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ def spy(method)
1010
meta_class = (class << @cls; self; end)
1111
method = ResolvesMethods.new(meta_class, method).resolve(@raises_no_method_error)
1212

13-
if meta_class.instance_methods(false).include? method
13+
if meta_class.method_defined? method
1414
Gimme.class_methods.set(@cls, method)
15-
meta_class.send(:remove_method, method)
15+
meta_class.send(:undef_method, method)
1616
end
1717

1818
cls = @cls

0 commit comments

Comments
 (0)