<?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</title>
    <link>http://crunchlife.com</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Ruby Fractal Library</title>
      <description>&lt;p&gt;&lt;img src="/files/mandelbrot1.png" class="right"&gt;Last October, I wrote a small &lt;a href="http://crunchlife.com/articles/2007/10/30/code-snippet-ruby-walks-with-shoes" target="_blank"&gt;fractal rendering program&lt;/a&gt; in Ruby using the &lt;a href="http://code.whytheluckystiff.net/shoes/" target="_blank"&gt;Shoes&lt;/a&gt; windowing toolkit written by &lt;a href="http://en.wikipedia.org/wiki/Why_the_lucky_stiff" target="_blank"&gt;why the lucky stiff&lt;/a&gt;. It&amp;#8217;s sole purpose was to test Shoes. The code was painfully slow at rendering the Mandelbrot set, but it did, however, begin a small obsession of mine with fractals. &lt;/p&gt;

&lt;p&gt;Since I haven&amp;#8217;t been able to find a fractal library for Ruby, I decided to write one. Over the last two weeks I&amp;#8217;ve written some code to generate both the &lt;a href="http://en.wikipedia.org/wiki/Mandelbrot_set" target="_blank"&gt;Mandelbrot&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Julia_set" target="_blank"&gt;Julia set&lt;/a&gt; fractals using the escape time algorithm. The code is still slow, but within a couple weeks I hope to replace the slow portions with inline C.&lt;/p&gt;

&lt;p&gt;There may still be some bugs and I haven&amp;#8217;t added any error handling, but here it is. An &amp;#8220;almost&amp;#8221; pure Ruby fractal library. Once this is cleaned up I&amp;#8217;ll repost the code. I suppose a gem could be possible as well. Happy 4th!&lt;/p&gt;

&lt;p&gt;&lt;a href="/files/fractals.rb"&gt;fractals.rb&lt;/a&gt;&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# RB&lt;/span&gt;

