<?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>crunchlife: Code Snippet: Fibonacci with IronRuby &amp; WPF</title>
    <link>http://crunchlife.com/articles/2007/07/25/code-snippet-fibonacci-with-ironruby-wpf</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Code Snippet: Fibonacci with IronRuby &amp;amp; WPF</title>
      <description>&lt;p&gt;On Tuesday, John Lam &lt;a href="http://www.iunknown.com/2007/07/a-first-look-at.html"&gt;announced&lt;/a&gt; the Pre-Alpha Release of IronRuby.  To satisfy my curiosity, I downloaded and compiled the source code found on his blog.  A simple Build.cmd file was provided and made the compilation a breeze.  Rather than write another Hello World, I decided to kick the tires by writing a simple application to calculate &lt;a href="http://en.wikipedia.org/wiki/Fibonacci_number"&gt;Fibonacci numbers&lt;/a&gt;.  I didn&amp;#8217;t use many of the niceties that make Ruby what it is, but I did, however, uncover some oddities between Ruby and .NET types.  I coded around a few of the problems only to find other features that were either broken or missing.  The release was labeled &#8220;Pre-Alpha&#8221; for a reason so don&amp;#8217;t be surprised if you have a similar experience with IronRuby.&lt;/p&gt;

&lt;p&gt;I have to admit that I felt a bit dirty hacking together bits of the Windows Presentation Foundation (WPF) with Ruby only to achieve a single, ugly window with one text box, some words, and a button.  It wouldn&amp;#8217;t have been so bad if only I could have managed to use the .NET HorizontalAlignment enumeration with the controls on my window.  Every effort I made in aligning a control caused an Exception.  Defining the Orientation of my StackPanel also resulted in an Exception.  Taking input was easy.  Performing validation on the input was not.  I could not get a string comparison to work between .NET and Ruby string types.  I tried a half dozen combinations of .ToString, to_s, and Convert.ToString with no success.  I gave up and wrapped my problem expression in a begin-rescue block.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;begin&lt;/span&gt;
  &lt;span class="ident"&gt;fibonacci_label&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;content&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;fetch_iterative_fibonacci&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;Convert&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;ToInt32&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;input_text_box&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;Text&lt;/span&gt;&lt;span class="punct"&gt;)).&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt; 
&lt;span class="keyword"&gt;rescue&lt;/span&gt;
  &lt;span class="constant"&gt;MessageBox&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;show&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;Please enter an integer.&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If Microsoft creating their own implementation of Ruby isn&amp;#8217;t interesting enough, John Lam has said that IronRuby will be hosted at RubyForge.  His reason for this is simple.  John believes that Open Source developers have more experience using Subversion rather than Microsoft&amp;#8217;s Team Foundation Server.  I agree and believe this decision will only benefit IronRuby by involving the whole Ruby community.  This will most likely cause those non-Microsoft developers to contribute who otherwise wouldn&amp;#8217;t if the project were hosted at CodePlex.  Check out episode &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=254"&gt;254&lt;/a&gt; of .NET Rocks! for more on this and John Lam&amp;#8217;s work on IronRuby and the Dynamic Language Runtime.&lt;/p&gt;

&lt;p&gt;I am not disappointed with the status of this release.  Since IronRuby will be open for community contribution, the code will be reviewed by many eyes and the bugs that I did encounter will be fixed quickly.  The integration of IronRuby and WPF was not pleasant, but I do believe that over time the two technologies could compliment each other nicely.  The power of the presentation foundation combined with Ruby&amp;#8217;s syntax sugar will undoubtedly make for an impressive stack that could give others a run for their money.  Now can I have this in Mono please?&lt;/p&gt;

&lt;p&gt;Both an iterative and recursive solution can be found within my source.  I provided both in the odd chance that somebody reading this might want to try a rudimentary O(2^n) performance test between the Ruby interpretor and IronRuby.  The source code for my Fibonacci example can be downloaded from the links below.&lt;/p&gt;

&lt;p&gt;&lt;a href="/files/Fibonacci.tar.gz"&gt;Fibonacci.tar.gz&lt;/a&gt;&lt;br /&gt;
&lt;a href="/files/Fibonacci.zip"&gt;Fibonacci.zip&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 25 Jul 2007 21:28:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:49ef81bb-2fa7-4ea4-a192-2e63932308cd</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2007/07/25/code-snippet-fibonacci-with-ironruby-wpf</link>
      <category>Code Snippets</category>
      <category>Ruby</category>
    </item>
    <item>
      <title>"Code Snippet: Fibonacci with IronRuby &amp; WPF" by Ryan Baxter</title>
      <description>Thank you, John, for your effort and the .to_clr_string tip!  I'll be looking forward to the next release.  Keep up the good work.</description>
      <pubDate>Thu, 26 Jul 2007 05:45:04 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:626e7e45-93e0-4e91-884e-777ca5eb9111</guid>
      <link>http://crunchlife.com/articles/2007/07/25/code-snippet-fibonacci-with-ironruby-wpf#comment-15</link>
    </item>
    <item>
      <title>"Code Snippet: Fibonacci with IronRuby &amp; WPF" by John Lam</title>
      <description>Thanks for writing this up, Ryan!

We hacked in support for .NET interop very late in this release cycle. We had interop working before, did a major architectural change, and had to hack back in some old code to enable interop for this release. The interop code will be completely re-written in the next few weeks, so things like missing enumerations, the marshaling of strings (use the magic .to_clr_string method for now) and others will become MUCH more stable.

Hang in there!</description>
      <pubDate>Wed, 25 Jul 2007 23:06:24 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:31fe08ac-347a-4743-8240-f77769010656</guid>
      <link>http://crunchlife.com/articles/2007/07/25/code-snippet-fibonacci-with-ironruby-wpf#comment-14</link>
    </item>
  </channel>
</rss>

