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


There is probably room for a shorter syntax for refinements.
But one thing which is not acceptable in this specific proposition here is that

~~~ ruby
class Test
  refine String do
    def refined?
      true
    end
  end
  # String refinements apply here
end
~~~

and

~~~ ruby
module Test
  refine String do
    def refined?
      true
    end
  end
  # String refinements do not apply here, it's a normal Module#refine
end
~~~

would do two very different things, which is too hard to understand and confusing.

Maybe a block to #using would make things slightly shorter:

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

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

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