&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rubygems&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;complex&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;png&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Fractals&lt;/span&gt;
  &lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Fractal&lt;/span&gt;
    &lt;span class="ident"&gt;attr_accessor&lt;/span&gt; &lt;span class="symbol"&gt;:begin_range&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:end_range&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;begin_range&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;end_range&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="attribute"&gt;@begin_range&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="attribute"&gt;@end_range&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;begin_range&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;end_range&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;draw&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;height&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;250&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;width&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;250&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;m&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;1.0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;
      &lt;span class="ident"&gt;save_as&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;class&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;downcase&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;.png&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
        &lt;span class="ident"&gt;canvas&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;PNG&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Canvas&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;height&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;width&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;

        &lt;span class="comment"&gt;# Find the complex coordinate for each pixel.&lt;/span&gt;
        &lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;upto&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;height&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;y&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
          &lt;span class="ident"&gt;i&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;y&lt;/span&gt; &lt;span class="punct"&gt;*&lt;/span&gt; &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="attribute"&gt;@end_range&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;image&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt; &lt;span class="attribute"&gt;@begin_range&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;image&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;/&lt;/span&gt; &lt;span class="ident"&gt;height&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt;
          &lt;span class="attribute"&gt;@begin_range&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;image&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;*&lt;/span&gt; &lt;span class="ident"&gt;m&lt;/span&gt;
          &lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;upto&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;width&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;x&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
            &lt;span class="ident"&gt;r&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;x&lt;/span&gt; &lt;span class="punct"&gt;*&lt;/span&gt; &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="attribute"&gt;@end_range&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;real&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt; &lt;span class="attribute"&gt;@begin_range&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;real&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;/&lt;/span&gt; &lt;span class="ident"&gt;width&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt;
            &lt;span class="attribute"&gt;@begin_range&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;real&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;*&lt;/span&gt; &lt;span class="ident"&gt;m&lt;/span&gt;
            &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;in_set?&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;Complex&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;r&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;i&lt;/span&gt;&lt;span class="punct"&gt;))&lt;/span&gt; &lt;span class="keyword"&gt;then&lt;/span&gt;
              &lt;span class="ident"&gt;canvas&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;x&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;y&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;PNG&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Color&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Black&lt;/span&gt;
            &lt;span class="keyword"&gt;else&lt;/span&gt;
              &lt;span class="ident"&gt;canvas&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;x&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;y&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;fetch_color&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;last_iteration&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;max_iterations&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
            &lt;span class="keyword"&gt;end&lt;/span&gt;                    
          &lt;span class="punct"&gt;}&lt;/span&gt;
        &lt;span class="punct"&gt;}&lt;/span&gt;

        &lt;span class="ident"&gt;png&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;PNG&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;canvas&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
        &lt;span class="ident"&gt;png&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;save&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;save_as&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;  

    &lt;span class="ident"&gt;private&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;fetch_color&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;last_iteration&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;max_iterations&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;  
      &lt;span class="ident"&gt;divisor&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;765&lt;/span&gt;&lt;span class="punct"&gt;*&lt;/span&gt;&lt;span class="ident"&gt;last_iteration&lt;/span&gt;&lt;span class="punct"&gt;/&lt;/span&gt;&lt;span class="ident"&gt;max_iterations&lt;/span&gt;
      &lt;span class="keyword"&gt;case&lt;/span&gt; &lt;span class="ident"&gt;divisor&lt;/span&gt;
        &lt;span class="keyword"&gt;when&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;..&lt;/span&gt;&lt;span class="number"&gt;254&lt;/span&gt; &lt;span class="keyword"&gt;then&lt;/span&gt; &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;PNG&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Color&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;divisor&lt;/span&gt;&lt;span class="punct"&gt;%&lt;/span&gt;&lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
        &lt;span class="keyword"&gt;when&lt;/span&gt; &lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;..&lt;/span&gt;&lt;span class="number"&gt;509&lt;/span&gt; &lt;span class="keyword"&gt;then&lt;/span&gt; &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;PNG&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Color&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;divisor&lt;/span&gt;&lt;span class="punct"&gt;%&lt;/span&gt;&lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
        &lt;span class="keyword"&gt;when&lt;/span&gt; &lt;span class="number"&gt;510&lt;/span&gt;&lt;span class="punct"&gt;..&lt;/span&gt;&lt;span class="number"&gt;765&lt;/span&gt; &lt;span class="keyword"&gt;then&lt;/span&gt; &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;PNG&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Color&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;divisor&lt;/span&gt;&lt;span class="punct"&gt;%&lt;/span&gt;&lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;255&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;       
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Julia&lt;/span&gt;
    &lt;span class="ident"&gt;include&lt;/span&gt; &lt;span class="constant"&gt;Fractal&lt;/span&gt;
    &lt;span class="ident"&gt;attr_accessor&lt;/span&gt; &lt;span class="symbol"&gt;:seed&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:bailout&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:max_iterations&lt;/span&gt;
    &lt;span class="ident"&gt;attr_reader&lt;/span&gt; &lt;span class="symbol"&gt;:last_iteration&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;seed&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;Complex&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;0.36&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;0.1&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt; &lt;span class="ident"&gt;bailout&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;max_iterations&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;100&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;
    &lt;span class="ident"&gt;begin_range&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;Complex&lt;/span&gt;&lt;span class="punct"&gt;(-&lt;/span&gt;&lt;span class="number"&gt;2.25&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt;&lt;span class="number"&gt;1.5&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt; &lt;span class="ident"&gt;end_range&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;Complex&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;0.75&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;1.5&lt;/span&gt;&lt;span class="punct"&gt;))&lt;/span&gt;
      &lt;span class="keyword"&gt;super&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;begin_range&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;end_range&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="attribute"&gt;@seed&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="attribute"&gt;@bailout&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="attribute"&gt;@max_iterations&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;seed&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;bailout&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;max_iterations&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;in_set?&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;z&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="attribute"&gt;@max_iterations&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;times&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;i&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
        &lt;span class="ident"&gt;z&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;z&lt;/span&gt;&lt;span class="punct"&gt;**&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="attribute"&gt;@seed&lt;/span&gt;
        &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;z&lt;/span&gt; &lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="attribute"&gt;@bailout&lt;/span&gt; &lt;span class="keyword"&gt;then&lt;/span&gt;
          &lt;span class="attribute"&gt;@last_iteration&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;i&lt;/span&gt;
          &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;false&lt;/span&gt;
        &lt;span class="keyword"&gt;end&lt;/span&gt;      
      &lt;span class="punct"&gt;}&lt;/span&gt;
      &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Mandelbrot&lt;/span&gt;
    &lt;span class="ident"&gt;include&lt;/span&gt; &lt;span class="constant"&gt;Fractal&lt;/span&gt;  
    &lt;span class="ident"&gt;attr_accessor&lt;/span&gt; &lt;span class="symbol"&gt;:bailout&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:max_iterations&lt;/span&gt;
    &lt;span class="ident"&gt;attr_reader&lt;/span&gt; &lt;span class="symbol"&gt;:last_iteration&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;bailout&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;5&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;max_iterations&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;100&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;begin_range&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;Complex&lt;/span&gt;&lt;span class="punct"&gt;(-&lt;/span&gt;&lt;span class="number"&gt;2.25&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;
    &lt;span class="punct"&gt;-&lt;/span&gt;&lt;span class="number"&gt;1.5&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt; &lt;span class="ident"&gt;end_range&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;Complex&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;0.75&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;1.5&lt;/span&gt;&lt;span class="punct"&gt;))&lt;/span&gt;
      &lt;span class="keyword"&gt;super&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;begin_range&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;end_range&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="attribute"&gt;@bailout&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="attribute"&gt;@max_iterations&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;bailout&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;max_iterations&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;in_set?&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;c&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="ident"&gt;z&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
      &lt;span class="attribute"&gt;@max_iterations&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;times&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;i&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
        &lt;span class="ident"&gt;z&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;z&lt;/span&gt;&lt;span class="punct"&gt;**&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="ident"&gt;c&lt;/span&gt;
        &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;z&lt;/span&gt; &lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="attribute"&gt;@bailout&lt;/span&gt; &lt;span class="keyword"&gt;then&lt;/span&gt;
          &lt;span class="attribute"&gt;@last_iteration&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;i&lt;/span&gt;
          &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;false&lt;/span&gt;
        &lt;span class="keyword"&gt;end&lt;/span&gt;            
      &lt;span class="punct"&gt;}&lt;/span&gt;
      &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using this library is as simple as the following:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;fractals&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="ident"&gt;mandelbrot&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Fractals&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Mandelbrot&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
