Issue #14344 has been updated by Eregon (Benoit Daloze).


kddeisz (Kevin Deisz) wrote:
> Just to take a real example from my current application, here's a job (from Rails ActiveJob) that I want to refine by moving the logic into the class in which it belongs. It currently looks like this:

That's an interesting example indeed: using refinements to not pollute the model class but still make it convenient to write methods with the model as `self`.

To clarify my comment above: I'm not against a shorter way to define refinements+use them.
But #refine as proposed is wrong: if EventEndActionsJob was a module it would stop working because then
it would be the refine-just-define-refinements (current semantics) and not the refine-define-and-use-refinements you propose.

Or are you proposing to change the behavior of `refine do ... end` to always enable refinements after it until the end of the class/module body or the end of the file?
Then there would be a compatibility risk.

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

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