Project

General

Profile

Bug #14671

Refining Module#refine itself introduces strange state

Added by shyouhei (Shyouhei Urabe) over 2 years ago. Updated about 1 month ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.0dev (2018-04-09 trunk 63122) [x86_64-darwin15]
[ruby-core:86487]
Tags:

Description

using Module.new {
  refine Module do
    def refine *;
      puts self
    end
    public :refine
  end
}
Object.refine # => NoMethodError

It is possible to refine Module#refine, but there seems to be no way to call it.

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

  • Status changed from Open to Rejected

This is because Class undefs refine, and the refinement for Module is after the lookup chain for Class. Your code works fine if you call refine on a Module and not a Class, or if you remove the undef from Class. Ruby does not support directly removing an undef through remove_method, but you can override the method and remove the overridden method. This code:

using Module.new {
  refine Module do
    def refine *;
      puts self
    end
    public :refine
  end
}
Enumerable.refine

Class.define_method(:refine){}
Class.remove_method(:refine)
Object.refine

prints:

Enumerable
Object

Your code also works if you refine Class instead of Module.

Also available in: Atom PDF