&lt;span class="ident"&gt;mandelbrot&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;draw&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Any suggestions/bug fixes can be posted here. Thanks.&lt;/p&gt;</description>
      <pubDate>Thu, 03 Jul 2008 14:26:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:c65638d5-d30a-4340-a3f4-34b9ec4eb55f</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/07/03/ruby-fractal-library</link>
      <category>Code Snippets</category>
      <category>Ruby</category>
      <category>Fractals</category>
      <enclosure url="http://crunchlife.com/files/mandelbrot1.png" type="image/png" length="9532"/>
      <trackback:ping>http://crunchlife.com/articles/trackback/70</trackback:ping>
    </item>
    <item>
      <title>These are a Few of My Favorite Things</title>
      <description>&lt;p&gt;A few of my favorite applications published major releases this week. The most known being the &lt;a href="http://www.mozilla.com/firefox/" target="_blank"&gt;Firefox web browser&lt;/a&gt;. On Tuesday, the Mozilla team launched version 3 of their flagship product, Mozilla Firefox. It includes many bug fixes and an enhanced bookmarking system (love it). Within 24 hours, Firefox wracked up over 8 million downloads making it the most downloaded software ever.&lt;/p&gt;

&lt;p&gt;The venerable &lt;a href="http://www.winehq.org/" target="_blank"&gt;Wine (Wine Is Not an Emulator)&lt;/a&gt; project finally reached version 1.0. For those not familiar with Unix-like operating systems, Wine allows users to execute Microsoft Windows programs on Linux, Mac OS X, FreeBSD, and Solaris. It&amp;#8217;s a must have for those who just can&amp;#8217;t quite give up their favorite Windows software.&lt;/p&gt;

&lt;p&gt;This may not be considered a major release, but the folks at reddit have &lt;a href="http://blog.reddit.com/2008/06/reddit-goes-open-source.html" target="_blank"&gt;announced&lt;/a&gt; the open-sourcing of their social news website, &lt;a href="http://www.reddit.com" target="_blank"&gt;reddit.com&lt;/a&gt;. IMO reddit.com takes the best elements of both slashdot.org and digg.com and combines them to make something even better. Kudos reddit, Mozilla, and Wine!&lt;/p&gt;</description>
      <pubDate>Fri, 20 Jun 2008 10:17:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:78b2b93b-0900-42d8-acf4-12728393c42c</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/06/20/these-are-a-few-of-my-favorite-things</link>
      <category>Inspiration</category>
      <category>Life</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/69</trackback:ping>
    </item>
    <item>
      <title>IE7's Inanimate GIF</title>
      <description>&lt;p&gt;Animated GIFs are most often used as activity indicators in modern &lt;a
href="http://en.wikipedia.org/wiki/Ajax_(programming)"
target="_blank"&gt;AJAX (asynchronous JavaScript and XML)&lt;/a&gt; enabled
websites. I decided to use a &lt;a
href="http://en.wikipedia.org/wiki/Gif" target="_blank"&gt;GIF (Graphic
Interchange Format)&lt;/a&gt; from &lt;a href="http://www.ajaxload.info/"
target="_blank"&gt;ajaxload.info&lt;/a&gt; on an application that I&amp;#8217;ve been
developing at work. The application performs server-side
processing on files uploaded by employees. Processing time varies depending on the size of the file. Larger files take longer.&lt;/p&gt;

&lt;p&gt;I wanted the animated GIF to appear when the file upload button was clicked so I placed the image within a &lt;a href="http://www.w3schools.com/tags/tag_DIV.asp" target="_blank"&gt;DIV&lt;/a&gt; tag and hid it by setting a blank &lt;a href="http://www.w3schools.com/css/pr_class_display.asp" target="_blank"&gt;CSS display property&lt;/a&gt;. OnClick of the upload button, a
JavaScript function toggled the display value to &amp;#8220;block&amp;#8221;, making the DIV appear. This worked as
expected in &lt;a href="http://www.mozilla.com/en-US/firefox/?utm_id=Q108&amp;amp;utm_source=google&amp;amp;utm_medium=ppc&amp;amp;gclid=CPnY35q27JMCFQtvGgodcnzhWg" target="_blank"&gt;Firefox&lt;/a&gt; and IE6, but not in IE7. The DIV appeared in IE7,
but it&amp;#8217;s GIF wasn&amp;#8217;t moving.&lt;/p&gt;

