[ruby-core:77590] [Ruby trunk Feature#9704] Refinements as files instead of modules

From: transfire@...
Date: 2016-10-11 18:36:00 UTC
List: ruby-core #77590
Issue #9704 has been updated by Thomas Sawyer.


Shugo Maeda wrote:
> Monkey-patching style has another problem that super cannot be supported in monkey patching.
> 
> I prefer another approach to extend classes both globally and locally in the same manner.
> How about to provide a way to activate refinements globally?
> 
>   using Foo, global: true
>   # Refinements in Foo are activated globally.
> 
> Module#refine creates a module as a refinement, so it can be globally activated by Module#prepend-ing that module to the target class.

That last line just clicked with me, and I see old problem. This only works at one level. How would one write a refinement that adds methods to class level and instance level at the same time?




----------------------------------------
Feature #9704: Refinements as files instead of modules
https://bugs.ruby-lang.org/issues/9704#change-60860

* Author: Thomas Sawyer
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
If refinements are to remain file-scoped, then it would be more convenient if `using` worked at the file level, akin to `require`, rather than behave like a module `include`. For instance, instead of:

~~~
# foo.rb
module Foo
  refine String do
    def some_method
      ...
    end
  end
end
~~~

~~~
require 'foo'
using Foo
~~~

We could do:

~~~
# foo.rb
class String
  def some_method
    ...
  end
end
~~~

~~~
using 'foo'
~~~

This would make `require` and `using`, in a certain sense, *polymorphic* --if we `require` it will extend the classes directly, but if `using` then they will be refined instead.




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

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

In This Thread

Prev Next