Forum: Ruby-core Refinements as files instead of modules

45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2014-04-22 17:57
(Received via mailing list)
Issue #9704 has been updated by Thomas Sawyer.


I realized there is a downside to this approach.

~~~
# a.rb
require 'b'
class String
  def ab
    self + "a" + b
  end
end

# b.rb
class String
  def b
    self + "b"
  end
end
~~~

So what would happen with `b.rb` when `using 'a'`? Should the `require
'b'` become a `using`? If so, then what happens when it requires
something that is not an extension, e.g. `set`?

If that is an unsolvable issue. The only fix, it seems, would be a way
to have something like `require_or_using` which would act according to
the initial loading method used. And that starts to look pretty ugly.





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

* Author: Thomas Sawyer
* Status: Open
* Priority: Normal
* Assignee:
* Category: core
* Target version: Next Major
----------------------------------------
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.
This topic is locked and can not be replied to.