&lt;p&gt;A little googling turned up a helpful comment on &lt;a href="http://west-wind.com/WebLog/default.aspx" target="_blank"&gt;Rick Strahl&amp;#8217;s blog&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
I&amp;#8217;ve run into a problem with animated gifs inside of a hidden area of a Web page that is hidden with style.display=&amp;#8217;none&amp;#8217;. When the area is made visible again, in Internet Explorer this causes the image to not be displayed an animated GIF whatever I try. [sic] 
&lt;/blockquote&gt;

&lt;p&gt;Apparently IE7 doesn&amp;#8217;t like to animate hidden GIFs. User submitted comments on Rick Strahl&amp;#8217;s blog provided many solutions, but only a couple worked in my situation. The first uses the JavaScript setTimeout method to populate the image&amp;#8217;s SRC attribute 200 ms after the function call. The second sets the DIV&amp;#8217;s innerHTML to a string containing an &lt;a href="http://www.w3schools.com/tags/tag_IMG.asp" target="_blank"&gt;IMG&lt;/a&gt; tag with the animated GIF.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&amp;lt;script language='javascript'&amp;gt; 
    function ShowLoading(elementId) 
    {
        document.getElementById(elementId).style.display = &amp;quot;block&amp;quot;; 
        setTimeout('document.images[&amp;quot;loadingImage&amp;quot;].src = &amp;quot;../images/loading.gif&amp;quot;', 200); 
    } 
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Neither solution is ideal, but setting the element&amp;#8217;s innerHTML felt a little bit cleaner to me.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&amp;lt;script language='javascript'&amp;gt;
    function ShowLoading(elementId)
    {   
        var element = document.getElementById(elementId);    

        element.innerHTML = &amp;quot;&amp;lt;img src='../images/loading.gif'&amp;gt;&amp;quot;;    
        element.style.display = &amp;quot;block&amp;quot;;
    }
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;
*IE7 may have animations disabled. Go to Tools &gt; Internet Options &gt; Advanced &gt; Multimedia. Checking &amp;#8220;Play animations in webpages&amp;#8221; may affect how IE7 renders your animated GIFs.
&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 11 Jun 2008 05:06:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:a2283586-36cd-4064-8291-8155c59f2cf9</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/06/11/ie7s-inanimate-gif</link>
      <category>Code Snippets</category>
      <category>Expect the Unexpected</category>
      <category>ASPNET</category>
      <category>JavaScript</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/68</trackback:ping>
    </item>
    <item>
      <title>Samba Network Shares with Nautilus in Hardy Heron Part 2</title>
      <description>&lt;p&gt;&lt;img src="/files/ubuntu_logo.png" class="right"&gt;Nearly a month ago I wrote about my &lt;a href="http://crunchlife.com/articles/2008/04/28/samba-network-shares-with-nautilus-in-hardy-heron" target="_blank"&gt;problems connecting to the network shares&lt;/a&gt; of my &lt;a href="http://crunchlife.com/articles/2007/08/03/review-linksys-nas200" target="_blank"&gt;Linksys NAS200&lt;/a&gt; using Nautilus in Ubuntu, Hardy Heron. My fix was simple and it worked. Unfortunately, the solution&amp;#8217;s &lt;a href="http://en.wikipedia.org/wiki/Woman_acceptance_factor" target="_blank"&gt;WAF (Wife Acceptance Factor)&lt;/a&gt; was low. Apparently she didn&amp;#8217;t think it was easy enough to execute smbmount from a Terminal window. Whatever :) I have since written a small shell script that is called from Session Startup.&lt;/p&gt;

&lt;p&gt;Initially, the script failed to connect to my network shares because my wireless network connection hadn&amp;#8217;t finished negotiating before the script was executed. I coded around this by creating a while loop that &lt;a href="http://en.wikipedia.org/wiki/Grep#Usage_as_a_conversational_verb" target="_blank"&gt;greps&lt;/a&gt; the output of a ping to my router. If successful, the mount_shares function is called. Otherwise. the thread will sleep for 10 seconds and try again.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;#!/bin/bash

user=&amp;quot;thorbardin/ryan%password&amp;quot;
root_dir=&amp;quot;/home/ryan/Network Shares&amp;quot;

mount_shares() {
  public_dir=&amp;quot;$root_dir&amp;quot;/&amp;quot;Public on Thorbardin&amp;quot;
  home_dir=&amp;quot;$root_dir&amp;quot;/&amp;quot;Home on Thorbardin&amp;quot;

  [ -d &amp;quot;$public_dir&amp;quot; ] || mkdir -p &amp;quot;$public_dir&amp;quot;
  [ -d &amp;quot;$home_dir&amp;quot; ] || mkdir -p &amp;quot;$home_dir&amp;quot;

  smbmount &amp;quot;//192.168.1.105/public disk&amp;quot; &amp;quot;$public_dir&amp;quot; -o user=&amp;quot;$user&amp;quot;
  smbmount &amp;quot;//192.168.1.105/ryan&amp;quot; &amp;quot;$home_dir&amp;quot; -o user=&amp;quot;$user&amp;quot;
}

