<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>jvoorhis comments on Ruby's Hidden do {} while () Loop</title>
    <link>http://www.jvoorhis.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>jvoorhis comments</description>
    <item>
      <title>"Ruby's Hidden do {} while () Loop": comment by Dr Nic</title>
      <description>&lt;p&gt;Hmm, I don&amp;#8217;t think I use the following syntax barely enough:&lt;/p&gt;


&lt;pre&gt;
@foo =
  begin
    ... do something ...
  end
&lt;/pre&gt;</description>
      <pubDate>Wed, 13 Jun 2007 19:04:18 NZST</pubDate>
      <guid>http://www.jvoorhis.com/articles/2007/06/13/ruby-hidden-do-while-loop#comment-512</guid>
      <link>http://www.jvoorhis.com/articles/2007/06/13/ruby-hidden-do-while-loop#comment-512</link>
    </item>
    <item>
      <title>"Ruby's Hidden do {} while () Loop": comment by Sam Smoot</title>
      <description>&lt;p&gt;Just a warning, but I heard tell on comp.lang.ruby once upon a time that this (post-conditional-loops) was to be removed in 2.0 in favor of flow-control within your loop.&lt;/p&gt;


	&lt;p&gt;That was probably the better part of a year ago though, and hell if I can find the thread.&lt;/p&gt;


	&lt;p&gt;So I&amp;#8217;ve avoided it since then, but maybe prematurely, since comp.lang.ruby seems loaded with threads on the construct with nary a whisper of deprecation. Count me as a fan of the post-conditional loop.&lt;/p&gt;</description>
      <pubDate>Wed, 13 Jun 2007 13:38:07 NZST</pubDate>
      <guid>http://www.jvoorhis.com/articles/2007/06/13/ruby-hidden-do-while-loop#comment-511</guid>
      <link>http://www.jvoorhis.com/articles/2007/06/13/ruby-hidden-do-while-loop#comment-511</link>
    </item>
    <item>
      <title>"Ruby's Hidden do {} while () Loop" by jvoorhis</title>
      <description>I found the following snippet while reading the source for &lt;code&gt;Tempfile#initialize&lt;/code&gt; in the Ruby core library:
&lt;pre&gt;&lt;code&gt;
begin
  tmpname = File.join(tmpdir, make_tmpname(basename, n))
  lock = tmpname + '.lock'
  n += 1
end while @@cleanlist.include?(tmpname) or
  File.exist?(lock) or File.exist?(tmpname)
&lt;/code&gt;&lt;/pre&gt;

At first glance, I assumed the &lt;code&gt;while&lt;/code&gt; modifier would be evaluated before the contents of &lt;code&gt;begin...end&lt;/code&gt;, but that is not the case. Observe:
&lt;pre&gt;&lt;code&gt;
&amp;gt;&amp;gt; begin
?&amp;gt;   puts "do {} while ()" 
&amp;gt;&amp;gt; end while false
do {} while ()
=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;
As you would expect, the loop will continue to execute while the modifier is true.
&lt;pre&gt;&lt;code&gt;
&amp;gt;&amp;gt; n = 3
=&amp;gt; 3
&amp;gt;&amp;gt; begin
?&amp;gt;   puts n
&amp;gt;&amp;gt;   n -= 1
&amp;gt;&amp;gt; end while n &amp;gt; 0
3
2
1
=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;

While I would be happy to never see this idiom again, &lt;code&gt;begin...end&lt;/code&gt; is quite powerful. The following is a common idiom to memoize a one-liner method with no params:
&lt;pre&gt;&lt;code&gt;
def expensive
  @expensive ||= 2 + 2
end
&lt;/code&gt;&lt;/pre&gt;
Here is an ugly, but quick way to memoize something more complex:
&lt;pre&gt;&lt;code&gt;
def expensive
  @expensive ||=
    begin
      n = 99
      buf = "" 
      begin
        buf &amp;lt;&amp;lt; "#{n} bottles of beer on the wall\n" 
        # ...
        n -= 1
      end while n &amp;gt; 0
      buf &amp;lt;&amp;lt; "no more bottles of beer" 
    end
end
&lt;/code&gt;&lt;/pre&gt;

</description>
      <pubDate>Wed, 13 Jun 2007 10:13:00 NZST</pubDate>
      <guid>&lt;a href="/articles/2007/06/13/ruby-hidden-do-while-loop"&gt;Ruby's Hidden do {} while () Loop&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2007/06/13/ruby-hidden-do-while-loop"&gt;Ruby's Hidden do {} while () Loop&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
