Bug #13446

refinements with prepend for module has strange behavior

Added by mtsmfm (Fumiaki Matsushima) over 2 years ago. Updated about 1 month ago.

Target version:
ruby -v:
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]


using {
  refine Enumerable do
    alias :orig_sum :sum

module Enumerable
  def sum(*args)

class GenericEnumerable
  include Enumerable

  def each

# # if we uncomment this line, `GenericEnumerable#sum` will work
Enumerable.prepend( # if we comment out this line, `GenericEnumerable#sum` will work

p # undefined method `orig_sum' for #<GenericEnumerable:0x0000000127c120 @values=[1, 2, 3]> (NoMethodError)

Is this intentional?

Related issues

Related to Ruby master - Bug #16242: Refinements method call to failedOpenActions


Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Assignee set to nobu (Nobuyoshi Nakada)
  • Status changed from Open to Assigned
  • Description updated (diff)

Updated by wanabe (_ wanabe) about 1 month ago

  • Related to Bug #16242: Refinements method call to failed added

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

Fixing this first requires fixing #16242, which allows including a module that uses prepend and is refined. However, the fix for #16242 does not fix the example in this case, as this prepends the included module after the module is included.

The reason this is still broken after the fix for #16242 is that a refined module that hasn't been prepended to yet keeps the refined methods in the module's method table. When prepending, the module's method table is moved to the origin iclass, and then the refined methods are moved from the method table to a new method table in the module itself.

Unfortunately, that means that if a class has included the module, prepending breaks the refinements, because when the methods are moved from the origin iclass method table to the module method table, they are removed from the method table from the iclass created when the module was included earlier.

Fix this by always creating an origin iclass when including a module that has any refinements, even if the refinements are not currently used (see I wasn't sure the best way to do that. The approach I choose was to use an object flag. The flag is set on the module when Module#refine is called, and if the flag is present when the module is included in another module or class, an origin iclass is created for the module.

Also available in: Atom PDF