<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Alice, Bob, and Mallory: A simple loop</title>
    <link>http://alicebobandmallory.com/articles/2010/06/21/a-simple-loop</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>A simple loop</title>
      <description>&lt;p&gt;There's more than one way to skin a cat and the same is true for looping in Ruby. This is a silly post with a silly number of ways to  &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;print the integers from 1 to 10.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're a BASIC-programmer and are getting your feet wet with Ruby,  you might end up with something like this.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;a href="#n1" name="n1"&gt;1&lt;/a&gt;
&lt;a href="#n2" name="n2"&gt;2&lt;/a&gt;
&lt;a href="#n3" name="n3"&gt;3&lt;/a&gt;
&lt;a href="#n4" name="n4"&gt;4&lt;/a&gt;
&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span style="color:#080;font-weight:bold"&gt;while&lt;/span&gt; i&amp;lt;=&lt;span style="color:#00D"&gt;10&lt;/span&gt; &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;
   puts i
   i+=&lt;span style="color:#00D"&gt;1&lt;/span&gt;
&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
That and the following &lt;code&gt;for&lt;/code&gt;-loop is not the usual Ruby way of looping.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;a href="#n1" name="n1"&gt;1&lt;/a&gt;
&lt;a href="#n2" name="n2"&gt;2&lt;/a&gt;
&lt;a href="#n3" name="n3"&gt;3&lt;/a&gt;
&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; i &lt;span style="color:#080;font-weight:bold"&gt;in&lt;/span&gt; &lt;span style="color:#00D"&gt;1&lt;/span&gt;..&lt;span style="color:#00D"&gt;10&lt;/span&gt;
 puts i
&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
Instead rubyists often iterates over &lt;a href="http://ruby-doc.org/core/classes/Range.html"&gt;ranges&lt;/a&gt; or &lt;a href="http://ruby-doc.org/core/classes/Array.html"&gt;arrays&lt;/a&gt; with &lt;code&gt;each&lt;/code&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;
&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;(&lt;span style="color:#00D"&gt;1&lt;/span&gt;..&lt;span style="color:#00D"&gt;10&lt;/span&gt;).each {|i| puts i }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
But for simple integer loops like this, we also have &lt;code&gt;upto&lt;/code&gt;&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;
&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span style="color:#00D"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D"&gt;10&lt;/span&gt;) {|i| puts i }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
 and &lt;code&gt;times&lt;/code&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;
&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span style="color:#00D"&gt;10&lt;/span&gt;.times {|i| puts i+&lt;span style="color:#00D"&gt;1&lt;/span&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
Here's where I should've stopped but I can't help myself, I just have to show off with some &lt;a href="http://weblog.raganwald.com/2008/02/1100inject.html"&gt;Symbol#to_proc&lt;/a&gt; "magic".&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;
&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;(&lt;span style="color:#00D"&gt;0&lt;/span&gt;..&lt;span style="color:#00D"&gt;10&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:p&lt;/span&gt;)&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
The above works because &lt;code&gt;p&lt;/code&gt; is an alias of &lt;code&gt;puts&lt;/code&gt; and &lt;code&gt;&amp;amp;&lt;/code&gt; converts the symbol &lt;code&gt;:p&lt;/code&gt; to a proc that is called with the numbers in the range as parameters.&lt;/p&gt;

&lt;p&gt;The alias &lt;code&gt;p&lt;/code&gt; also gives us, what I think has to be, the shortest possible way.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;
&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;p *&lt;span style="color:#00D"&gt;1&lt;/span&gt;..&lt;span style="color:#00D"&gt;10&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;p&gt;You could argue that it's a bad thing that there are so many ways to do something as simple as this. But I see no big problem here, if any at all, even though these are hardly all possible ways to loop over integers in Ruby.&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jun 2010 21:56:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:f6e3d9ac-2271-4371-a87d-5a89303735e6</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/06/21/a-simple-loop</link>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
