https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112021-02-12T11:06:15ZRuby Issue Tracking SystemRuby master - Bug #17623: irb starts with some local variables already definedhttps://bugs.ruby-lang.org/issues/17623?journal_id=903572021-02-12T11:06:15Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>I cannot reproduce the issue.</p>
<pre><code>$ ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
$ irb
irb(main):001:0* a = 1
=> 1
irb(main):002:1* def f = a
=> :f
irb(main):003:0> f
Traceback (most recent call last):
5: from /home/mame/local/bin/irb:23:in `<main>'
4: from /home/mame/local/bin/irb:23:in `load'
3: from /home/mame/local/lib/ruby/gems/3.0.0/gems/irb-1.3.0/exe/irb:11:in `<top (required)>'
2: from (irb):3:in `<main>'
1: from (irb):2:in `f'
NameError (undefined local variable or method `a' for main:Object)
irb(main):004:0>
$ ruby
a = 1
def f = a
f
-:2:in `f': undefined local variable or method `a' for main:Object (NameError)
from -:3:in `<main>'
</code></pre>
<p>Does your <code>.irbrc</code> have something?</p> Ruby master - Bug #17623: irb starts with some local variables already definedhttps://bugs.ruby-lang.org/issues/17623?journal_id=903582021-02-12T15:30:59Zxtkoba (Tee KOBAYASHI)
<ul></ul><p>It reproduces with Ruby 3.0.0-1 (x64) from RubyInstaller, and it seems that <code>f</code> has already been defined when the first prompt (<code>irb(main):001:0> </code>) appears.</p>
<p>It does not reproduce with my own x64-mingw32 build.</p> Ruby master - Bug #17623: irb starts with some local variables already definedhttps://bugs.ruby-lang.org/issues/17623?journal_id=903592021-02-12T16:08:30Zxtkoba (Tee KOBAYASHI)
<ul></ul><p>I found that <code>f</code> is indeed defined in RubyInstaller's <code>irb.cmd</code>:</p>
<pre><code>--- original/irb.cmd
+++ RubyInstaller/irb.cmd
@@ -28,7 +28,7 @@
end
if Gem.respond_to?(:activate_bin_path)
-load Gem.activate_bin_path('irb', 'irb', version)
+f = Gem.activate_bin_path('irb', 'irb', version); require 'irbrc_predefiner'; load f
else
gem "irb", version
load Gem.bin_path("irb", "irb", version)
</code></pre>
<p>The same goes for <code>str</code> or <code>version</code> with every environment:</p>
<pre><code>irb(main):001:0> str
=> nil
irb(main):002:0> version
=> ">= 0.a"
irb(main):003:0>
</code></pre>
<p>I have no idea whether it is a bug or not that the local variables defind in <code>irb</code> command are visible from IRB.</p> Ruby master - Bug #17623: irb starts with some local variables already definedhttps://bugs.ruby-lang.org/issues/17623?journal_id=903602021-02-12T16:49:21ZEregon (Benoit Daloze)
<ul></ul><p>I think that might be caused by <a href="https://bugs.ruby-lang.org/issues/9580#change-88660" class="external">https://bugs.ruby-lang.org/issues/9580#change-88660</a> cc <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/182">@marcandre (Marc-Andre Lafortune)</a></p> Ruby master - Bug #17623: irb starts with some local variables already definedhttps://bugs.ruby-lang.org/issues/17623?journal_id=905212021-02-19T22:05:46Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>marcandre (Marc-Andre Lafortune)</i></li></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/772">@Eregon (Benoit Daloze)</a> is correct. This issue first started in Ruby 3.0. It does not occur when the irb <code>--context-mode</code> is 1, 2, or 3 (Ruby 2.7 default), but it does occur when <code>--context-mode</code> is 4 (Ruby 3.0 default) or 0. Between Ruby 2.7 and 3.0, the default irb <code>--context-mode</code> switched from 3 to 4 to fix <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Refinements regression in IRB (Closed)" href="https://bugs.ruby-lang.org/issues/9580">#9580</a>.</p>
<p>It seems undesirable to me that <code>irb</code> would have local variables predefined other than <code>_</code>. While the <code>f</code> local variable is RubyInstaller specific, the <code>str</code> and <code>version</code> local variables are not. So I think this should be considered a bug.</p>
<p>Assigning to <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/182">@marcandre (Marc-Andre Lafortune)</a>, since he made the <code>--context-mode</code> change in irb. One possible solution is to accept the new irb behavior, and modify the binstubs to not expose or create local variables. An easy way to do that:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">proc</span> <span class="k">do</span>
<span class="c1"># previous binstub code</span>
<span class="k">end</span><span class="p">.</span><span class="nf">call</span>
</code></pre>
<p>The problem with that approach is it adds a stack frame. An alternative approach that doesn't add a stack frame is to switch to a much uglier approach using instance variables (which can be removed, unlike local variables):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">require</span> <span class="s1">'rubygems'</span>
<span class="vi">@version</span> <span class="o">=</span> <span class="s2">">= 0.a"</span>
<span class="vi">@str</span> <span class="o">=</span> <span class="no">ARGV</span><span class="p">.</span><span class="nf">first</span>
<span class="k">if</span> <span class="vi">@str</span>
<span class="vi">@str</span> <span class="o">=</span> <span class="vi">@str</span><span class="p">.</span><span class="nf">b</span><span class="p">[</span><span class="sr">/\A_(.*)_\z/</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="k">if</span> <span class="vi">@str</span> <span class="ow">and</span> <span class="no">Gem</span><span class="o">::</span><span class="no">Version</span><span class="p">.</span><span class="nf">correct?</span><span class="p">(</span><span class="vi">@str</span><span class="p">)</span>
<span class="vi">@version</span> <span class="o">=</span> <span class="vi">@str</span>
<span class="no">ARGV</span><span class="p">.</span><span class="nf">shift</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">remove_instance_variable</span><span class="p">(</span><span class="ss">:@str</span><span class="p">)</span>
<span class="k">if</span> <span class="no">Gem</span><span class="p">.</span><span class="nf">respond_to?</span><span class="p">(</span><span class="ss">:activate_bin_path</span><span class="p">)</span>
<span class="nb">load</span> <span class="no">Gem</span><span class="p">.</span><span class="nf">activate_bin_path</span><span class="p">(</span><span class="s1">'irb'</span><span class="p">,</span> <span class="s1">'irb'</span><span class="p">,</span> <span class="vi">@version</span><span class="p">.</span><span class="nf">tap</span><span class="p">{</span><span class="n">remove_instance_variable</span><span class="p">(</span><span class="ss">:@version</span><span class="p">)})</span>
<span class="k">else</span>
<span class="n">gem</span> <span class="s2">"irb"</span><span class="p">,</span> <span class="vi">@version</span>
<span class="nb">load</span> <span class="no">Gem</span><span class="p">.</span><span class="nf">bin_path</span><span class="p">(</span><span class="s2">"irb"</span><span class="p">,</span> <span class="s2">"irb"</span><span class="p">,</span> <span class="vi">@version</span><span class="p">.</span><span class="nf">tap</span><span class="p">{</span><span class="n">remove_instance_variable</span><span class="p">(</span><span class="ss">:@version</span><span class="p">)})</span>
<span class="k">end</span>
</code></pre> Ruby master - Bug #17623: irb starts with some local variables already definedhttps://bugs.ruby-lang.org/issues/17623?journal_id=909812021-03-18T13:50:18Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Subject</strong> changed from <i>endless def can access to outer local variables and lead to unexpected result</i> to <i>irb starts with some local variables already defined</i></li></ul><p>I think this is a nice solution: <a href="https://github.com/ruby/irb/pull/206" class="external">https://github.com/ruby/irb/pull/206</a></p> Ruby master - Bug #17623: irb starts with some local variables already definedhttps://bugs.ruby-lang.org/issues/17623?journal_id=935902021-09-09T21:37:58ZAnonymous
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="[ruby/irb] Avoid loading files' local variables [Bug #17623] https://github.com/ruby/irb/commit/..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/3503c94af501e38164613ef8347174a27346828a">git|3503c94af501e38164613ef8347174a27346828a</a>.</p>
<hr>
<p>[ruby/irb] Avoid loading files' local variables [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: irb starts with some local variables already defined (Closed)" href="https://bugs.ruby-lang.org/issues/17623">#17623</a>]</p>
<p><a href="https://github.com/ruby/irb/commit/b12f0cb8e2" class="external">https://github.com/ruby/irb/commit/b12f0cb8e2</a></p>