https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?14253458252012-08-07T11:39:32ZRuby Issue Tracking SystemRuby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=286942012-08-07T11:39:32ZTom Wardroptom@tomwardrop.com
<ul></ul><p>=begin<br>
The formatting obviously didn&#39;t work. Try this one:</p>
<p>Quite often in Ruby, I find myself doing something like: (({my_var[:foo][:bar] = my_var[:foo][:bar].to_i})) or (({obj.foo.bar = obj.foo.bar.to_i})). Realising this, I thought of what would be a fairly nice shorthand syntax for this, which could be: (({my_var[:foo][:bar] .= to_i})). How this works should be pretty self-explanatory. The (({.=})) operator works exactly like any other assignment operator of this nature.</p>
<p>Would be nice to see this in Ruby 2.0. Wondering what others think of this?<br>
=end</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=286972012-08-07T12:37:37ZNobuyoshi Nakadanobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a href="/journals/diff/28697?detail_id=21190" title="View differences">diff</a>)</li></ul> Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=287022012-08-07T16:05:44ZThomas Sawyer
<ul></ul><p>=begin<br>
Does <code>.</code> effectively become a &quot;call dispatch operator&quot; then? Could one write:</p>
<p>a.send(&#39;.&#39;, :foo)</p>
<p>I like the idea. But to complete the comparison to other operators, it made me think <code>.</code> would be some sort of method.<br>
=end</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=287062012-08-07T20:29:58ZTom Wardroptom@tomwardrop.com
<ul></ul><p>Yeah, I thought about making &quot;.&quot; a method, but I don&#39;t think that would be advantageous. If you were to overload &quot;.&quot;, perhaps to intercept all method calls (can&#39;t think of any other reasons besides as hook method), it wouldn&#39;t really work as there are other means to call a method, such as #send and #__send__, unless they use &quot;.&quot; internally? I&#39;d prefer to see a new hook instead, something like #method_called(name) if that was your use case.</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=287162012-08-08T07:19:13ZKurt Stephens
<ul></ul><p>I like the idea, iff:</p>
<p>obj.foo.bar.baz .= to_i</p>
<p>behaves as:</p>
<p>begin<br>
temp = obj.foo.bar<br>
temp.baz = temp.baz.to_i<br>
end</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=287182012-08-08T10:58:17ZTom Wardroptom@tomwardrop.com
<ul></ul><p>Yes, that&#39;s exactly how it would behave. To rewrite your example to make sure I understood it correctly:</p>
<pre>obj.foo.bar.baz = obj.foo.bar.baz.to_i
</pre>
<p>becomes </p>
<pre>obj.foo.bar.baz .= to_i
</pre> Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=287392012-08-09T09:31:40ZKurt Stephens
<ul></ul><p>If we want lexical multiplicity to equal evaluation multiplicity...</p>
<p>Should:</p>
<p>obj.foo.bar[baz.daz] .= to_i</p>
<p>behave as?:</p>
<p>begin<br>
temp1 = obj.foo.bar<br>
temp2 = baz.daz<br>
temp1[temp2] = temp1[temp2].to_i<br>
end</p>
<p>... Since the following evaluates &quot;obj.bar.baz&quot; and &quot;baz.daz&quot; only once:</p>
<p>class Obj<br>
def foo<br>
puts &quot;#{self}#foo&quot;<br>
@foo ||= Foo.new<br>
end<br>
end<br>
class Foo<br>
def bar<br>
puts &quot;#{self}#bar&quot;<br>
@bar ||= { }<br>
end<br>
end<br>
class Baz<br>
def daz<br>
puts &quot;#{self}#daz&quot;<br>
:x<br>
end<br>
end<br>
obj = Obj.new<br>
baz = Baz.new<br>
obj.foo.bar[baz.daz] = 1</p>
<p>... Similarly for below:</p>
<p>obj.foo.bar[baz.daz] ||= 1</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=290902012-08-29T10:08:09ZTom Wardroptom@tomwardrop.com
<ul></ul><p>@kstephens, yes, that would be the expected result I believe. I don&#39;t think anyone would expect baz.daz to be called twice in that instance. Principle of least surprise applies here.</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=317082012-10-27T07:32:50ZKoichi Sasada
<ul><li><strong>Assignee</strong> set to <i>Yusuke Endoh</i></li></ul><p>mame-san, could you judge this ticket?</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=317742012-10-27T12:52:21ZYusuke Endohmame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> changed from <i>Yusuke Endoh</i> to <i>Yukihiro Matsumoto</i></li><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>Next Major</i></li></ul><p>It requires matz&#39;s approval.</p>
<p>My personal comment: I agree, in fact I have thought the same thing<br>
(and as I recall, unak implemented a patch).<br>
But &quot;..= to_i&quot; resembles a variable named `to_i&#39;. I wonder if matz<br>
is interested or not.</p>
<h2></h2>
<p>Yusuke Endoh <a href="mailto:mame@tsg.ne.jp">mame@tsg.ne.jp</a></p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=371222013-02-27T09:07:25ZTom Wardroptom@tomwardrop.com
<ul></ul><p>=begin<br>
If there are concerns about using an identifier after &quot;.=&quot;, then perhaps a symbol could be used instead:</p>
<p>(({obj.foo.bar.baz .= :to_i}))</p>
<p>That would be somewhat consistant with the alternate block syntax (not sure what it&#39;s called):</p>
<p>(({[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;].each &amp;:upcase!}))<br>
=end</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=371362013-02-27T15:48:09ZMartin Dürstduerst@it.aoyama.ac.jp
<ul></ul><p>wardrop (Tom Wardrop) wrote:</p>
<blockquote>
<p>=begin<br>
Quite often in Ruby, I find myself doing something like: (({my_var[:foo][:bar] = my_var[:foo][:bar].to_i})) or (({obj.foo.bar = obj.foo.bar.to_i})). Realising this, I thought of what would be a fairly nice shorthand syntax for this, which could be: (({my_var[:foo][:bar] .= to_i})).</p>
</blockquote>
<p>What about introducing to_i!. This would look more like Ruby.</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=371472013-02-27T20:32:51ZNobuyoshi Nakadanobu@ruby-lang.org
<ul></ul><p>to_i! needs Object#become.</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=371652013-02-28T06:23:24ZYura Sokolovfunny.falcon@gmail.com
<ul></ul><p>=begin<br>
May be:</p>
<p>(({obj.foo.bar.baz = .to_i }))<br>
(({obi.foo[bar.baz] = .to_s(16) }))</p>
<p>While this not consistent with ||= , it looks readable, imho.</p>
<p>Or combine both variants:</p>
<p>(({obj.foo.bar.baz .= .to_i }))<br>
(({obi.foo[bar.baz] .= .to_s(16) }))</p>
<p>( looks like morse ;) )<br>
27.02.2013 4:07 пользователь &quot;wardrop (Tom Wardrop)&quot; <a href="mailto:tom@tomwardrop.com">tom@tomwardrop.com</a><br>
написал:</p>
<blockquote>
<p>Issue <a href="https://bugs.ruby-lang.org/issues/6841" class="issue tracker-2 status-2 priority-4 priority-default" title="Shorthand for Assigning Return Value of Method to Self (Assigned)">#6841</a> has been updated by wardrop (Tom Wardrop).</p>
<p>=begin<br>
If there are concerns about using an identifier after &quot;.=&quot;, then perhaps a<br>
symbol could be used instead:</p>
<p>(({obj.foo.bar.baz .= :to_i}))</p>
<p>That would be somewhat consistant with the alternate block syntax (not<br>
sure what it&#39;s called):</p>
<p>(({[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;].each &amp;:upcase!}))</p>
<a name="end"></a>
<h2 >=end<a href="#end" class="wiki-anchor">&para;</a></h2>
<p>Feature <a href="https://bugs.ruby-lang.org/issues/6841" class="issue tracker-2 status-2 priority-4 priority-default" title="Shorthand for Assigning Return Value of Method to Self (Assigned)">#6841</a>: Shorthand for Assigning Return Value of Method to Self<br>
<a href="https://bugs.ruby-lang.org/issues/6841#change-37122">https://bugs.ruby-lang.org/issues/6841#change-37122</a></p>
<p>Author: wardrop (Tom Wardrop)<br>
Status: Assigned<br>
Priority: Normal<br>
Assignee: matz (Yukihiro Matsumoto)<br>
Category: core<br>
Target version: Next Major</p>
<p>=begin<br>
Quite often in Ruby, I find myself doing something like:<br>
(({my_var[:foo][:bar] = my_var[:foo][:bar].to_i})) or (({obj.foo.bar =<br>
obj.foo.bar.to_i})). Realising this, I thought of what would be a fairly<br>
nice shorthand syntax for this, which could be: (({my_var[:foo][:bar] .=<br>
to_i})). How this works should be pretty self-explanatory. The (({.=}))<br>
operator works exactly like any other assignment operator of this nature.</p>
<p>Would be nice to see this in Ruby 2.0. Wondering what others think of this?<br>
=end</p>
<h2></h2>
<p><a href="http://bugs.ruby-lang.org/">http://bugs.ruby-lang.org/</a></p>
</blockquote>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=371752013-02-28T11:23:19ZNobuyoshi Nakadanobu@ruby-lang.org
<ul></ul><p>(13/02/28 6:16), Юрий Соколов wrote:</p>
<blockquote>
<p>=begin</p>
</blockquote>
<p>You can&#39;t write in RD mode via e-mail.</p>
<blockquote>
<p>May be:</p>
<p>(({obj.foo.bar.baz = .to_i }))<br>
(({obi.foo[bar.baz] = .to_s(16) }))</p>
<p>While this not consistent with ||= , it looks readable, imho.</p>
</blockquote>
<p>It&#39;s different from ||= etc, so it should not consistent with them.</p>
<p>Rather,</p>
<p>obj.foo.bar.baz += .to_i<br>
obi.foo[bar.baz] *= .to_s(16)</p>
<p>might be useful.</p>
<p>-- <br>
Nobu Nakada</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=371822013-02-28T14:33:21ZNathan Zookblogger@pierian-spring.net
<ul></ul><p><a href="http://en.wikipedia.org/wiki/Law_of_Demeter">http://en.wikipedia.org/wiki/Law_of_Demeter</a></p>
<p>The term that I&#39;ve heard to describe obj.foo.bar.baz is &quot;train wreck&quot;. Encouraging such seems to me to be problematic.</p>
<p>I am NOT saying that the transformation/normalization is wrong, just that it is being handled a the wrong level. Think about it. To make this call, you have to know about obj (which I&#39;m assuming you should). You have to know foo about obj. You have to know bar about obj.foo. You have to know baz about obj.foo.bar. And you have to know that you want baz to be an integer. If that is not being overly intimate with your arguments, I wouldn&#39;t know what is.</p>
<p>If you are doing this a lot, then most likely you are normalizing a data structure. That is, your code looks like:</p>
<p>obj.foo.bar.baz = obj.foo.bar.baz.to_i<br>
obj.for.bar.boz = obj.foo.bar.boz.to_f<br>
...</p>
<p>If so, the to_i part is CERTAINLY not the problem! My first question would be &quot;where does obj come from&quot;? Most likely some sort of naive deserializing process. Many deserializers are in fact quite sophisticated, and perhaps a more informed usage would bypass ever creating the object in the undesirable form. </p>
<p>I have a hard time imagining any other way that one would ever get to the point that obj.foo.bar.baz = obj.foo.bar.baz.to_i would make sense. But perhaps I have missed something.</p>
Ruby trunk - Feature #6841: Shorthand for Assigning Return Value of Method to Selfhttps://bugs.ruby-lang.org/issues/6841?journal_id=381562013-04-03T15:55:30ZTom Wardroptom@tomwardrop.com
<ul></ul><p>=begin<br>
(({Regarding obj.foo.bar.baz = .to_i})) syntax, with the dot prefix on the ((|to_i|)), another proposal has been made further down the discussion for issue <a href="https://bugs.ruby-lang.org/issues/8191" class="issue tracker-2 status-2 priority-4 priority-default" title="Short-hand syntax for duck-typing (Assigned)">#8191</a>. The idea is that an expression beginning with a dot could be an inferred method call on the result of the last expression.</p>
<p>Just consider that before using the dot prefix syntax for anything else.<br>
=end</p>