Issue #14344 has been updated by zverok (Victor Shepelev).


> Also I guess in general refinements are not defined right in the class using them, especially if they are not trivial one-liner methods.

I believe that "one-liner" & "inplace" definition is one of the most important usages of refinements.

Because, speaking philosophically, why do we need them at all? 

Instead of doing "`refine String` + call `string.something`" you always can `StringUtilModule.something(string)`, but when you do some, say, complicated reporting algorithm, those `StringUtilModule.something(string)`, especially several in a row can become REALLY ugly. 

Therefore you just... add one small method to `String`. Just here and there, as close to its usage and as visible and as easy as possible, and it WILL be one-line method, just calling the same `StringUtilModule.something(string)` (which is easier to test and maintain and document).

I believe that "module with refinement for the several classes" is, to the opposite, much less frequent case. It is either "all cool shticks for my entire project in one file", or something very domain-specific (like, I don't know, `String#as_currency`, `Numeric#as_money(currency)`, `CSV#ready_money` something).

----------------------------------------
Feature #14344: refine at class level
https://bugs.ruby-lang.org/issues/14344#change-69499

* Author: kddeisz (Kevin Deisz)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I rely on refinements a lot, but don't want to keep writing `Module.new` in code. I'm proposing `Object::refine`, which would create an anonymous module behind the scenes with equivalent functionality. So:

~~~ ruby
class Test
  using Module.new {
    refine String do
      def refined?
        true
      end
    end
  }
end
~~~

would become

~~~ ruby
class Test
  refine String do
    def refined?
      true
    end
  end
end
~~~

It's a small change, but reads a lot more clearly. Thoughts?



-- 
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>