Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>_</code> is a valid identifier. Identifiers can't just contain underscores, they can also <em>be</em> an underscore.</p> <pre><code>_ = o = Object.new _.object_id == o.object_id # =&gt; true </code></pre> <p>You can also use it as method names:</p> <pre><code>def o._; :_ end o._ # =&gt; :_ </code></pre> <p>Of course, it is not exactly a readable name, nor does it pass any information to the reader about what the variable refers to or what the method does.</p> <p><code>IRB</code>, in particular, sets <code>_</code> to the value of the last expression:</p> <pre><code>$ irb &gt; 'asd' # =&gt; "asd" &gt; _ # =&gt; "asd" </code></pre> <p>As it is <a href="https://github.com/ruby/ruby/blob/trunk/lib/irb/context.rb#L167" rel="noreferrer">in the source code</a>, it simply sets <code>_</code> to the last value:</p> <pre><code>@workspace.evaluate self, "_ = IRB.CurrentContext.last_value" </code></pre> <hr> <p>Did some repository exploring. Here's what I found:</p> <p>On the last lines of the file <a href="https://github.com/ruby/ruby/blob/trunk/id.c#L50" rel="noreferrer"><code>id.c</code></a>, there is the call:</p> <pre><code>REGISTER_SYMID(idUScore, "_"); </code></pre> <p><code>grep</code>ing the source for <code>idUScore</code> gave me two seemingly relevant results:</p> <ul> <li>In the <a href="https://github.com/ruby/ruby/blob/trunk/parse.y#L8993" rel="noreferrer"><code>shadowing_lvar_gen</code> function</a></li> <li>In the <a href="https://github.com/ruby/ruby/blob/trunk/parse.y#L9692" rel="noreferrer"><code>warn_unused_var</code> function</a></li> </ul> <p><code>shadowing_lvar_gen</code> seems to be the mechanism through which the formal parameter of a block replaces a variable of the same name that exists in another scope. It is the function that seems to raise "duplicated argument name" <code>SyntaxError</code> and the "shadowing outer local variable" warning.</p> <p>After <code>grep</code>ing the source for <code>shadowing_lvar_gen</code>, I found the following <a href="https://raw.github.com/ruby/ruby/trunk/doc/ChangeLog-1.9.3" rel="noreferrer">on the changelog for Ruby 1.9.3</a>:</p> <blockquote> <p>Tue Dec 11 01:21:21 2007 Yukihiro Matsumoto </p> <ul> <li>parse.y (shadowing_lvar_gen): no duplicate error for "_".</li> </ul> </blockquote> <p>Which is likely to be the origin of <a href="https://github.com/ruby/ruby/blob/trunk/parse.y#L8993" rel="noreferrer">this line</a>:</p> <pre><code>if (idUScore == name) return name; </code></pre> <p>From this, I deduce that in a situation such as <code>proc { |_, _| :x }.call :a, :b</code>, one <code>_</code> variable simply shadows the other.</p> <hr> <p>Here's <a href="https://github.com/ruby/ruby/commit/6bd65de203c7aaffc8d4241a771491356002ed75" rel="noreferrer">the commit in question</a>. It basically introduced these two lines:</p> <pre><code>if (!uscore) uscore = rb_intern("_"); if (uscore == name) return; </code></pre> <p>From a time when <code>idUScore</code> did not even exist, apparently.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload