Make refinement scoping to be like that of constants
Refinements are currently lexically scoped, which makes their use burdensome when one wants to apply a refinement to an entire project, requiring boiler plate at the top of every project file. I propose that there ought to be a method of applying refinements to an entire namespace (module), similar to how constants are scoped.
For example, here's a trivial example of how one is able to scope a new RuntimeError class inside a module.
# a.rb require 'b' module MyProject class RuntimeError < ::RuntimeError; end end # b.rb module MyProject class SomeClass def initialize raise RuntimeError, "Application error occurred" # Raises MyProject::RuntimeError exception end end
Could refinements be scoped in a similar way, without the requirement for the
using clause in every file. For example:
# a.rb require 'b' module MyProject refine! String do def exclaim self + "!!!" end end end # b.rb module MyProject class SomeClass def initialize puts "Hello".exclaim # Outputs "Hello!!!" end end
I believe this would be an intuitive means of making refinements global to a project, which seems to be a highly desired feature, and something that needs to happen to mostly eliminate monkey patching. Of course, using the
refine name would clash with the current behaviour of refine and break backwards compatibility, so I'd propose introducing
refine! as an alternative to invoke this proposed new behaviour.
Apologies if this has already been proposed/discussed. I did search for similar proposals, but couldn't find anything. I'm interested to hear what some of the potential pitfalls of this would be.