https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico2012-04-28T07:11:56ZRuby Issue Tracking SystemRuby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262682012-04-28T07:11:56ZYusuke Endohmame@ruby-lang.org
<ul><li><strong>Assignee</strong> set to <i>Yukihiro Matsumoto</i></li><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li></ul> Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262702012-04-28T08:28:03ZMartin Dürstduerst@it.aoyama.ac.jp
<ul></ul><p><code>#identity</code> (or whatever it&#39;s called) is quite important in functional languages. It&#39;s handy to pass to another function that e.g. uses it as an argument to map,... So I think it&#39;s a good idea to add it.</p>
<p>The name should be identity or id (Haskell) or some such.</p>
<p>Making self public sounds impressive, but first, it doesn&#39;t expose anything new (it&#39;s the object itself, which is already accessible) an second, I feel it&#39;s the wrong name, because <code>#idenity</code> essentially only makes sense in locations in a program where the actual object isn&#39;t directly around.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262762012-04-28T10:05:34ZYui NARUSEnaruse@airemix.jp
<ul></ul><p>Use <strong>id</strong>.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262832012-04-28T16:02:31ZCharles Nutterheadius@headius.com
<ul></ul><p>At first I found this laughable, but then I realized there&#39;s no clear method you can call against an object that simply returns the object. It is a small thing, but turns out to be very useful.</p>
<p>For example, if you want a chain of method calls that successively transformation some data. Those transformations, in the form of folds or filters or what have you, may accept an object and return some new object by calling a method. In that case, the simplest transformation is to simply return the object unmodified. There is no such method on Object or BasicObject right now, so rather than having the uniformity of a method call you would need a special filter form that calls nothing and returns its one argument. Identity is an easy, simple functional form that should be available.</p>
<p>I would suggest it should be called <strong>self</strong> or similar, since it is a key core method nobody should ever override. But I do think there is utility in having it always available.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262842012-04-28T16:06:28ZCharles Nutterheadius@headius.com
<ul></ul><p>Actually, I just realized there is already a method you can call to just return the method itself, albeit in a really gross way: <code>#tap</code>.</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">obj.tap{} <span class="comment">#returns obj; block is required and invoked</span>
</span></code></pre> Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262862012-04-28T18:31:30ZIlya Vorontsovprijutme4ty@gmail.com
<ul></ul><p><code>enum.map(&amp;:identity)</code> can be replaced with <code>enum.to_a</code><br>
But I think <code>Object#identity</code> is useful. <code>Object#tap</code> requires a block. It&#39;s not an option to use it in many cases. I can&#39;t imagine how to replace <code>enum.group_by(&amp;:identity)</code> with <code>tap</code> instead of <code>identity</code>.</p>
<p>I think that tap method can be improved to be used without block yielding self. But for readability it&#39;d be however aliased to <code>identity</code>.</p>
<p><code>__self__</code> looks like a method every programmer&#39;d avoid.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262962012-04-29T00:10:20ZYukihiro Matsumotomatz@ruby-lang.org
<ul></ul><p><code>__id__</code> returns <code>object_id</code> number, identity here is supposed to return itself.<br>
I agree with introducing method to return self, but not fully satisfied with the name &#39;<code>identity</code>&#39;.<br>
Any opinion?</p>
<p>Matz.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=262992012-04-29T00:53:57ZThomas Sawyer
<ul></ul><p>Public #self seems like the most obvious choice. Is there some reason not to use it?</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=263002012-04-29T00:59:18ZAlex Youngalex@blackkettle.org
<ul></ul><p>On 28/04/2012 16:10, matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="public #self (Closed)" href="https://bugs.ruby-lang.org/issues/6373">#6373</a> has been updated by matz (Yukihiro Matsumoto).</p>
<p><code>__id__</code> returns <code>object_id</code> number, identity here is supposed to return itself.<br>
I agree with introducing method to return self, but not fully satisfied with the name &#39;<code>identity</code>&#39;.<br>
Any opinion?</p>
</blockquote>
<p>&quot;<code>itself</code>&quot;?</p>
<p>-- <br>
Alex</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=263342012-04-30T23:20:08ZPablo Herreropablodherrero@gmail.com
<ul></ul><p>What about if we borrow #yourself message name from Smalltalk?</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=263362012-05-01T01:10:54ZAlexey Muranov
<ul></ul><p>Thomas, i think an argument against public #self is that &#39;self&#39; is a reserved word, which moreover is used more as an object name than as a method name. So &#39;self&#39; would need to stop being a keyword and become a public &#39;predefined&#39; method of BasicObject (it cannot be defined without the &#39;self&#39; keyword, i guess).</p>
<p>I like #itself or #yourself, but i do not know which one is a proper way to talk to my objects.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=263372012-05-01T02:03:31ZThomas Sawyer
<ul></ul><p>Like many of Ruby&#39;s keywords, it can still be used to define a public method:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay"><span class="keyword">class</span> <span class="class">X</span>
<span class="keyword">def</span> <span class="function">self</span>; <span class="string"><span class="delimiter">&quot;</span><span class="content">x</span><span class="delimiter">&quot;</span></span>; <span class="keyword">end</span>
<span class="keyword">end</span>
x = <span class="constant">X</span>.new
x.self <span class="comment">#=&gt; &quot;x&quot;</span>
</span></code></pre> Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=265322012-05-08T22:16:16ZRobert A. Heilershevegen@gmail.com
<ul></ul><p>Perhaps Smalltalk has the best suggestion. :)</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=266212012-05-15T03:41:02ZAlexey Muranov
<ul></ul><p>Another option: <code>#the_self</code>. The same number of symbols as in <code>#yourself</code>, but harder to type :(.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=266222012-05-15T03:59:10ZBenoit Daloze
<ul></ul><p>On 28 April 2012 17:54, Alex Young <a href="mailto:alex@blackkettle.org">alex@blackkettle.org</a> wrote:</p>
<blockquote>
<p>&quot;<code>itself</code>&quot;?</p>
</blockquote>
<p>I agree, <code>#itself</code> is the best to me.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=273852012-06-24T03:34:24ZMarc-Andre Lafortuneruby-core@marc-andre.ca
<ul></ul><p>I second the addition of <code>Object#self</code>.</p>
<p>For the objection that <code>self</code> is a keyword, so is <code>class</code>. And there wouldn&#39;t ever be a need to call <code>self.self</code> :-)</p>
<p>Do we need a slide-show for this?</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=277262012-07-03T04:15:06ZThomas Sawyer
<ul></ul><p>Does it really need a slide? Does someone at developers meeting want to bring it up? It&#39;s such a simple thing. It&#39;s probably a one line addition to code to make <code>self</code> available as public method. Only question is name, which seems to me, why have different public name than private name? Go with #self. But if necessary for public/private names to differ, everyone seems okay with #itself.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=279272012-07-11T00:38:42ZAlexey Muranov
<ul></ul><p>I&#39;ve heard that the underscore <code>_</code> is commonly used for ignored block variables. Maybe this &quot;public self&quot; can be considered as an &quot;ignored method&quot;, and called <code>Object#_</code>?</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=279292012-07-11T04:37:14ZThomas Sawyer
<ul></ul><p><code>_</code> is used by irb. Also, I don&#39;t really see why. Code would look much more &quot;perlish&quot; using <code>_</code>.</p>
<p>Please, what&#39;s wrong with public #self?</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=279312012-07-11T05:55:54ZAlexey Muranov
<ul></ul><p>trans (Thomas Sawyer) wrote:</p>
<blockquote>
<p>Please, what&#39;s wrong with public #self?</p>
</blockquote>
<p>Nothing, just was wondering how to use <code>_</code> :). I didn&#39;t know IRB uses it.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=285422012-07-30T03:42:10ZMichael Kohlcitizen428@gmail.com
<ul></ul><p>FWIW, I&#39;m the one who suggested this method as an addition to Facets, mainly for the reason headius mentions above, it&#39;s the simplest filter available. I&#39;m still torn on the name, but for some reason #self didn&#39;t seem right. For my own extension library I finally went with #it.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=286822012-08-07T02:47:25ZMichael Kohlcitizen428@gmail.com
<ul></ul><p>i also found a previous issue where this behavior would come in handy: <a href="http://bugs.ruby-lang.org/issues/2172">http://bugs.ruby-lang.org/issues/2172</a></p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=286852012-08-07T06:00:27ZAlexey Muranov
<ul></ul><p>How about merging this with feature request <a class="issue tracker-2 status-1 priority-4 priority-default" title="Object#yield_self (Open)" href="https://bugs.ruby-lang.org/issues/6721">#6721</a> for #yield_self?</p>
<p>Object#self can optionally accept a block, yield self to the block if block given, and return the result of the block. What do you think?</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=286872012-08-07T06:20:10ZThomas Sawyer
<ul></ul><p>Strikes me as a very good idea! I forgot about that. In Facets it is called <code>#ergo</code>. Essentially,</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay"><span class="keyword">def</span> <span class="function">ergo</span>
<span class="keyword">return</span> <span class="keyword">yield</span>(<span class="predefined-constant">self</span>) <span class="keyword">if</span> block_given?
<span class="predefined-constant">self</span>
<span class="keyword">end</span>
</span></code></pre>
<p>Call it <code>#self</code> instead and we get two features for the price of none!</p>
<p>Not sure if should take additional <code>*args</code> or not, but it could.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=287542012-08-10T14:22:36ZBoris Stitnickyboris@iis.sinica.edu.tw
<ul></ul><p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p><code>__id__</code> returns <code>object_id</code> number, identity here is supposed to return itself.<br>
I agree with introducing method to return self, but not fully satisfied with the name &#39;<code>identity</code>&#39;.<br>
Any opinion?</p>
<p>Matz.</p>
</blockquote>
<p>I did some thinking, and there is hardly anything better than &#39;<code>identity</code>&#39;.<br>
&#39;<code>identity_function</code>&#39; would be hypercorrect, and &#39;id&#39; is commonly understood<br>
as identifier. In my personal library, I use &#39;ɪ&#39; (small cap Unicode I) for<br>
identity mapping, though...</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=287582012-08-10T16:47:13ZAlexey Muranov
<ul></ul><p>boris_stitnicky (Boris Stitnicky) wrote:</p>
<blockquote>
<p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p><code>__id__</code> returns <code>object_id</code> number, identity here is supposed to return itself.<br>
I agree with introducing method to return self, but not fully satisfied with the name &#39;<code>identity</code>&#39;.<br>
Any opinion?</p>
<p>Matz.</p>
</blockquote>
<p>I did some thinking, and there is hardly anything better than &#39;<code>identity</code>&#39;.<br>
&#39;<code>identity_function</code>&#39; would be hypercorrect, and &#39;id&#39; is commonly understood<br>
as identifier. In my personal library, I use &#39;ɪ&#39; (small cap Unicode I) for<br>
identity mapping, though...</p>
</blockquote>
<p>ID as an identifier or a piece of identification and Id as the <em>identity function</em> are two different meanings, as far as i understand. Also it does not look to me like &quot;method&quot; and &quot;function&quot; have exactly the same semantics.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=287632012-08-10T23:53:11ZAnonymous
<ul></ul><p>On Fri, Aug 10, 2012 at 4:25 PM, matz (Yukihiro Matsumoto)<br>
<a href="mailto:m...@ruby-lang.org">m...@ruby-lang.org</a> wrote:</p>
<blockquote>
<p><code>__id__</code> returns <code>object_id</code> number, identity here is supposed to return itself.<br>
I agree with introducing method to return self, but not fully satisfied with the name &#39;<code>identity</code>&#39;.<br>
Any opinion?</p>
</blockquote>
<p>How about allowing <code>Object#tap</code> to take no block, simply returning self?<br>
This syntactic sugar would allow</p>
<pre> this.tap
</pre>
<p>instead of</p>
<pre> this.tap {}
</pre>
<p>If a new method must be added, please for the love of sanity don&#39;t<br>
call it id or identity or identifier. This space already chronically<br>
overloaded by frameworks, the need for entities to have publically<br>
visible identity notwithstanding.</p>
<p>Perhaps <code>__object__</code>, which would feel natural to anyone already<br>
familiar with <code>__id__</code>? Failing that, <code>Object#object</code>, although I&#39;d expect<br>
this to break a fair amount of existing code. :-(</p>
<p>Ciao,<br>
Sheldon.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=287712012-08-11T05:17:17ZThomas Sawyer
<ul></ul><p>Why no answer for: &quot;Why not just public #self&quot;? Why add YAMS? </p>
<p>(YAM = Yet Another Method)</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=320352012-10-31T02:23:57ZYukihiro Matsumotomatz@ruby-lang.org
<ul><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>next minor</i></li><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Feedback</i></li></ul><p>The point is when we see the code like:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">[<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">3</span>,<span class="integer">4</span>,<span class="integer">5</span>,<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">2</span>,<span class="integer">3</span>].group_by(&amp;<span class="symbol">:self</span>)
</span></code></pre>
<p>sometimes it would be less intuitive that self refers elements in the array, not self in the scope.<br>
I think this cause YAM syndrome here. We haven&#39;t met name consensus yet (as usual), so I postpone to next minor.</p>
<p>Matz.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=397302013-06-06T02:45:55ZAlexey Chernenkovlaise@pisem.net
<ul></ul><p>Quote: &quot;I think that tap method can be improved to be used without block yielding self.&quot;</p>
<p>+1</p>
<p>It is a VERY usefull feature! Can&#39;t understand why #tap still need to be used with block only.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=397322013-06-06T06:54:31ZMatthew Kerwinmatthew@kerwin.net.au
<ul></ul><p>laise (Alexey Chernenkov) wrote:</p>
<blockquote>
<p>Quote: &quot;I think that tap method can be improved to be used without block yielding self.&quot;</p>
<p>+1</p>
<p>It is a VERY usefull feature! Can&#39;t understand why #tap still need to be used with block only.</p>
</blockquote>
<p>Because it&#39;s called &quot;tap.&quot; Tap doesn&#39;t &quot;return self&quot;, it taps into an execution flow, extracting an intermediate value for inspection without interrupting the original flow. The analogy is literally tapping a hole in a pipe, to extract liquid samples at various phases in a process. (Also think of tapping a phone line.) Changing its semantics to a straight-up &quot;returns self&quot; method would just make it idiosyncratic, instead of metaphoric.</p>
<p>Matz wrote:</p>
<blockquote>
<p>The point is when we see the code like:</p>
<p>[1,2,3,4,5,1,2,2,3].group_by(&amp;:self)</p>
<p>sometimes it would be less intuitive that self refers elements in the array, not self in the scope.</p>
</blockquote>
<p>I have trouble imagining such a scenario. I actually think the <code>some_array.group_by(&amp;:self)</code> example is a strong plus for this feature.</p>
<p>I&#39;m also not entirely convinced it&#39;s really YAM, since &#39;self&#39; is already a word in the language, all we&#39;re doing is pushing it from keyword to keyword+method, i.e. making it more easily accessible from outside the instance. Even more so since this use-case is really the only place it will ever show up; if there was another way to to_proc the &#39;self&#39; keyword to make it easy to pass to a method like group_by I&#39;d be for that as well/instead.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=397662013-06-07T15:06:30ZBoris Stitnickyboris@iis.sinica.edu.tw
<ul></ul><p>trans (SYSTEM ERROR) wrote:</p>
<blockquote>
<p>Why no answer for: &quot;Why not just public <code>#self</code>&quot;? Why add YAMS?</p>
<p>(YAM = Yet Another Method)</p>
</blockquote>
<p>Because explicit identity element is a VIP of the functional space.<br>
Summary of the discussion thus far + my opinions follow:</p>
<table><thead>
<tr>
<th>Proposal:</th>
<th>My opinion:</th>
<th>Remark:</th>
</tr>
</thead><tbody>
<tr>
<td><code>#self</code></td>
<td>+1</td>
<td>proposed and defended by OP, objected by Matz</td>
</tr>
<tr>
<td><code>#identity</code></td>
<td>+1</td>
<td>strong objection by Anonymous</td>
</tr>
<tr>
<td><code>#__id__</code></td>
<td>0</td>
<td></td>
</tr>
<tr>
<td><code>#__self__</code></td>
<td>-1</td>
<td></td>
</tr>
<tr>
<td><code>#yourself</code></td>
<td>-1</td>
<td></td>
</tr>
<tr>
<td><code>#itself</code></td>
<td>+3</td>
<td></td>
</tr>
<tr>
<td><code>#_</code></td>
<td>-1</td>
<td></td>
</tr>
<tr>
<td><code>#the_self</code></td>
<td>-1</td>
<td></td>
</tr>
<tr>
<td><code>#it</code></td>
<td>-1</td>
<td></td>
</tr>
<tr>
<td><code>#__object__</code></td>
<td>-1</td>
<td></td>
</tr>
<tr>
<td><code>#id</code></td>
<td>-1</td>
<td>not proposed, Anonymous strongly objects this option</td>
</tr>
<tr>
<td><code>#tap</code></td>
<td>-2</td>
<td>me and phluid61 strongly object</td>
</tr>
</tbody></table>
<p>I used to favor <code>#identity</code>, but today, I favor <code>#itself</code>. I already advertised this<br>
method, and <code>#ergo</code> method (<a class="issue tracker-2 status-1 priority-4 priority-default" title="Object#yield_self (Open)" href="https://bugs.ruby-lang.org/issues/6721">#6721</a>) on <a href="http://stackoverflow.com/questions/16932711">http://stackoverflow.com/questions/16932711</a> ,<br>
where these features are in demand. To make the naming decision more difficult,<br>
let me append Freudian <code>#ego</code> to the list, which would go nicely with <code>#ergo</code>.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=401012013-06-24T03:51:27ZCharles Nutterheadius@headius.com
<ul></ul><p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>laise (Alexey Chernenkov) wrote:</p>
<blockquote>
<p>It is a VERY usefull feature! Can&#39;t understand why #tap still need to be used with block only.</p>
</blockquote>
<p>Because it&#39;s called &quot;tap.&quot; Tap doesn&#39;t &quot;return self&quot;, it taps into an execution flow, extracting an intermediate value for inspection without interrupting the original flow. The analogy is literally tapping a hole in a pipe, to extract liquid samples at various phases in a process. (Also think of tapping a phone line.) Changing its semantics to a straight-up &quot;returns self&quot; method would just make it idiosyncratic, instead of metaphoric.</p>
</blockquote>
<p>I still like <code>#tap</code>.</p>
<ol>
<li>It would only require removal of the block-yielding requirement.</li>
<li>It&#39;s functionally equivalent to tapping into execution flow but doing nothing, as in tap {}. I think the objection that it no longer means we&#39;re tapping execution flow is a bit pedantic; we <em>are</em> tapping execution flow, but the only operation we seek is the reference to the object.</li>
</ol>
<p>My favorite remains #self, but I can appreciate objections, especially confusion over &quot;self&quot; and &quot;obj.self&quot; and especially &quot;self.self&quot; if #self can be overridden. It does have the advantage that there&#39;s probably fewer libraries that define their own &quot;self&quot; method than any other suggestion here.</p>
<p>Other systems I know would use #identity. It also maps to functional programming language for a function that just returns its sole argument (in this case, the 0th argument, the object itself). I would support <code>#identity</code> as well.</p>
<blockquote>
<p>Matz wrote:</p>
<blockquote>
<p>The point is when we see the code like:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">[<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">3</span>,<span class="integer">4</span>,<span class="integer">5</span>,<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">2</span>,<span class="integer">3</span>].group_by(&amp;<span class="symbol">:self</span>)
</span></code></pre>
<p>sometimes it would be less intuitive that self refers elements in the array, not self in the scope.</p>
</blockquote>
<p>I have trouble imagining such a scenario. I actually think the <code>some_array.group_by(&amp;:self)</code> example is a strong plus for this feature.</p>
<p>I&#39;m also not entirely convinced it&#39;s really YAM, since &#39;self&#39; is already a word in the language, all we&#39;re doing is pushing it from keyword to keyword+method, i.e. making it more easily accessible from outside the instance. Even more so since this use-case is really the only place it will ever show up; if there was another way to <code>to_proc</code> the &#39;self&#39; keyword to make it easy to pass to a method like group_by I&#39;d be for that as well/instead.</p>
</blockquote>
<p>Another option based on matz&#39;s objection: #reference. We want a method that returns the reference to the object we&#39;re calling against. #reference seems logical.</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">[<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">3</span>,<span class="integer">4</span>,<span class="integer">5</span>,<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">2</span>,<span class="integer">3</span>].group_by(&amp;<span class="symbol">:reference</span>)
</span></code></pre>
<p>Variations on this might be <code>#self_reference</code>, <code>#self_ref</code>, <code>#selfref</code>. Also <code>#self_object</code>, <code>#self_obj</code>, <code>#selfobj</code>.</p>
<p><code>#self</code> and <code>#identity</code> are probably the most likely to be guessed by a new user.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=401022013-06-24T04:07:17ZCharles Nutterheadius@headius.com
<ul></ul><p>Another argument why &quot;<code>tap</code>&quot; is fine...</p>
<p>If <code>tap</code> were defined in a functional style, it would be simply</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay"><span class="keyword">def</span> <span class="function">tap</span>(obj, &amp;block)
block.call(obj)
obj
<span class="keyword">end</span>
</span></code></pre>
<p>Anywhere you can pass a function you should be able to pass a no-op function, so <code>tap</code> could be defined as</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay"><span class="keyword">def</span> <span class="function">tap</span>(obj, &amp;block)
block = proc{} <span class="keyword">unless</span> block
block.call(obj)
obj
<span class="keyword">end</span>
</span></code></pre>
<p>So defining <code>tap</code> such that it defaults to a no-op function (i.e. does not yield if block not given) seems perfectly valid to me.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=401072013-06-24T09:02:12ZMatthew Kerwinmatthew@kerwin.net.au
<ul></ul><p>headius (Charles Nutter) wrote:</p>
<blockquote>
<p>Another option based on matz&#39;s objection: <code>#reference</code>. We want a method that returns the reference to the object we&#39;re calling against. <code>#reference</code> seems logical.</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">[<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">3</span>,<span class="integer">4</span>,<span class="integer">5</span>,<span class="integer">1</span>,<span class="integer">2</span>,<span class="integer">2</span>,<span class="integer">3</span>].group_by(&amp;<span class="symbol">:reference</span>)
</span></code></pre></blockquote>
<p>+1. It&#39;s sensible (i.e. anyone who knows OOP knows what &#39;reference&#39; means), there&#39;s no overloading of names, and the intention is clear.</p>
<blockquote>
<p>So defining tap such that it defaults to a no-op function (i.e. does not yield if block not given) seems perfectly valid to me.</p>
</blockquote>
<p>I know I&#39;m throwing a lot of paint at this shed, but while I agree that a default noop <code>#tap</code> is valid, I still strongly believe it makes <code>ary.group_by(&amp;:tap)</code> seem like voodoo. I like <code>#reference</code> a lot.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=401082013-06-24T09:37:23ZCharlie Somervillecharliesome@ruby-lang.org
<ul></ul><p>I think out of all the options proposed, &#39;<code>identity</code>&#39; is the most readable/quickly understandable.</p>
<p>For example, I think the use of &#39;<code>identity</code>&#39; reads very nicely in <code>[1,2,3,4].group_by(&amp;:identity)</code></p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=401092013-06-24T11:07:38ZMatthew Kerwinmatthew@kerwin.net.au
<ul></ul><p>charliesome (Charlie Somerville) wrote:</p>
<blockquote>
<p>I think out of all the options proposed, &#39;identity&#39; is the most readable/quickly understandable.</p>
<p>For example, I think the use of &#39;<code>identity</code>&#39; reads very nicely in <code>[1,2,3,4].group_by(&amp;:identity)</code></p>
</blockquote>
<p>Except that <code>#identity</code> seems to imply the same thing as <code>#__id__</code> , and <code>&quot;a&quot;.__id__</code> is not necessarily == <code>&quot;a&quot;.__id__</code> , as Matz said earlier.</p>
<p>The advantage of using <code>#reference</code> is that there&#39;s no existing method or concept we&#39;re overloading; there are no such things as &quot;reference&quot; objects (or &quot;pointers&quot;) in Ruby -- it is understood that all references are automagically dereferenced when operated on -- so returning the references and then comparing them should be understood by most rubyists as effectively the same as comparing the objects directly (whatever &quot;directly&quot; means).</p>
<p>tl;dr:<br>
Defining <code>#identity</code> that conflicts with <code>#__id__</code> is confusing, however slightly it might be.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=401232013-06-25T07:24:36ZCharles Nutterheadius@headius.com
<ul></ul><p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>charliesome (Charlie Somerville) wrote:</p>
<blockquote>
<p>I think out of all the options proposed, &#39;<code>identity</code>&#39; is the most readable/quickly understandable.</p>
<p>For example, I think the use of &#39;<code>identity</code>&#39; reads very nicely in <code>[1,2,3,4].group_by(&amp;:identity)</code></p>
</blockquote>
<p>Except that <code>#identity</code> seems to imply the same thing as <code>#__id__</code> , and <code>&quot;a&quot;.__id__</code> is not necessarily == <code>&quot;a&quot;.__id__</code> , as Matz said earlier.</p>
</blockquote>
<p>I have this concern as well. Having <code>&quot;a&quot;.__id__</code>/<code>object_id</code> and <code>&quot;a&quot;.identity</code> return drastically different things feels like it will just be confusing.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=401242013-06-25T07:46:19ZJoel VanderWerfvjoel@users.sourceforge.net
<ul></ul><p>Another argument against <code>#identity</code>: it is used by several libraries for something completely different. For example, in narray:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">&gt;&gt; <span class="constant">NMatrix</span>.float(<span class="integer">2</span>,<span class="integer">2</span>).identity
=&gt; <span class="constant">NMatrixfloat2</span>,<span class="integer">2</span>:
[ [ <span class="float">1.0</span>, <span class="float">0.0</span> ],
[ <span class="float">0.0</span>, <span class="float">1.0</span> ] ]
</span></code></pre>
<p>It&#39;s also used in celluloid-zmq:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">s1 = <span class="constant">PubSocket</span>.new
s1.identity = <span class="string"><span class="delimiter">&quot;</span><span class="content">publisher-A</span><span class="delimiter">&quot;</span></span>
</span></code></pre>
<p>I vote for &quot;<code>itself</code>&quot; or &quot;<code>self</code>&quot;, which are unlikely to be defined anywhere with some fundamentally different meaning.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=414402013-08-31T06:59:40ZMarc-Andre Lafortuneruby-core@marc-andre.ca
<ul><li><strong>File</strong> <a href="/attachments/download/3924/self.pdf">self.pdf</a> added</li></ul><p>Slide attached.</p>
<p>I hope to win the prize for simplest slide too.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=414532013-08-31T12:14:52ZCharlie Somervillecharliesome@ruby-lang.org
<ul></ul><p>marcandre: I think you made a mistake in your slide. It says &quot;Returns the class of obj&quot;, but it should say &quot;Returns obj&quot;</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=414562013-08-31T13:25:38ZYukihiro Matsumotomatz@ruby-lang.org
<ul></ul><p>I can accept <code>#itself</code>. I want to see it isn&#39;t conflict with existing methods.</p>
<p>Matz.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=444372014-01-20T00:48:24ZAndrew Vitandrew@avit.ca
<ul></ul><p>Rails ActiveSupport includes a similar method called <code>presence</code>. There is also a request to add block support to it, for a similar purpose: <a href="https://github.com/rails/rails/pull/13416#issuecomment-32636227">https://github.com/rails/rails/pull/13416#issuecomment-32636227</a></p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=444452014-01-20T05:47:50ZMarc-Andre Lafortuneruby-core@marc-andre.ca
<ul></ul><p>Andrew Vit wrote:</p>
<blockquote>
<p>Rails ActiveSupport includes a similar method called <code>presence</code>.</p>
</blockquote>
<p>Mmm, no, that&#39;s quite different. <code>&quot;&quot;.presence # =&gt; nil</code> for example.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=444512014-01-20T14:30:47ZFuad Saudfuadksd@gmail.com
<ul></ul><p>Wouldn’t such method accepting a block remove the need to have <code>Object#tap</code> at all? As I understand this method is just a tap that doesn’t need a block. <br>
-- <br>
Fuad Saud<br>
Sent with Airmail</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=444532014-01-20T17:16:14ZTsuyoshi Sawadasawadatsuyoshi@gmail.com
<ul></ul><p>I would like to propose <code>receiver</code> as the method name.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=444552014-01-20T20:20:47ZMatthew Kerwinmatthew@kerwin.net.au
<ul></ul><p>On Jan 21, 2014 12:29 AM, &quot;Fuad Saud&quot; <a href="mailto:fuadksd@gmail.com">fuadksd@gmail.com</a> wrote:</p>
<blockquote>
<p>Wouldn’t such method accepting a block remove the need to have <code>Object#tap</code><br>
at all? As I understand this method is just a tap that doesn’t need a<br>
block.</p>
</blockquote>
<p>That depends on the contract. I was under the impression that <code>#itself</code> (or<br>
whatever name) in block form would return the value of the block. e.g:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay"><span class="keyword">def</span> <span class="function">tap</span>
<span class="keyword">yield</span> <span class="predefined-constant">self</span> <span class="keyword">if</span> block_given?
<span class="predefined-constant">self</span>
<span class="keyword">end</span>
<span class="keyword">def</span> <span class="function">itself</span>
<span class="keyword">if</span> block_given?
<span class="keyword">yield</span> <span class="predefined-constant">self</span>
<span class="keyword">else</span>
<span class="predefined-constant">self</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
</span></code></pre> Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=444562014-01-20T20:42:30ZFuad Saudfuadksd@gmail.com
<ul></ul><p>That is interesting behaviour for chaining; not sure if consistent though.</p>
<p>—<br>
Sent from Mailbox for iPhone</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=445782014-01-24T21:19:28ZAndrew Vitandrew@avit.ca
<ul></ul><blockquote>
<p>Mmm, no, that&#39;s quite different. <code>&quot;&quot;.presence # =&gt; nil</code> for example.</p>
</blockquote>
<p>Yes, I was aware of the differences from ActiveSupport presence, I just wanted to point out the similar need for chaining &quot;<code>itself</code>&quot; with a block.</p>
<p>The proposed &quot;<code>itself</code>&quot; method also looks similar to &quot;<code>Enumerable#map</code>&quot; but for a single object, effectively:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay">[<span class="string"><span class="delimiter">&quot;</span><span class="content">ruby</span><span class="delimiter">&quot;</span></span>].map {|n| n.upcase }.first
<span class="comment"># same as:</span>
<span class="string"><span class="delimiter">&quot;</span><span class="content">ruby</span><span class="delimiter">&quot;</span></span>.itself {|n| n.upcase }
</span></code></pre>
<p>But I suppose &quot;<code>map</code>&quot; as a method name would be out of the question.</p>
<p>Has anyone considered &quot;<code>yield</code>&quot; as the method name? It also seems to fit well, and would likely not conflict with anything:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay"><span class="keyword">class</span> <span class="class">Object</span>
<span class="keyword">def</span> <span class="function">yield</span>
<span class="keyword">if</span> block_given?
<span class="keyword">yield</span> <span class="predefined-constant">self</span>
<span class="keyword">else</span>
<span class="predefined-constant">self</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="comment"># yield as a noun means &quot;the result&quot;: an object's result (its yield) is itself</span>
<span class="string"><span class="delimiter">&quot;</span><span class="content">ruby</span><span class="delimiter">&quot;</span></span>.yield <span class="comment">#=&gt; &quot;ruby&quot;</span>
<span class="comment"># yield as a verb means &quot;give way to&quot; or &quot;produce&quot;: the object gives way to the block</span>
<span class="string"><span class="delimiter">&quot;</span><span class="content">ruby</span><span class="delimiter">&quot;</span></span>.yield {|s| s.upcase } <span class="comment">#=&gt; &quot;RUBY&quot;</span>
</span></code></pre>
<p>Although there might be confusion between yield as a keyword and self.yield as a method, I do like this symmetry:</p>
<pre><code class="ruby syntaxhl"><span class="CodeRay"><span class="keyword">yield</span> <span class="predefined-constant">self</span> <span class="comment"># calls block with self, returns result</span>
<span class="predefined-constant">self</span>.yield(&amp;block) <span class="comment"># calls block with self, returns result</span>
<span class="predefined-constant">self</span>.yield <span class="comment"># implicit identity, just like: {|obj| obj }</span>
</span></code></pre> Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=481022014-07-28T04:25:52ZNobuyoshi Nakadanobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/diff/48102?detail_id=34758">diff</a>)</li></ul><p>Yukihiro Matsumoto wrote:</p>
<blockquote>
<p>I can accept <code>#itself</code>. I want to see it isn&#39;t conflict with existing methods.</p>
</blockquote>
<p>It&#39;s hard to imagine other functionality from that name for me.<br>
The best way to tell if it will break something should be implementing it before previews, IMHO.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=481032014-07-28T04:37:11ZMartin Dürstduerst@it.aoyama.ac.jp
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Open</i></li></ul> Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=481592014-08-01T17:18:13ZRafael Françarafaelmfranca@gmail.com
<ul></ul><p>We just implemented it on Active Support <a href="https://github.com/rails/rails/commit/702ad710b57bef45b081ebf42e6fa70820fdd810">https://github.com/rails/rails/commit/702ad710b57bef45b081ebf42e6fa70820fdd810</a></p>
<p>I believe matz already accepted it as <code>#itself</code> so we are renaming it to <code>#itself</code>.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=481602014-08-01T18:23:21ZRafael Françarafaelmfranca@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/download/4606/itself.diff">itself.diff</a><a href="/attachments/4606/itself.diff"><img src="/images/magnifier.png" alt="Magnifier" /></a> added</li></ul><p>Here the implementation. This is my first patch to Ruby so I&#39;m not sure it is correct. Let me know if I need to change something.</p>
Ruby trunk - Feature #6373: public #selfhttps://bugs.ruby-lang.org/issues/6373?journal_id=481612014-08-02T01:27:10ZNobuyoshi Nakadanobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>% Done</strong> changed from <i>0</i> to <i>100</i></li></ul><p>Applied in changeset <a class="changeset" title="object.c: Object#itsef * object.c (rb_obj_itself): new method Object#itsef. based on the patc..." href="https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/47028">r47028</a>.</p>
<hr>
<p>object.c: Object#itsef</p>
<ul>
<li>object.c (rb_obj_itself): new method Object#itsef. based on the
patch by Rafael França in .
[EXPERIMENTAL] this method may be renamed due to compatibilities.
[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="public #self (Closed)" href="https://bugs.ruby-lang.org/issues/6373">#6373</a>]</li>
</ul>