https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112018-01-09T16:38:36ZRuby Issue Tracking SystemRuby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=694952018-01-09T16:38:36Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I like the proposed syntax. Syntax is one major reason for<br>
me, oddly enough, to not use refinements. In particular the<br>
"using" clause felt weird, so I like changes (improvements?)<br>
to the syntax.</p>
<p>I think your syntax suggestion is better than the status quo<br>
too.</p>
<p>However had, I am saying all of this without really having<br>
used refinements extensively (other than in test.rb files),<br>
so I am essentially not qualified to comment really. I'd<br>
be inclined to use your suggestion more than the "using"<br>
variant, though. ;)</p>
<p>(There was some presentation at ruby or railsconf about<br>
someone talking about nobody using refinements or so,<br>
a few years ago perhaps. A bit like the old "nobody knows<br>
nobu", which isn't true by now anymore either. Either way<br>
I think people should comment who use refinements a lot<br>
too, since they should be able to consider the impact of<br>
the changed syntax proposal.)</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=694962018-01-09T17:13:52ZEregon (Benoit Daloze)
<ul></ul><p>The whole purpose of the Module around it is so multiple refinements can be bundled up together in a Module and that Module is passed to #using.</p>
<p>I don't think #refine should enable refinements after the block.<br>
It doesn't seem clear to me and is too confusing with the existing usage of #refine.<br>
Whether #refine is called on a Module or not would have very different semantics.</p>
<p>Also I guess in general refinements are not defined right in the class using them, especially if they are not trivial one-liner methods.</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=694972018-01-09T17:19:00Zkddnewton (Kevin Newton)kddnewton@gmail.com
<ul></ul><p>I think we're talking about two different use cases. There is a place for refinements with large batch changes and a module is very appropriate for that. But for smaller refinements, which make life a lot easier, it's really not that far of a stretch. It's not that much different from <code>class Foobar ... end</code> being a shortcut for <code>Foobar = Class.new do ... end</code></p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=694982018-01-09T17:26:56ZEregon (Benoit Daloze)
<ul></ul><p>There is probably room for a shorter syntax for refinements.<br>
But one thing which is not acceptable in this specific proposition here is that</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Test</span>
<span class="n">refine</span> <span class="no">String</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">refined?</span>
<span class="kp">true</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="c1"># String refinements apply here</span>
<span class="k">end</span>
</code></pre>
<p>and</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Test</span>
<span class="n">refine</span> <span class="no">String</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">refined?</span>
<span class="kp">true</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="c1"># String refinements do not apply here, it's a normal Module#refine</span>
<span class="k">end</span>
</code></pre>
<p>would do two very different things, which is too hard to understand and confusing.</p>
<p>Maybe a block to #using would make things slightly shorter:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Test</span>
<span class="n">using</span> <span class="k">do</span>
<span class="n">refine</span> <span class="no">String</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">refined?</span>
<span class="kp">true</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=694992018-01-09T17:31:07Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><blockquote>
<p>Also I guess in general refinements are not defined right in the class using them, especially if they are not trivial one-liner methods.</p>
</blockquote>
<p>I believe that "one-liner" & "inplace" definition is one of the most important usages of refinements.</p>
<p>Because, speaking philosophically, why do we need them at all?</p>
<p>Instead of doing "<code>refine String</code> + call <code>string.something</code>" you always can <code>StringUtilModule.something(string)</code>, but when you do some, say, complicated reporting algorithm, those <code>StringUtilModule.something(string)</code>, especially several in a row can become REALLY ugly.</p>
<p>Therefore you just... add one small method to <code>String</code>. 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 <code>StringUtilModule.something(string)</code> (which is easier to test and maintain and document).</p>
<p>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, <code>String#as_currency</code>, <code>Numeric#as_money(currency)</code>, <code>CSV#ready_money</code> something).</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=695282018-01-10T16:05:51Zkddnewton (Kevin Newton)kddnewton@gmail.com
<ul></ul><p>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:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">EventEndActionsJob</span> <span class="o"><</span> <span class="no">ApplicationJob</span>
<span class="n">queue_as</span> <span class="ss">:default</span>
<span class="k">def</span> <span class="nf">perform</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
<span class="k">return</span> <span class="k">if</span> <span class="n">event</span><span class="p">.</span><span class="nf">end_actions_completed?</span>
<span class="n">event</span><span class="p">.</span><span class="nf">update!</span><span class="p">(</span><span class="ss">end_actions_completed: </span><span class="kp">true</span><span class="p">)</span>
<span class="n">activate_survey_for</span><span class="p">(</span><span class="n">event</span><span class="p">)</span> <span class="k">if</span> <span class="n">event</span><span class="p">.</span><span class="nf">survey</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="k">def</span> <span class="nf">activate_survey_for</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
<span class="n">event</span><span class="p">.</span><span class="nf">survey</span><span class="p">.</span><span class="nf">update!</span><span class="p">(</span><span class="ss">active: </span><span class="kp">true</span><span class="p">)</span>
<span class="n">event</span><span class="p">.</span><span class="nf">rsvps</span><span class="p">.</span><span class="nf">not_declined</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">rsvp</span><span class="o">|</span>
<span class="no">EmailJob</span><span class="p">.</span><span class="nf">perform_later</span><span class="p">(</span><span class="s1">'PostEventSurvey'</span><span class="p">,</span> <span class="n">rsvp</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>but I want it to look like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">EventEndActionsJob</span> <span class="o"><</span> <span class="no">ApplicationJob</span>
<span class="n">refine</span> <span class="no">Event</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">perform_end_actions</span>
<span class="k">return</span> <span class="k">if</span> <span class="n">end_actions_completed?</span>
<span class="n">update!</span><span class="p">(</span><span class="ss">end_actions_completed: </span><span class="kp">true</span><span class="p">)</span>
<span class="n">activate_survey</span> <span class="k">if</span> <span class="n">survey</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="k">def</span> <span class="nf">activate_survey</span>
<span class="n">survey</span><span class="p">.</span><span class="nf">update!</span><span class="p">(</span><span class="ss">active: </span><span class="kp">true</span><span class="p">)</span>
<span class="n">rsvps</span><span class="p">.</span><span class="nf">not_declined</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">rsvp</span><span class="o">|</span>
<span class="no">EmailJob</span><span class="p">.</span><span class="nf">perform_later</span><span class="p">(</span><span class="s1">'PostEventSurvey'</span><span class="p">,</span> <span class="n">rsvp</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">queue_as</span> <span class="ss">:default</span>
<span class="k">def</span> <span class="nf">perform</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
<span class="n">event</span><span class="p">.</span><span class="nf">perform_end_actions</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>now all of the logic is in the right place (in the Event model) but I don't have to clutter up the class definition with a method that will only be used in this one place. I don't need to refine multiple classes, so I don't want to build a whole module, but instead right now I have to:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">EventEndActionsJob</span> <span class="o"><</span> <span class="no">ApplicationJob</span>
<span class="n">using</span><span class="p">(</span>
<span class="no">Module</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span>
<span class="n">refine</span> <span class="no">Event</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">perform_end_actions</span>
<span class="k">return</span> <span class="k">if</span> <span class="n">end_actions_completed?</span>
<span class="n">update!</span><span class="p">(</span><span class="ss">end_actions_completed: </span><span class="kp">true</span><span class="p">)</span>
<span class="n">activate_survey</span> <span class="k">if</span> <span class="n">survey</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="k">def</span> <span class="nf">activate_survey</span>
<span class="n">survey</span><span class="p">.</span><span class="nf">update!</span><span class="p">(</span><span class="ss">active: </span><span class="kp">true</span><span class="p">)</span>
<span class="n">rsvps</span><span class="p">.</span><span class="nf">not_declined</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">rsvp</span><span class="o">|</span>
<span class="no">EmailJob</span><span class="p">.</span><span class="nf">perform_later</span><span class="p">(</span><span class="s1">'PostEventSurvey'</span><span class="p">,</span> <span class="n">rsvp</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="p">)</span>
<span class="n">queue_as</span> <span class="ss">:default</span>
<span class="k">def</span> <span class="nf">perform</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
<span class="n">event</span><span class="p">.</span><span class="nf">perform_end_actions</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=695312018-01-10T18:05:24ZEregon (Benoit Daloze)
<ul></ul><p>kddeisz (Kevin Deisz) wrote:</p>
<blockquote>
<p>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:</p>
</blockquote>
<p>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 <code>self</code>.</p>
<p>To clarify my comment above: I'm not against a shorter way to define refinements+use them.<br>
But #refine as proposed is wrong: if EventEndActionsJob was a module it would stop working because then<br>
it would be the refine-just-define-refinements (current semantics) and not the refine-define-and-use-refinements you propose.</p>
<p>Or are you proposing to change the behavior of <code>refine do ... end</code> to always enable refinements after it until the end of the class/module body or the end of the file?<br>
Then there would be a compatibility risk.</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=695322018-01-10T18:22:55Zkddnewton (Kevin Newton)kddnewton@gmail.com
<ul></ul><p>I was proposing the former, which would be to have <code>refine</code> be a class method that would effectively be the same as <code>using</code> with an anonymous module. I get what you're saying about it being different between a class and a module but I'm not sure I necessary see that as a problem. Class and Module already don't have perfect parity (allocate, new, superclass) so it doesn't seem like we need to enforce that. I doubt people would be caught off guard by a change in the semantics of the method between Module and Class because it doesn't seem like it would be a common practice to be switching constants back and forth between modules and classes all the time.</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=714532018-04-11T14:45:02Zkddnewton (Kevin Newton)kddnewton@gmail.com
<ul></ul><p>I haven't contributed before so I'm not sure how to bump this ticket, but I'd like to keep pushing on this. Could someone from core take a look at this proposal? I'd love to help introduce this syntax.</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=714902018-04-16T10:03:29ZEregon (Benoit Daloze)
<ul></ul><p>I am a MRI committer. This is just my opinion, but I'm confident it is shared by other committers as well.</p>
<p>I believe changing the semantics of <code>refine</code> (to be using+Module.new+refine) based on whether the receiver is a Class or Module is not acceptable.<br>
So, we need a new name for this feature as <code>refine</code> would be too confusing with the existing semantics.</p>
<p>Changing the semantics for <code>refine</code> for all cases would be consistent, but introduce too large incompatibilities.<br>
So a new name seems the way forward, do you have a suggestion?</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=715022018-04-17T15:38:27Zkddnewton (Kevin Newton)kddnewton@gmail.com
<ul></ul><p>Thanks Benoit. A couple of suggestions would be:</p>
<pre><code>anonymous_refine
inline_refine
class_refine
refining
refine_class
</code></pre>
<p>Any of these would be fine I think - <code>anonymous_refine</code> gets across that it's creating an anonymous module that will be used. <code>inline_refine</code> is similar in that sense. <code>class_refine</code> may be confusing in that it's not going up to the singleton class and refining. <code>refining</code> is inline with Rails' <code>concerning</code>, but doesn't particularly convey special meaning. <code>refine_class</code> gets across that it's only refining a specific class, which I kind of like the best.</p>
<p>Would something like <code>refine_class</code> be amenable to the core team?</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=760492019-01-02T22:41:03Zpalkan (Vladimir Dementyev)dementiev.vm@gmail.com
<ul></ul><p>I'm also also using refinements a lot and in most cases I use "inlined" refinements, so would be glad to have a shorter syntax (and, actually, have been thinking about it for a long time and was going to propose a feature–and found this ticket).</p>
<p>I was thinking about a syntax proposed by Benoit:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">using</span> <span class="k">do</span>
<span class="n">refine</span> <span class="no">Array</span> <span class="k">do</span>
<span class="c1"># ...</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<blockquote>
<p>I believe changing the semantics of refine (to be using+Module.new+refine) based on whether the receiver is a Class or Module is not acceptable.</p>
</blockquote>
<p>What if we change it only for top-level <code>refine</code>?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># this works</span>
<span class="n">refine</span> <span class="no">String</span> <span class="k">do</span>
<span class="c1"># ...</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">Something</span>
<span class="c1"># this raises an exception</span>
<span class="n">refine</span> <span class="o">...</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">AnotherThing</span>
<span class="c1"># and this raises</span>
<span class="n">refine</span> <span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>This could be implemented just as syntactic sugar (by defining a <code>refine</code> method on vm top).</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=762432019-01-11T11:05:32Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I think that:</p>
<pre><code>using do
refine Array do
</code></pre>
<p>Is not good. It looks very strange to me.</p>
<p>The other variants, such as the original one:</p>
<pre><code>class Test
refine String do
</code></pre>
<p>or just toplevel:</p>
<pre><code>refine String do
</code></pre>
<p>would seem better to me.</p>
<p>Having a short(er) or more concise syntax would be good, in my opinion, as<br>
it may lower the "barrier of entry" for people using the functionaliy. Syntax<br>
was the major reason why I have not yet used refinements even though I<br>
agree with the possibility of doing what we can do with them; the syntax<br>
felt very clumsy to me. My point there was mostly that it is super-easy<br>
in ruby to extend/modify ruby at all times (which is a great feature), so<br>
ideally it should also be very simple to do so via refinements, which I<br>
may consider as extending behaviour while keeping a more limited scope<br>
to these changes - aka "careful changes".</p>
<p>It may be best to have matz pick a syntax, if he agrees with the feature. :)</p>
<p>(May have to then also update documentation and specification of<br>
refinements; I remember that I was once surprised thinking that I<br>
knew the original definition but upon re-reading it, I realized that<br>
my assumption was not completely correct.)</p>
<p>(On a side note, I think you added it to the recent developer meeting<br>
which already happened; may have to add it to the next one or<br>
just have it be a carry-over to the next meeting.)</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=762482019-01-11T16:04:57Zpalkan (Vladimir Dementyev)dementiev.vm@gmail.com
<ul></ul><p>shevegen (Robert A. Heiler) wrote:</p>
<blockquote>
<p>I think that:</p>
<pre><code>using do
refine Array do
</code></pre>
<p>Is not good. It looks very strange to me.</p>
</blockquote>
<p>On the other hand, it's closer to what we have to write now (<code>using(Module.new do</code>) and it doesn't have any additional constraints like being at the top level of class/module context dependent.</p>
<blockquote>
<p>The other variants, such as the original one:</p>
<pre><code>class Test
refine String do
</code></pre>
</blockquote>
<p>The problem with this approach has been already <a href="https://bugs.ruby-lang.org/issues/14344#note-7" class="external">mentioned by Benoit</a>: the behaviour of <code>refine</code> changes depending on whether we're in the module or class context.</p>
<p>IMO, it would introduce even more confusion. And the goal of this feature, as it has been said, is to lower the barrier.</p>
<blockquote>
<p>(On a side note, I think you added it to the recent developer meeting<br>
which already happened)</p>
</blockquote>
<p>Oops, I got lost in time) thanks for noticing!</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=767122019-02-07T06:35:39Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>I understand the need. But I cannot accept the proposed syntax for two reasons.</p>
<p>(1) the original proposal using <code>refine</code> for classes, which is confusing with <code>refine</code> in refinement modules.<br>
(2) the modified syntax <code>using do</code> is also confusing. The scope of refinement may be in the block or the surrounding scope.</p>
<p>Matz.</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=767412019-02-07T21:13:25Zpalkan (Vladimir Dementyev)dementiev.vm@gmail.com
<ul></ul><p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>I understand the need. But I cannot accept the proposed syntax for two reasons.</p>
<p>(1) the original proposal using <code>refine</code> for classes, which is confusing with <code>refine</code> in refinement modules.<br>
(2) the modified syntax <code>using do</code> is also confusing. The scope of refinement may be in the block or the surrounding scope.</p>
<p>Matz.</p>
</blockquote>
<p>Thanks!</p>
<p>Since this has been marked as rejected, should we propose alternatives in a new ticket or could we continue discussing here?</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=767422019-02-07T22:14:53Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/14299">@palkan (Vladimir Dementyev)</a>, yes, post a new issue, when you come up with a new idea.</p>
<p>Matz.</p> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=819232019-10-06T19:15:09ZEregon (Benoit Daloze)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/16241">Feature #16241</a>: Shorter syntax for anonymous refinements</i> added</li></ul> Ruby master - Feature #14344: refine at class levelhttps://redmine.ruby-lang.org/issues/14344?journal_id=943832021-10-28T11:14:15ZEregon (Benoit Daloze)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-6 priority-4 priority-default closed" href="/issues/11919">Feature #11919</a>: Passing a module directly </i> added</li></ul>