while [ 1 ]; do
  if ping -c2 192.168.1.1 2&amp;gt;&amp;amp;1 | grep ttl; then
    mount_shares

    exit 0   
  fi
  sleep 10
done

exit 0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#8217;m already thinking about rewriting this script so that it scans my network using &lt;a href="http://www.menet.umn.edu/docs/software/samba/3.0.14a/help/smbtree.1.html" target="_blank"&gt;smbtree&lt;/a&gt; and automatically mounts all available network shares. That&amp;#8217;ll be Part 3!&lt;/p&gt;</description>
      <pubDate>Sun, 01 Jun 2008 16:07:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:d387ed9b-c3b3-469c-8052-8514021774bc</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/06/01/samba-network-shares-with-nautilus-in-hardy-heron-part-2</link>
      <category>Code Snippets</category>
      <category>NAS</category>
      <category>Ubuntu</category>
      <category>Oops</category>
      <category>Bash</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/67</trackback:ping>
    </item>
    <item>
      <title>Temporary Identity Impersonation in ASP.NET</title>
      <description>&lt;p&gt;Uploading files in an ASP.NET application is relatively easy to do. Uploading to a remote machine is a little bit trickier, but certainly doable. I followed a &lt;a href="http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all" target="_blank"&gt;set of instructions&lt;/a&gt; on aspalliance.com, but rather than declare an account to impersonate in my web.config file, I decided to do it in code. That way I could use impersonation only when needed and encapsulate it for later use.&lt;/p&gt;

&lt;p&gt;I referred to an &lt;a href="http://support.microsoft.com/kb/306158#4" target="_blank"&gt;article&lt;/a&gt; on Microsoft&amp;#8217;s Help and Support website about how to implement impersonation. Their code worked great, but I decided to put it in a class to help keep things &lt;a href="http://en.wikipedia.org/wiki/DRY" target="_blank"&gt;DRY&lt;/a&gt;.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;using System;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Runtime.InteropServices;

namespace Utilities
{    
    public class ImpersonateUser
    {
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;

        WindowsImpersonationContext impersonationContext;

        [DllImport(&amp;quot;advapi32.dll&amp;quot;)]
        public static extern int LogonUserA(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int DuplicateToken(IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);

        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();

        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);

        public bool ImpersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            if (RevertToSelf())
            {
                if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if (impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }
            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
        }

