Project

General

Profile

Feature #12533

Refinements: allow modules inclusion, in which the module can call internal methods which it defines.

Added by chucke (Tiago Cardoso) about 4 years ago. Updated almost 3 years ago.

Status:
Assigned
Priority:
Normal
Target version:
-
[ruby-core:76197]

Description

Right now this isn't possible:

module Extensions
  def vegetables ; potatoe ; end
  def potatoe ; "potatoe" ; end
end

module Refinary
  refine String do
  # this doesn't work
  include Extensions
  # this would work...
  # def vegetables ; potatoe ; end
  # def potatoe ; "potatoe" ; end
  end
end

using Refinary

puts "tomatoe".vegetables

#=> in <main>': undefined method 'vegetables' for "tomatoe":String

Wrongly reported as a bug here.

According to Shugo Maeda, this was expected behaviour. I argued that this is the way most monkey-patches work, and if Refinements can't cover the use case of inserting a custom DSL which references itself in the classes it refines, it can't fully replace monkey-patches, which I read was the main reason Refinements have been added to the language.


Related issues

Related to Ruby master - Bug #14012: NameError is raised when use class variables in RefinementsOpenActions

Also available in: Atom PDF