<?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>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>professional procrastinator</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>
    <item>
      <title>"the beauty of ruby 1.9" by Stefan</title>
      <description>&lt;p&gt;Wow, beautiful! Thanks for your suggestion. I have actually seen the map function before, but never used it myself. Pretty elegant. Although I don&amp;#8217;t completely understand the ampersand-colon-syntax, it is explained &lt;a href="http://jlaine.net/2008/5/8/amp-lified"&gt;here&lt;/a&gt; and &lt;a href="http://books.google.de/books?id=kq2dBNdAl3IC&amp;amp;pg=PA203&amp;amp;lpg=PA203&amp;amp;source=bl&amp;amp;ots=0TT-g1Rvi9&amp;amp;sig=qM344Ks11J6Zgt8u9KfOzTzxXxw&amp;amp;hl=de&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;resnum=5&amp;amp;ct=result#PPA203,M1"&gt;here&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Mon, 02 Feb 2009 19:50:25 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:230da07d-0e6e-4b95-990e-338bb130cf4b</guid>
      <link>http://loopkid.net/articles/2008/06/28/the-beauty-of-ruby-1-9#comment-31165</link>
    </item>
    <item>
      <title>"the beauty of ruby 1.9" by Radarek</title>
      <description>&lt;p&gt;I know that you posted it some time ago but I have even simpler code for your problem:&lt;/p&gt;

&lt;p&gt;string.chars.map(&amp;amp;:to_i)&lt;/p&gt;

or even simpler!

&lt;p&gt;string.chars.map { |digit| digit.to_i }&lt;/p&gt;</description>
      <pubDate>Mon, 02 Feb 2009 17:51:07 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:171a02d9-5060-4cc2-a952-50b30d94dae8</guid>
      <link>http://loopkid.net/articles/2008/06/28/the-beauty-of-ruby-1-9#comment-31159</link>
    </item>
  </channel>
</rss>

