<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Gray Soft / Early Steps / The Evils of the For Loop</title>
  <id>tag:graysoftinc.com,2014-03-20:/posts/77</id>
  <updated>2016-02-11T13:50:08Z</updated>
  <link rel="self" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop/feed.xml"/>
  <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop"/>
  <author>
    <name>James Edward Gray II</name>
  </author>
  <entry>
    <title>The 11th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_607"/>
    <id>tag:graysoftinc.com,2016-02-11:/comments/607</id>
    <updated>2016-02-11T13:50:08Z</updated>
    <summary>It&amp;#39;s a fair complaint.  I need to update the site.</summary>
    <content type="html">&lt;p&gt;It's a fair complaint.  I need to update the site.&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
  <entry>
    <title>The 10th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_606"/>
    <id>tag:graysoftinc.com,2016-02-11:/comments/606</id>
    <updated>2016-02-11T13:50:08Z</updated>
    <summary>nice collection of flash things that are not visible nowadays. update or remove.</summary>
    <content type="html">&lt;p&gt;nice collection of flash things that are not visible nowadays. update or remove.&lt;/p&gt;</content>
    <author>
      <name>Parabans Belroder</name>
    </author>
  </entry>
  <entry>
    <title>The 9th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_405"/>
    <id>tag:graysoftinc.com,2010-11-04:/comments/405</id>
    <updated>2014-04-17T19:43:41Z</updated>
    <summary>JavaScript has the same problem!

