Project

General

Profile

Bug #13096

error using undef_method + refinements

Added by zenspider (Ryan Davis) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
2.3 or 2.4
[ruby-core:78944]

Description

I've got a bug report on minitest/mock that I've reduced to the following:

class Object
  def to_query            # remove this, works fine
  end
end

Object.prepend Module.new # remove this, works fine

module ObjectExtensions
  refine ::Object do
    def to_query          # remove this, works fine
    end
  end
end

class Mock
  instance_methods.each do |m|
    next if m =~ /object_id|__send__/
    undef_method m
  end
end

giving me the error:

bug671.rb:18:in `undef_method': undefined method `to_query' for class `Mock' (NameError)
    from bug671.rb:18:in `block in <class:Mock>'
    from bug671.rb:16:in `each'
    from bug671.rb:16:in `<class:Mock>'
    from bug671.rb:15:in `<main>'

This code comes from activesupport via regular requires, so this is a real problem for me.

Looks like the culprit might be here:

https://github.com/ruby/ruby/blob/v2_4_0/vm_method.c#L1187

I'd expect to be able to undef anything that comes back from instance_methods regardless of whether there is a refinement in place or not.

Additionally, the error message is just plain wrong. The method is most certainly defined (twice!) and should be improved.

Also available in: Atom PDF