Issue #12533 has been updated by matz (Yukihiro Matsumoto).


As you may know, `include` inserts the module in the inheritance hierarchy. In this case, `module Extensions` is inserted above `String` in the `using Refinary` scope. That means the lexical scope of `vegetables` and `potatoes` are different from the refined scope so that `potatoes` cannot be called from `vegetables` because the scope is not refined. The situation is a bit complex. Do you follow me?

In some other class extension proposals found in other languages (for example, ClassBox in Java), scopes of methods called from within ClassBox are also modified. This is called local rebinding. But we don't choose that because it has bigger side effects. We chose the safer side.

The issue is by `include` we might expect the features from the included module are available but in fact, they aren't due to the mechanism of `include` and refinements.

So I counter-propose a new feature, `Module#inject`. Unlike `include`, `inject` does not modify inheritance hierarchy. Instead, it copies attributes (constants, modules, and refinements) into the target class/module.


shevegen,
We have no concrete plan to refine the refinement. We are vaguely thinking about combining `require` and `using` in some way. Just idea.

Matz.


----------------------------------------
Feature #12533: Refinements: allow modules inclusion, in which the module can call internal methods which it defines. 
https://bugs.ruby-lang.org/issues/12533#change-66931

* Author: chucke (Tiago Cardoso)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Right now this isn't possible:

~~~ruby
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](https://bugs.ruby-lang.org/issues/12514). 

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. 



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>