<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.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>loopkid: the beauty of ruby 1.9</title>
    <link>http://loopkid.net/articles/2008/06/28/the-beauty-of-ruby-1-9</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>sad songs make me happy</description>
    <item>
      <title>the beauty of ruby 1.9</title>
      <description>&lt;p&gt;Let&amp;#8217;s say we have a string and three arrays&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string = "672"
java = []
ruby18 = []
ruby19 = []
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you want to convert the string to an array of integers. If you were writing Ruby in javastyle you would probably write something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;i=0
digits = string.split(//)
while i &amp;lt; digits.size do
  java &amp;lt;&amp;lt; digits[i].to_i
  i+=1
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but since we&amp;#8217;re we&amp;#8217;re doing Ruby in rubystyle it looks more like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string.split(//).each { |digit| ruby18 &amp;lt;&amp;lt; digit.to_i }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which looks &lt;em&gt;much&lt;/em&gt; nicer, but still not quite right, but with Ruby 1.9&amp;#8217;s &lt;a href="http://www.ruby-doc.org/core/classes/String.html#M000777"&gt;&lt;code&gt;chars&lt;/code&gt;&lt;/a&gt; interator it gets even better.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string.chars { |digit| ruby19 &amp;lt;&amp;lt; digit.to_i }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Goes down like butter, doesn&amp;#8217;t it? Isn&amp;#8217;t that how it always should have been? And the good thing is, there is lots of this good stuff in Ruby 1.9. Maybe in Ruby 2.0 there will even be a &lt;code&gt;to_i&lt;/code&gt; method in the array class, but now I&amp;#8217;m getting carried away.&lt;/p&gt;</description>
      <pubDate>Sat, 28 Jun 2008 12:11:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:f8db2c61-51ea-4fa3-9032-29377f0ed221</guid>
      <author>Stefan</author>
      <link>http://loopkid.net/articles/2008/06/28/the-beauty-of-ruby-1-9</link>
      <category>English</category>
      <category>Ruby</category>
      <trackback:ping>http://loopkid.net/articles/trackback/8426</trackback:ping>
    </item>
  </channel>
</rss>