```javascript
var items = [&amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;cherry&amp;quot;]
var results = []

for (var i in items) {
    // i is the index, i.e. items[i] will be &amp;quot;apple&amp;quot;, ...
    results.push(function() { return i; })
}

r...</summary>
    <content type="html">&lt;p&gt;JavaScript has the same problem!&lt;/p&gt;

&lt;div class="highlight highlight-javascript"&gt;&lt;pre&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"banana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"cherry"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// i is the index, i.e. items[i] will be "apple", ...&lt;/span&gt;
    &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You get &lt;code&gt;2 2 2&lt;/code&gt; instead of &lt;code&gt;0 1 2&lt;/code&gt;, as the &lt;code&gt;i&lt;/code&gt; in &lt;code&gt;for (var ... in ... )&lt;/code&gt; is actually a single variable, rather than instantiated afresh (and thus different in each closure) each time.&lt;/p&gt;

&lt;p&gt;(note that I'm ignoring the actual values of items above, just using the array to get indices. In a real life situation, you might be indexing the array with &lt;code&gt;i&lt;/code&gt; in the closure, expecting to do something to a different member each time, whereas they'd all be operating on &lt;code&gt;"cherry"&lt;/code&gt; here.)&lt;/p&gt;</content>
    <author>
      <name>Arlen</name>
    </author>
  </entry>
  <entry>
    <title>The 8th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_277"/>
    <id>tag:graysoftinc.com,2009-04-18:/comments/277</id>
    <updated>2014-04-17T19:40:13Z</updated>
    <summary>`for...in` is easier to type and easier to read. You would only have problems with existing variables if your methods are too long.

That said, I use `each()` because I don&amp;#39;t want the cool kids to sneer at me. </summary>
    <content type="html">&lt;p&gt;&lt;code&gt;for...in&lt;/code&gt; is easier to type and easier to read. You would only have problems with existing variables if your methods are too long.&lt;/p&gt;

&lt;p&gt;That said, I use &lt;code&gt;each()&lt;/code&gt; because I don't want the cool kids to sneer at me. &lt;/p&gt;</content>
    <author>
      <name>Mark Wilden</name>
    </author>
  </entry>
  <entry>
    <title>The 7th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_276"/>
    <id>tag:graysoftinc.com,2009-04-17:/comments/276</id>
    <updated>2014-03-27T01:38:26Z</updated>
    <summary>A recent post by Andrej Bauer about a similar thing in Python:

[http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/](http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/)

The discussion there, and the follow-up post:

[http:...</summary>
    <content type="html">&lt;p&gt;A recent post by Andrej Bauer about a similar thing in Python:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/"&gt;http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The discussion there, and the follow-up post:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://math.andrej.com/2009/04/11/on-programming-language-design/"&gt;http://math.andrej.com/2009/04/11/on-programming-language-design/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;are interesting from the point of view of comparing how a language does work (from a comprehending-the-current-implementatino perspective) to how a language should work (from a general language design perspective).&lt;/p&gt;</content>
    <author>
      <name>Chris Mear</name>
    </author>
  </entry>
  <entry>
    <title>The 6th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_273"/>
    <id>tag:graysoftinc.com,2009-04-16:/comments/273</id>
    <updated>2014-03-27T01:38:26Z</updated>
    <summary>I think it&amp;#39;s not a bug, it&amp;#39;s feature. Consider the same code in other languages like C(++), PHP or Python - it works like the same.</summary>
    <content type="html">&lt;p&gt;I think it's not a bug, it's feature. Consider the same code in other languages like C(++), PHP or Python - it works like the same.&lt;/p&gt;</content>
    <author>
      <name>texec</name>
    </author>
  </entry>
  <entry>
    <title>The 5th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_254"/>
    <id>tag:graysoftinc.com,2009-01-09:/comments/254</id>
    <updated>2014-03-27T01:38:26Z</updated>
    <summary>While my take is nowhere near as nuanced as yours, you can always refer to my version when you&amp;#39;re feeling sassy: [http://therealadam.com/archive/2008/01/06/the-barbarism-of-the-for-loop/](http://therealadam.com/archive/2008/01/06/the-barbarism-of-...</summary>
    <content type="html">&lt;p&gt;While my take is nowhere near as nuanced as yours, you can always refer to my version when you're feeling sassy: &lt;a href="http://therealadam.com/archive/2008/01/06/the-barbarism-of-the-for-loop/"&gt;http://therealadam.com/archive/2008/01/06/the-barbarism-of-the-for-loop/&lt;/a&gt;&lt;/p&gt;</content>
    <author>
      <name>Adam Keys</name>
    </author>
  </entry>
  <entry>
    <title>The 4th Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_253"/>
    <id>tag:graysoftinc.com,2009-01-08:/comments/253</id>
    <updated>2014-04-17T19:38:46Z</updated>
    <summary>&amp;gt; Out of curiosity, if this problem with `for..in` stepping on local variables was fixed in 1.9, would you still have issues with using it?

I would still prefer `each()`.

I feel it is better to stick with the uniform iterator approach throug...</summary>
    <content type="html">&lt;blockquote&gt;
&lt;p&gt;Out of curiosity, if this problem with &lt;code&gt;for..in&lt;/code&gt; stepping on local variables was fixed in 1.9, would you still have issues with using it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I would still prefer &lt;code&gt;each()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I feel it is better to stick with the uniform iterator approach throughout.  For example, it's easier to turn &lt;code&gt;each()&lt;/code&gt; into &lt;code&gt;each_with_index()&lt;/code&gt; than &lt;code&gt;for&lt;/code&gt;.  I also feel it's easier for us to just teach the iterator approach to new Rubyists if we don't bother with unneeded exceptions like the &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;That's all just one guy's opinion though.&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
  <entry>
    <title>The 3rd Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_252"/>
    <id>tag:graysoftinc.com,2009-01-08:/comments/252</id>
    <updated>2014-04-17T19:38:46Z</updated>
    <summary>I agree `for` loops are ugly, I hate to see them in plain Ruby code but have grown used to them in the Rails views.

Out of curiosity, if this problem with `for..in` stepping on local variables was fixed in 1.9, would you still have issues with ...</summary>
    <content type="html">&lt;p&gt;I agree &lt;code&gt;for&lt;/code&gt; loops are ugly, I hate to see them in plain Ruby code but have grown used to them in the Rails views.&lt;/p&gt;

&lt;p&gt;Out of curiosity, if this problem with &lt;code&gt;for..in&lt;/code&gt; stepping on local variables was fixed in 1.9, would you still have issues with using it?&lt;/p&gt;</content>
    <author>
      <name>Ryan Bates</name>
    </author>
  </entry>
  <entry>
    <title>The 2nd Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_251"/>
    <id>tag:graysoftinc.com,2009-01-08:/comments/251</id>
    <updated>2014-04-17T19:36:56Z</updated>
    <summary>&amp;gt; I don&amp;#39;t use the `for..in` thinger, but even the fact that each() steps on my existing variables is probably cause for some of my hair-pulling. I&amp;#39;m ready for 1.9.

You can actually use 1.9 to find these bugs in 1.8 code.  Dave Thomas did exactl...</summary>
    <content type="html">&lt;blockquote&gt;
&lt;p&gt;I don't use the &lt;code&gt;for..in&lt;/code&gt; thinger, but even the fact that each() steps on my existing variables is probably cause for some of my hair-pulling. I'm ready for 1.9.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can actually use 1.9 to find these bugs in 1.8 code.  Dave Thomas did exactly that recently while upgrading some code that runs in TextMate to be 1.9 friendly.  He ran our scripts through 1.9, just to make sure they were working there.  Because 1.9 has some new and improved warnings, it found bugs for us as an added bonus:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ruby_dev -vwe 'a = 1; (3..5).each { |a| }'
ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21203) [i386-darwin9.6.0]
-e:1: warning: shadowing outer local variable - a
&lt;/code&gt;&lt;/pre&gt;

&lt;blockquote&gt;
&lt;p&gt;I notice you waited so very patiently for 1.9 to be right around the corner before you started bad-mouthing 1.8.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I really didn't mean it to be too negative.  Consider it a sign of good things to come.&lt;/p&gt;

&lt;p&gt;I adore Ruby and that includes 1.8, but I hope we all know it's not perfect in everything it does.  ;)&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
  <entry>
    <title>The 1st Comment on "The Evils of the For Loop"</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop#comment_250"/>
    <id>tag:graysoftinc.com,2009-01-07:/comments/250</id>
    <updated>2014-04-17T19:36:56Z</updated>
    <summary>Thanks for this post! I wonder how many times that bit me and I didn&amp;#39;t even know it!!?

I don&amp;#39;t use the `for..in` thinger, but even the fact that each() steps on my existing variables is probably cause for some of my hair-pulling. I&amp;#39;m ready for ...</summary>
    <content type="html">&lt;p&gt;Thanks for this post! I wonder how many times that bit me and I didn't even know it!!?&lt;/p&gt;

&lt;p&gt;I don't use the &lt;code&gt;for..in&lt;/code&gt; thinger, but even the fact that each() steps on my existing variables is probably cause for some of my hair-pulling. I'm ready for 1.9.&lt;/p&gt;

&lt;p&gt;I notice you waited so very patiently for 1.9 to be right around the corner before you started bad-mouthing 1.8. :-)&lt;/p&gt;</content>
    <author>
      <name>Tim Morgan</name>
    </author>
  </entry>
  <entry>
    <title>The Evils of the For Loop</title>
    <link rel="alternate" href="http://graysoftinc.com/early-steps/the-evils-of-the-for-loop"/>
    <id>tag:graysoftinc.com,2009-01-07:/posts/77</id>
    <updated>2016-02-11T13:50:08Z</updated>
    <summary>This is a reminder of the reasons why we should avoid the for loop Ruby provides.</summary>
    <content type="html">&lt;p&gt;I've never liked the &lt;code&gt;for…in&lt;/code&gt; loop in Ruby.  I cringe every time I see it in examples (Rails seems to put it in views a lot) and I tend to switch it to an &lt;code&gt;each()&lt;/code&gt; call.  It really bugs me.&lt;/p&gt;

&lt;p&gt;That's mostly just my gut reaction, but if I had to put it into words it's that I fell in love with Ruby's iterators early on and &lt;code&gt;for&lt;/code&gt; just doesn't seem to fit in well with them.  I don't think that's just my emotions talking either, it really doesn't fit in.  I'll try to show you why I say that.&lt;/p&gt;

&lt;p&gt;First, let's see what I'm talking about.  We are all pretty comfortable with &lt;code&gt;each()&lt;/code&gt;, right?&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 1&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 2&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I doubt that surprises anyone.  Many of you probably also know that Ruby allows you to write that as:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 1&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 2&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That's &lt;strong&gt;almost&lt;/strong&gt; the same thing.  It really does use &lt;code&gt;each()&lt;/code&gt; under the hood, for example:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyEachThing&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;each&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;MyEachThing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
  &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 1&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 42&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 2&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 42&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, there's one tiny difference that ends up being pretty critical.  The &lt;code&gt;for&lt;/code&gt; version really translates closer to this Ruby 1.8 code:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;whatever&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# translates to:&lt;/span&gt;
&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;  &lt;span class="c1"&gt;# assuming i didn't already exist&lt;/span&gt;
&lt;span class="n"&gt;whatever&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It turns out that insignificant looking assignment makes all the difference.  For those who aren't familiar with why it matters, have a look at this surprising Ruby 1.8 code:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Blocks in Ruby 1.8, like the one used by the &lt;code&gt;each()&lt;/code&gt; iterator here, will reuse a local variable if it exists before the block is entered.  You can see the affect of that here in that the value of my variable changed, even though my iterator really didn't do anything.  It just reassigned my variable a few times.&lt;/p&gt;

&lt;p&gt;Don't worry if the above behavior seems surprising and/or evil.  It is.  As a result, it was removed in Ruby 1.9.  However, &lt;code&gt;for&lt;/code&gt; is unchanged in Ruby 1.9 and still best avoided, in my opinion.&lt;/p&gt;

&lt;p&gt;Using this knowledge, we can now see one often harmless side effect of using &lt;code&gt;for&lt;/code&gt; over &lt;code&gt;each()&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice that &lt;code&gt;i&lt;/code&gt; still exists after the loop.  That's not the case with a simple &lt;code&gt;each()&lt;/code&gt; (as long as it didn't exist before the call):&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt; -:4: undefined local variable or method `i' for main:Object (NameError)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is important so let me say it one more way.  Calling &lt;code&gt;(1..3).each { |i|  }&lt;/code&gt; without an existing &lt;code&gt;i&lt;/code&gt; variable works like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a local &lt;code&gt;i&lt;/code&gt; variable&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run block&lt;/li&gt;
&lt;li&gt;Create a local &lt;code&gt;i&lt;/code&gt; variable&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run block&lt;/li&gt;
&lt;li&gt;Create a local &lt;code&gt;i&lt;/code&gt; variable&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;3&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run block&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;However, running the same code with an existing &lt;code&gt;i&lt;/code&gt; variable, or using &lt;code&gt;for&lt;/code&gt;, changes the steps to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an &lt;code&gt;i&lt;/code&gt; variable unless it exists (only &lt;code&gt;for&lt;/code&gt; does this)&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run block&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run block&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;3&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run block&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Notice how we are missing a bunch of variable creation steps there?  That's because there's only one variable now and it's not local to the block.&lt;/p&gt;

&lt;p&gt;I bet you are wondering why I've made such a big deal out of this tiny change by now.  Why does this matter?  Let me show you one more set of examples to answer that.  First, this code shouldn't surprise you:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 1&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 2&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But does the following example surprise you?  It sure surprised me the first time I saw it:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You should be able to reason it out now though.  Remember, there's only one &lt;code&gt;i&lt;/code&gt; variable in this example.  The &lt;code&gt;lambda()&lt;/code&gt; blocks all refer to the same variable, because they are closures.  By the time we run any of those blocks, the variable holds its final value.&lt;/p&gt;

&lt;p&gt;This example may seem a little contrived, but the truth is that it's not.  I just simplified &lt;a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/324126"&gt;an actual bug report&lt;/a&gt; down to the core problem so I could explain it.  Because we use blocks so much in Ruby, it's actually pretty easy to run into issues like this.&lt;/p&gt;

&lt;p&gt;The moral (in my opinion, of course):  &lt;code&gt;for&lt;/code&gt; is evil because it's surprising and hard to think through.  Avoid it.&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
</feed>