        public void UndoImpersonation()
        {
            impersonationContext.Undo();
        }
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After referencing my Utilities namespace I was then able to impersonate the account required for uploading:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;if (FileUpload1.HasFile)
{
    ImpersonateUser impersonateUser = new ImpersonateUser();

    if (impersonateUser.ImpersonateValidUser(&amp;quot;userName&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;password&amp;quot;))
    {
        FileUpload1.SaveAs(Server.MapPath(&amp;quot;~/files/fileName.txt&amp;quot;));
        impersonateUser.UndoImpersonation();
    }
    else
    {
        throw new Exception(&amp;quot;Identity impersonation has failed.&amp;quot;);
    }   
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;*The SaveAs method of the FileUpload control requires a root path. Using Server.MapPath will provide the root path of your IIS virtual folder.&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 27 May 2008 09:01:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:9aea73ef-a3d1-4208-9128-12eb032da707</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/05/27/temporary-identity-impersonation-in-asp-net</link>
      <category>Code Snippets</category>
      <category>ASPNET</category>
      <category>dotNET</category>
      <enclosure url="http://crunchlife.com/files/lock.jpg" type="image/jpeg" length="60658"/>
      <trackback:ping>http://crunchlife.com/articles/trackback/66</trackback:ping>
    </item>
    <item>
      <title>DIY Multi-Touch Pad</title>
      <description>&lt;p&gt;&lt;object width="425" height="355"&gt;&lt;param name="movie" value="http://www.youtube.com/v/pQpr3W-YmcQ&amp;amp;hl=en"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/pQpr3W-YmcQ&amp;amp;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 23 May 2008 09:04:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:5c7fa795-b338-4a68-8c46-1470be66f662</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/05/23/diy-multi-touch-pad</link>
      <category>Inspiration</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/65</trackback:ping>
    </item>
    <item>
      <title>A Plea to New Communities</title>
      <description>&lt;p&gt;Mark Pilgrim, a Technical Writer at Google, explains his new project, &lt;a href="http://code.google.com/p/doctype/wiki/Welcome?tm=6" target="_blank"&gt;Google DocType&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;object width="425" height="355"&gt;&lt;param name="movie" value="http://www.youtube.com/v/MmUiKaHWjLI"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/MmUiKaHWjLI" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;

&lt;p&gt;According to Mark, Google DocType will be an open resource for sharing web programming knowledge. As of today, it contains HOWTO articles on &lt;a href="http://en.wikipedia.org/wiki/CSS" target="_blank"&gt;CSS (Cascading Style Sheets)&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Document_Object_Model" target="_blank"&gt;DOM (Document Object Model)&lt;/a&gt; manipulation, and Web security. Links to CSS and HTML references round out the site&amp;#8217;s content. I have no doubt that DocType is just the beginning for Google, but will this be just another W3Schools?&lt;/p&gt;

&lt;p&gt;In numerous posts on codinghorror.com, Jeff Atwood has suggested that programmers &lt;a href="http://www.codinghorror.com/blog/archives/001108.html" target="_blank"&gt;no longer read books&lt;/a&gt;. Jeff believes the Internet is most programmers&#8217; first reference choice. He has since &lt;a href="http://www.codinghorror.com/blog/archives/001101.html" target="_blank"&gt;announced a partnership&lt;/a&gt; with Joel Spolsky to create a community for developers. In his words:&lt;/p&gt;

&lt;blockquote&gt;There&amp;#8217;s far too much great programming information trapped in forums, buried in online help, or hidden away in books that nobody buys any more. We&amp;#8217;d like to unlock all that. Let&amp;#8217;s create something that makes it easy to participate, and put it online in a form that is trivially easy to find.&lt;/blockquote&gt;

&lt;p&gt;My plea to both of these fledgling projects is best stated in the last sentence of the above quote. Please make the information trivially easy to find. I&#8217;m one of those developers using the Internet as a programming reference and I&#8217;m tired of searching for the proverbial needle in a haystack.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Search_engine_optimization" target="_blank"&gt;SEO (Search Engine Optimization)&lt;/a&gt; isn&#8217;t the only answer.  Too many times keywords return results containing solutions for yesterday&#8217;s problems.  What worked yesterday may not be today&#8217;s answer.  Operating systems, software dependencies, and programming languages all change.  How do we keep content relevant?  Unfortunately I don&#8217;t have an answer to this problem, but perhaps Mark, Jeff, or Joel can come up with something profound. I wish them luck.&lt;/p&gt;</description>
      <pubDate>Wed, 21 May 2008 06:57:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:0f2ca7d2-3f79-44b5-ad0d-893066ec07ea</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/05/21/a-plea-to-new-communities</link>
      <category>Programming</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/64</trackback:ping>
    </item>
    <item>
      <title>Rockscroll Visual Studio Add-in</title>
      <description>&lt;p&gt;&lt;img src="/files/rockscroll.jpg" class="right" /&gt;Last Friday, &lt;a href="http://www.hanselman.com/blog/" target="_blank"&gt;Scott Hanselman&lt;/a&gt; announced the release of the &lt;a href="http://www.hanselman.com/blog/IntroducingRockScroll.aspx" target="_blank"&gt;Rockscroll&lt;/a&gt; Visual Studio Add-in. Rockscroll replaces Visual Studio&amp;#8217;s code-view scrollbar with a miniature visual representation of your source code. This feature makes navigation in large source files that much easier.&lt;/p&gt;

&lt;p&gt;Before Rockscroll, I&#8217;d oftentimes make mental notes of the scrollbar&#8217;s position while traversing code. That, and when spending a lot of time on a project I&#8217;d become so familiar with the source code that I&#8217;d begin to recognize most code blocks by shape. Rockscroll provides both of these &lt;a href="http://en.wikipedia.org/wiki/Mnemonic" target="_blank"&gt;mnemonic devices&lt;/a&gt;, but with less brain work. That is a good thing. Thank you, Rocky Downs. Can I have this in NetBeans? :)&lt;/p&gt;</description>
      <pubDate>Tue, 13 May 2008 06:32:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:39041988-d9be-43b4-a6e3-b5ea8de816c0</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/05/13/rockscroll-visual-studio-add-in</link>
      <category>Visual Studio</category>
      <category>dotNET</category>
    </item>
    <item>
      <title>Quote of the Day</title>
      <description>&lt;p&gt;&lt;strong&gt;&lt;i&gt;XML is like violence. If it doesn&amp;#8217;t solve your problem, you&amp;#8217;re not using enough of it.&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I got a chuckle out of reading this in a comment  on &lt;a href="http://www.codinghorror.com/blog/" target="_blank"&gt;Jeff Atwood&lt;/a&gt;&amp;#8217;s latest post, &lt;a href="http://www.codinghorror.com/blog/archives/001114.html" target="_blank"&gt;XML: The Angle Bracket Tax&lt;/a&gt;. The quote is more than fitting for the XML I&amp;#8217;ve been working with lately. For the severely twisted, check out the &lt;a href="http://en.wikipedia.org/wiki/OpenDocument" target="_blank"&gt;ODF (OpenDocument Format)&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats" target="_blank"&gt;OpenXML (Office Open XML)&lt;/a&gt; document file formats.&lt;/p&gt;</description>
      <pubDate>Mon, 12 May 2008 08:01:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:8957c884-ec3b-4da4-bfb1-735c5d728fac</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/05/12/quote-of-the-day</link>
      <category>XML</category>
    </item>
    <item>
      <title>A Few Date Methods</title>
      <description>&lt;p&gt;An ASP.NET project of mine recently required the calculation of the start and end date of the current date&amp;#8217;s previous month. This was more difficult putting into words than code.  It did, however, get me thinking about other common date routines.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public static DateTime FirstDayOfPreviousMonth
{
    get
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1);
    }    
}

public static DateTime LastDayOfPreviousMonth
{
    get
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Being preemptive, I decided to include a few methods for determining the start and end date of the current date&amp;#8217;s fiscal quarter. I started by calculating the current date&amp;#8217;s quarter. This was accomplished with just a little division.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public static int CurrentQuarter
{
    get
    {
        return (DateTime.Now.Month + 2) / 3;
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finding the start and end dates of the current quarter was harder, but could still be expressed in a single line of code (or two).&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public static DateTime FirstDayOfCurrentQuarter
{
    get
    {
        return new DateTime(DateTime.Now.Year, 1 + ((CurrentQuarter - 1) * 3), 1);
    }
}

public static DateTime LastDayOfCurrentQuarter
{
    get
    {
        int lastMonthOfCurrentQuarter = 3 + ((CurrentQuarter - 1) * 3);

        return new DateTime(DateTime.Now.Year, lastMonthOfCurrentQuarter, DateTime.DaysInMonth(DateTime.Now.Year, lastMonthOfCurrentQuarter));
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#8217;d like to extend this collection to include other common date routines. Feel free to post your date methods as comments if you&amp;#8217;d like to share.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;*The methods I&amp;#8217;ve created for calculating the current quarter&amp;#8217;s start and end dates assume the fiscal year starts on January 1st. This may not be suitable for your needs.&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 06 May 2008 11:45:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:0f4f646c-7a7d-42c8-b6b1-306d40200836</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/05/06/a-few-date-methods</link>
      <category>Code Snippets</category>
      <category>dotNET</category>
      <category>CSharp</category>
      <enclosure url="http://crunchlife.com/files/clock.jpg" type="image/jpeg" length="22591"/>
    </item>
    <item>
      <title>Samba Network Shares with Nautilus in Hardy Heron</title>
      <description>&lt;p&gt;&lt;img src="/files/ubuntu_logo.png" class="right"&gt;A couple of days ago I mentioned &lt;a href="http://crunchlife.com/articles/2008/04/26/ruby-on-rails-with-hardy-heron" target="_blank"&gt;a few of my woes&lt;/a&gt; with the latest release of Ubuntu. Since then, I&amp;#8217;ve installed Ruby on Rails and have found a work around for the continued problems I&amp;#8217;ve had in accessing &lt;a href="http://crunchlife.com/articles/2007/08/03/review-linksys-nas200" target="_blank"&gt;my samba shares&lt;/a&gt; using the &amp;#8220;Connect to Server&amp;#8230;&amp;#8221; option in Nautilus. If you&amp;#8217;re experiencing the symptoms described in &lt;a href="https://bugs.launchpad.net/ubuntu/+source/samba/+bug/208531" target="_blank"&gt;bug report #208531&lt;/a&gt;, then you might want to give &lt;a href="http://en.wikipedia.org/wiki/Smbmount" target="_blank"&gt;smbmount&lt;/a&gt; a try. From the command-line, type:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;smbmount //{server}/{share} /home/{user}/{share} -o username={server_username}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To unmount the share:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;sudo umount /home/{user}/{share}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#8217;m using this as a temporary solution until the &lt;a href="https://bugs.launchpad.net/ubuntu/+source/samba/+bug/208531" target="_blank"&gt;bug in Nautilus&lt;/a&gt; is resolved. Your mileage may vary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;*The statements above were written under the assumption that you&amp;#8217;ve created a folder within your home directory for mounting the share.&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 28 Apr 2008 17:50:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:769275e1-c2fe-46f2-b3fc-84c5f86c48d2</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/04/28/samba-network-shares-with-nautilus-in-hardy-heron</link>
      <category>NAS</category>
      <category>Ubuntu</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/60</trackback:ping>
    </item>
    <item>
      <title>Ruby on Rails with Hardy Heron</title>
      <description>&lt;p&gt;&lt;img src="/files/ruby.png" class="right"&gt;This Thursday, Canonical released &lt;a href="http://www.ubuntu.com/" target="_blank"&gt;Ubuntu&lt;/a&gt;, Hardy Heron, into the wild. Like many others, I immediately fired up a torrent and began downloading. From what I read of the aftermath, many launch day downloaders were not as lucky. Most Torrent servers became immediately inaccessible due to Hardy Heron&amp;#8217;s popular demand. It has been three days and the US update servers are still saturated. This happens every release and can only mean that Ubuntu&amp;#8217;s userbase is steadily growing.&lt;/p&gt;

&lt;p&gt;My installation went well, but with any new OS release comes problems and Hardy Heron was not an exception. I couldn&amp;#8217;t connect to &lt;a href="http://crunchlife.com/articles/2007/08/03/review-linksys-nas200" target="_blank"&gt;my NAS&lt;/a&gt; through Nautilus. None of my browser extensions worked and &lt;a href="http://www.rubyonrails.org/" target="_blank"&gt;Ruby on Rails&lt;/a&gt; was MIA. I decided to fix Rails first. Here are the steps I took to install Ruby on Rails on Ubuntu, Hardy Heron.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;sudo apt-get install ruby&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At this point I could not install Ruby on Rails with &lt;a href="http://rubygems.org/" target="_blank"&gt;RubyGems&lt;/a&gt; because of the following exception:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;/usr/bin/gem:23: uninitialized constant Gem::GemRunner(NameError)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I opened up the source file and edited the offending line.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;sudo gedit /usr/bin/gem&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Adding &lt;strong&gt;require &amp;#8216;rubygems/gem_runner&amp;#8217;&lt;/strong&gt; to the source file,&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;require 'rubygems'
require 'rubygems/gem_runner'&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I was then able to update RubyGems&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;sudo gem install rubygems-update&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;and install Ruby on Rails and the Mongrel web server.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;sudo gem install rails mongrel&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At this point I had a working version of Rails on my installation of Ubuntu, Hardy Heron. To get back to work the only thing missing was the NetBeans Ruby IDE. If you haven&amp;#8217;t tried NetBeans for your Ruby on Rails projects, &lt;a href="http://crunchlife.com/articles/2007/10/25/the-netbeans-ruby-ide" target="_blank"&gt;I highly recommend it&lt;/a&gt;. It&amp;#8217;s located in the repos and only takes a few plugins to have Rails support out of the box.&lt;/p&gt;</description>
      <pubDate>Sat, 26 Apr 2008 14:20:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:c4860403-cbd3-439c-9e29-9681d677b943</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/04/26/ruby-on-rails-with-hardy-heron</link>
      <category>Ubuntu</category>
      <category>Rails</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/56</trackback:ping>
    </item>
    <item>
      <title>Festo Air Jelly</title>
      <description>&lt;p&gt;&lt;a href="http://www.festo.com/cms/de_de/5890.htm" target="_blank"&gt;Amazing&lt;/a&gt;. I&amp;#8217;d love to see more natural design in programming languages.&lt;/p&gt;

&lt;p&gt;&lt;object width="425" height="355"&gt;&lt;param name="movie" value="http://www.youtube.com/v/F_citFkSNtk&amp;amp;hl=en"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/F_citFkSNtk&amp;amp;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 24 Apr 2008 13:08:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:d7e7948a-02bf-4759-a012-877059bb949e</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/04/24/festo-air-jelly</link>
      <category>Inspiration</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/55</trackback:ping>
    </item>
    <item>
      <title>System.Drawing.Color Hex Values</title>
      <description>&lt;p&gt;Do you know the &lt;a href="http://en.wikipedia.org/wiki/Hexadecimal" target="_blank"&gt;hexidecimal&lt;/a&gt; value of PapayaWhip? How about BlanchedAlmond? Me either. I&amp;#8217;m not a big fan of the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.drawing.color.aspx" target="_blank"&gt;System.Drawing.Color&lt;/a&gt; structure in the Microsoft .NET Framework. I don&amp;#8217;t think the addition of color names makes good framework design sense. Unless you&amp;#8217;re an Interior Designer; the majority of &lt;a href="http://msdn2.microsoft.com/en-us/library/system.drawing.color_properties.aspx" target="_blank"&gt;these&lt;/a&gt; colors won&amp;#8217;t make sense to you either.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ve recently spent time reworking a few design elements of an ASP.NET website. All design related ASP.NET attributes were replaced with &lt;a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets" target="_blank"&gt;Cascading Style Sheets (CSS)&lt;/a&gt; where applicable. Unfortunately the website was spattered with named colors from the System.Drawing.Color structure. To help replace these named colors with hexadecimal values, I found the following resource extremely helpful.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.opinionatedgeek.com/DotNet/Tools/Colors/default.aspx" target="_blank"&gt;http://www.opinionatedgeek.com/DotNet/Tools/Colors/default.aspx&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 16 Apr 2008 16:29:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:639ae4c8-8229-4ae4-83da-ae27bffdeb7d</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/04/16/system-drawing-color-hex-values</link>
      <category>Expect the Unexpected</category>
      <category>ASPNET</category>
      <category>dotNET</category>
      <trackback:ping>http://crunchlife.com/articles/trackback/54</trackback:ping>
    </item>
  </channel>
</rss>
