https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17097754782012-02-24T08:23:05ZRuby Issue Tracking SystemRuby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=240712012-02-24T08:23:05Zshugo (Shugo Maeda)
<ul></ul><p>Hi,</p>
<p>2012/2/24 Shugo Maeda <a href="mailto:redmine@ruby-lang.org" class="email">redmine@ruby-lang.org</a>:</p>
<blockquote>
<p>There is at least one problem in this patch. The problem is that it cannot handle<br>
the following code in mkmf.rb.</p>
<a name="I-suspected-that-this-code-was-written-by-nobu-but-it-was-written-by-Eric"></a>
<h1 >I suspected that this code was written by nobu, but it was written by Eric.<a href="#I-suspected-that-this-code-was-written-by-nobu-but-it-was-written-by-Eric" class="wiki-anchor">¶</a></h1>
</blockquote>
<p>I apologize that it was wrong, Eric.<br>
Eric just fixed indentation of code written by nobu.<br>
I believe that no one else write such code.</p>
<p>--<br>
Shugo Maeda</p> Ruby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=240802012-02-24T14:25:57Zshugo (Shugo Maeda)
<ul><li><strong>File</strong> <a href="/attachments/2477">for_loop_scope_fix_2.diff</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/2477/for_loop_scope_fix_2.diff">for_loop_scope_fix_2.diff</a> added</li></ul><p>Hi,</p>
<p>Shugo Maeda wrote:</p>
<blockquote>
<p>There is at least one problem in this patch. The problem is that it cannot handle<br>
the following code in mkmf.rb.</p>
</blockquote>
<p>I've fixed my patch to fix the problem.<br>
By the attached patch, the scope of variables in expr_value is not limited in<br>
the for expression.</p>
<p>I think it's reasonable because</p>
<p>for i in <expr_value><br>
end</p>
<p>is equivalent to</p>
<p><expr_value>.each do |i|<br>
end</p>
<p>, where <expr_value> is outside the block.</p> Ruby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=240902012-02-25T03:23:10ZAnonymous
<ul></ul><p>On Fri, Feb 24, 2012 at 02:25:57PM +0900, Shugo Maeda wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: The scope of for loop variables (Rejected)" href="https://bugs.ruby-lang.org/issues/6070">#6070</a> has been updated by Shugo Maeda.</p>
<p>File for_loop_scope_fix_2.diff added</p>
<p>Hi,</p>
<p>Shugo Maeda wrote:</p>
<blockquote>
<p>There is at least one problem in this patch. The problem is that it cannot handle<br>
the following code in mkmf.rb.</p>
</blockquote>
<p>I've fixed my patch to fix the problem.<br>
By the attached patch, the scope of variables in expr_value is not limited in<br>
the for expression.</p>
<p>I think it's reasonable because</p>
<p>for i in <expr_value><br>
end</p>
<p>is equivalent to</p>
<p><expr_value>.each do |i|<br>
end</p>
<p>, where <expr_value> is outside the block.</p>
</blockquote>
<p>I agree with your opinion regarding the current side effect problems of<br>
<code>for</code>. I would love to see your patch in Ruby 2.0!</p>
<p>--<br>
Aaron Patterson<br>
<a href="http://tenderlovemaking.com/" class="external">http://tenderlovemaking.com/</a></p> Ruby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=241502012-02-25T23:42:22Zshugo (Shugo Maeda)
<ul><li><strong>File</strong> <a href="/attachments/2484">for_loop_scope_fix_0224.diff</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/2484/for_loop_scope_fix_0224.diff">for_loop_scope_fix_0224.diff</a> added</li></ul><p>Hi,</p>
<blockquote>
<p>I agree with your opinion regarding the current side effect problems of<br>
<code>for</code>. I would love to see your patch in Ruby 2.0!</p>
</blockquote>
<p>Thanks for your agreement:)</p>
<p>I've updated my patch, whose changes are:</p>
<ul>
<li>use NODE_MEMO to avoid memory leak when parsing of expr_value failed.</li>
<li>removed unused code.</li>
<li>fixed tests for new behavior of for expressions.</li>
<li>fixed rss/parser.rb for new behavior of for expressions.</li>
</ul>
<p>A good news is that rss/parser.rb is the only library affected by this change of for expressions.<br>
The patch passes make test, and the result of test-all is 1F0E, which is the same as the version without the patch.</p>
<p>The version with the patch is a bit faster than the original version:</p>
<p>defiant:ruby$ time ruby-trunk -e 'for i in 1..10000000; end'<br>
ruby-trunk -e 'for i in 1..10000000; end' 3.92s user 0.01s system 99% cpu 3.936 total<br>
defiant:ruby$ time build/ruby-trunk -e 'for i in 1..10000000; end'<br>
build/ruby-trunk -e 'for i in 1..10000000; end' 3.52s user 0.02s system 99% cpu 3.545 total</p> Ruby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=242492012-03-01T03:46:13Zkernigh (George Koehler)xkernigh@netscape.net
<ul></ul><p>=begin<br>
This patch (for_loop_scope_fix_0224.diff) introduces a confusing change. With current Ruby, for loops and loops never have local variables. With this patch, for loops have local variables, but while loops never have them. Here is a brief example.</p>
<p>[nil].each do |i|<br>
x = "x is string"<br>
end<br>
puts((x rescue "x not defined"))</p>
<p>for j in [nil]<br>
y = "y is string"<br>
end<br>
puts((y rescue "y not defined"))</p>
<p>while true<br>
z = "z is string"<br>
break<br>
end<br>
puts((z rescue "z not defined"))</p>
<p>With current Ruby, y and z are not local to their loops.</p>
<p>$ ../tool/runruby.rb -v<br>
ruby 2.0.0dev (2012-03-01 trunk 34861) [x86_64-openbsd5.0]<br>
$ ../tool/runruby.rb ~/scratch.rb<br>
x not defined<br>
y is string<br>
z is string</p>
<p>After applying for_loop_scope_fix_0224.diff, y is local to its for loop, but z is not local to its while loop.</p>
<p>$ ../tool/runruby.rb ~/scratch.rb<br>
x not defined<br>
y not defined<br>
z is string</p>
<p>With for_loop_scope_fix_0224.diff, it is difficult to understand why for loops can restrict scope of variables, but while loops never do so.</p>
<p>=end</p> Ruby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=243042012-03-05T16:23:15Zshugo (Shugo Maeda)
<ul></ul><p>Hello,</p>
<p>2012/3/1 George Koehler <a href="mailto:xkernigh@netscape.net" class="email">xkernigh@netscape.net</a>:</p>
<blockquote>
<p>This patch (for_loop_scope_fix_0224.diff) introduces a confusing change. With current Ruby, for loops and loops never have local variables. With this patch, for loops have local variables, but while loops never have them. Here is a brief example.</p>
</blockquote>
<p>Yes, it is what I have expected.</p>
<blockquote>
<p>With for_loop_scope_fix_0224.diff, it is difficult to understand why for loops can restrict scope of variables, but while loops never do so.</p>
</blockquote>
<p>Without the patch, the behavior of for loops is consistent with that<br>
of while loops; however, it is inconsistent with that of blocks. By<br>
contrast with the patch, the behavior of for loops is consistent with<br>
that of blocks; however, it is inconsistent with that of while loops.<br>
So there's a trade-off between these two design choices.</p>
<p>For loops are syntactically close to while loops, but for loops are<br>
semantically more close to blocks, so I think it's better to make for<br>
loops consistent with blocks.</p>
<p>--<br>
Shugo Maeda</p> Ruby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=243052012-03-05T16:59:09Zko1 (Koichi Sasada)
<ul></ul><p>(2012/03/05 16:10), Shugo Maeda wrote:</p>
<blockquote>
<p>For loops are syntactically close to while loops, but for loops are<br>
semantically more close to blocks, so I think it's better to make for<br>
loops consistent with blocks.</p>
</blockquote>
<p>I like current behavior because of consistency with "while" statement :)</p>
<p>--<br>
// SASADA Koichi at atdot dot net</p> Ruby master - Feature #6070: The scope of for loop variableshttps://bugs.ruby-lang.org/issues/6070?journal_id=246032012-03-15T17:53:49Zshugo (Shugo Maeda)
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Koichi Sasada wrote:</p>
<blockquote>
<p>(2012/03/05 16:10), Shugo Maeda wrote:</p>
<blockquote>
<p>For loops are syntactically close to while loops, but for loops are<br>
semantically more close to blocks, so I think it's better to make for<br>
loops consistent with blocks.</p>
</blockquote>
<p>I like current behavior because of consistency with "while" statement :)</p>
</blockquote>
<p>Matz has the same opinion, so I'll close this ticket.</p>