<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
 
 <title>One of These Days</title>
 
 <link href="http://oneofthesedaysblog.com/" />
 <updated>2011-08-18T20:09:56+12:00</updated>
 <id>http://oneofthesedaysblog.com/</id>
 <author>
   <name>Sam Dalton</name>
   <email>mail@samdalton.co.nz</email>
 </author>

 
 <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/feedburner/ootdb" /><feedburner:info uri="feedburner/ootdb" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
   <title>Facebook Users Need More Moustaches</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/pkj_7eoD8I0/Facebook-users-need-more-moustaches" />
   <updated>2011-08-18T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/Facebook-users-need-more-moustaches</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Drag the link below to your bookmarks bar, visit a friend&amp;rsquo;s &lt;a href="https://www.facebook.com/RickAstley"&gt;Facebook profile&lt;/a&gt;, click the bookmark, and see how manly they look with a moustache.&lt;/p&gt;

&lt;p&gt;&lt;a href='javascript:(function(){var pic = document.getElementById("profile_pic");var src = pic.src;pic.src = "http://mustachify.me/?src=" + src;}());' title='Mustachify.fb' alt='Mustachify.fb' name='Mustachify.rb'&gt;Mustachify.fb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Many thanks to &lt;a href="http://www.aidanfeldman.com/"&gt;Aidan&lt;/a&gt; for creating &lt;a href="http://mustachify.me/"&gt;Mustachify.me&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;&lt;img src="/images/fbstach.png" alt="Awesomestach" /&gt;&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/pkj_7eoD8I0" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/Facebook-users-need-more-moustaches</feedburner:origLink></entry>
 
 <entry>
   <title>Instance Evil?</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/YzmmoKzI5CU/Instance-Evil%3F" />
   <updated>2011-06-15T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/Instance-Evil?</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Some playing with dynamic methods in a Ruby class led me into an interesting problem. &amp;lsquo;instance_eval&amp;rsquo; was being used to allow for dynamic getting and setting of various attributes. This worked great, until the objects needed to be cached in Rails.
An error was being thrown, &amp;ldquo;TypeError: singleton can&amp;rsquo;t be dumped&amp;rdquo;, which didn&amp;rsquo;t make immediate sense. Some digging and playing however led to the answer that Marshal cannot serialize objects that have had instance_eval used on them, which is probably
fair enough as Marshall would have no way of knowing how to redo the eval when the object is loaded back again.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We can add an instance_eval block to add in an instance method, to demonstrate.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instance_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;unleash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;what&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;what&lt;/span&gt; 
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unleash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;the hounds&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# the hounds&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;However, we are not able to serialize the object ):&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;Marshal&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#&amp;gt; TypeError: singleton can&amp;#39;t be dumped&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The solution was to use the magical method_missing; we get the same result, but we&amp;rsquo;re also able to serialize the object.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;method_missing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;)&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;  
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unleash&lt;/span&gt; &lt;span class="c1"&gt;# unleash([])&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unleash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;the hounds&amp;#39;&lt;/span&gt; &lt;span class="c1"&gt;# unleash=([&amp;quot;the hounds&amp;quot;])&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;Marshal&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# o:A&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Happily marshaled (:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;em&gt;(Reverse smilies inspired from &lt;a href="http://blog.frankchimero.com/post/6358013062/ten-more-things"&gt;Frank Chimero&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/YzmmoKzI5CU" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/Instance-Evil%3F</feedburner:origLink></entry>
 
 <entry>
   <title>Phonegap With Jquery Mobile And Backbone Js</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/EYqXe9yRdnM/PhoneGap-with-jQuery-Mobile-and-Backbone-Js" />
   <updated>2011-04-18T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/PhoneGap-with-jQuery-Mobile-and-Backbone-Js</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;There&amp;rsquo;s a lot of hype about mobile web apps being the future of mobile application development, but with my time spent developing for iOS, and seeing how enjoyable/powerful it was, I wasn&amp;rsquo;t convinced. So I took the plunge into PhoneGap, using jQuery Mobile and Backbone.js as my weapons of choice.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The app will be a fairly simple one, showing off the basic naviagtion features of jQuermy mobile, and some of the audio capabilities of PhoneGap. The content of the app will be driven by a single JSON file that we can easily update.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The post will go through each file, and the complete phonegap project can be found &lt;a href="https://github.com/samdalton/mobile-content"&gt;here on github&lt;/a&gt;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-script.js'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-script.js"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;script.js&lt;/h2&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here&amp;rsquo;s our usual backbone namespace, with models, views and collections. We&amp;rsquo;ve got pages inside views to contain the different page types we will encounter.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;backbone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;models&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="n"&gt;views&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;pages&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;base is a global reference to our main DOM object. This was required to make jQuery mobile work a bit nicer, and we&amp;rsquo;ll get to it soon.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;em&gt;Note: I would normally extend the backbone namespace with the name of the app I&amp;rsquo;m buidling, to ensure that multiple backbone apps in a page won&amp;rsquo;t interfere with each other.&lt;/em&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    
    &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#base&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Launch the app, setting our view, and passing in page_data which contains our parsed JSON file.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;mainApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="n"&gt;backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pages&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;page_data&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    
&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-views/Application.js'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-views/Application.js"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;views/Application.js&lt;/h2&gt;

&lt;p&gt;This is the first view that kicks off everything. If you&amp;rsquo;re coming from the iPhone world, consider this to be the (Home|Root)ViewController&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Application&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        
        &lt;span class="kp"&gt;initialize&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Remember the pages variable we passed in during instantiation? We&amp;rsquo;re grabbing it here, and using one of underscore.js' enumerator methods to iterate over each page object. _.map is going to return an array with values equal to what we return in the closure, for each value in args.pages.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;            &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We&amp;rsquo;re creating a new Page view, and passing in the page data that _.map has given us.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="n"&gt;backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now we have a backbone view for each page to be navigated, so we can call render to kick things off.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;            &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        
        &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Just as iPhone apps have screen titles when you traverse content hierarchies, we want to be able to give each page a unique title. We need these titles to display on the main screen, so we can show the user what pages are available for them to navigate to. That&amp;rsquo;s why we&amp;rsquo;re collecting them here.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;            &lt;span class="n"&gt;page_titles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;_.each functions much the same as _.map, except it doesn&amp;rsquo;t return anything.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;            &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Remember that each page is a backbone view, so we&amp;rsquo;re calling render to get the HTML content that makes up the view, and appending it to our view element. As we set our el to be equal to base, we&amp;rsquo;re appending everything to the $(&amp;ldquo;#base&amp;rdquo;) element.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;                &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Grab the title and put it in the array, along with a unique id. The id is required for jQuery Mobile, which we will get to shortly.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;               &lt;span class="n"&gt;page_titles&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;page_id&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Using &lt;a href="http://icanhazjs.com/"&gt;icanhazjs&lt;/a&gt;, we render the home template, passing in the array of page titles&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;            &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ich&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;page_titles&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;page_titles&lt;/span&gt;&lt;span class="p"&gt;}));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/EYqXe9yRdnM" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/PhoneGap-with-jQuery-Mobile-and-Backbone-Js</feedburner:origLink></entry>
 
 <entry>
   <title>New Blog</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/lAfLHByJkqE/New-Blog" />
   <updated>2011-04-11T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/New-Blog</id>
   <content type="html">&lt;tr id='section-One_of_These_Days,_this_blog_was_going_to_change'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-One_of_These_Days,_this_blog_was_going_to_change"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Hello, world, again.&lt;/h3&gt;

&lt;p&gt;So, I&amp;rsquo;ve now completely switched blogging platforms, migrating off of Wordpress to a custom &lt;a href="https://github.com/mojombo/jekyll"&gt;Jekyll&lt;/a&gt; setup.
While wordpress was a seriously powerful platform, I was only using ~2% of its functionality, and that 2% wasn&amp;rsquo;t doing quite
what I wanted. Now, with Jekyll, running on Sinatra on Heroku, I can hack things to work as needed. For example, posts
can be pre-processed with &lt;a href="http://jashkenas.github.com/docco/"&gt;Docco&lt;/a&gt; which creates the amazing styling on this page.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;and the amazing styling of source code, like this &amp;ndash;&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;explain&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I figured it was worth the switch just for this alone, as most of the time I&amp;rsquo;m talking about code.
I&amp;rsquo;m also hoping that the easiser method of posting (git push heroku master) will encourage me to write more frequently, so stay tuned!&lt;/p&gt;

&lt;p&gt;(I will be gradually migrating posts from the old blog over to here, which hopefully won&amp;rsquo;t take too long)&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/lAfLHByJkqE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/New-Blog</feedburner:origLink></entry>
 
 <entry>
   <title>Block Vs For In Objective C Enumeration</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/jCfsf89RBuY/block-vs-for-in-objective-c-enumeration" />
   <updated>2011-04-01T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/block-vs-for-in-objective-c-enumeration</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Having now become used to, and in entirely in love with Ruby, I often find myself wishing other languages had blocks. It&amp;rsquo;s such an neat construct that leads to simplistic, elegant, and often more efficient code. During some work on an iPhone app, I had a need to iterate over the objects of a dictionary, getting both key and object for each pair. This is often done with a simple for loop, however the syntax was ugly and, in my opinion, wholly unnecessary:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="no"&gt;NSArray&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;allKeys&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;objCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="n"&gt;objectAtIndex&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;objectForKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="n"&gt;objectAtIndex&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;5 lines just to get keys and objects from a dictionary? Crazy! Pining for another way, I delved into the documentation and pulled out this gem of a method &amp;ndash; &amp;ldquo;enumerateKeysAndObjectsUsingBlock&amp;rdquo;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This does exactly what it sounds, and, oh what joy, it accepts a block.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;enumerateKeysAndObjectsUsingBlock&lt;/span&gt;&lt;span class="ss"&gt;:^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BOOL&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The block is declared with a slightly odd, but manageable syntax: ^ (params) { code }
In this case, the params are the key, object, and a boolean called stop. According to the documentation, if we set stop to true, the enumeration will stop, effectively acting as a &amp;lsquo;break&amp;rsquo; function.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now that we&amp;rsquo;ve got our code looking pretty, how does it match up in performance? Curious, I ran a simple experiment.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;objCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="no"&gt;NSMutableDictionary&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt;&lt;span class="no"&gt;NSMutableDictionary&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;objCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;setObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;test&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;forKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;NSNumber&lt;/span&gt; &lt;span class="n"&gt;numberWithInt&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;iterations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="no"&gt;NSDate&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;methodStart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;NSDate&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;NSArray&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;allKeys&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;objCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="n"&gt;objectAtIndex&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;objectForKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="n"&gt;objectAtIndex&lt;/span&gt;&lt;span class="ss"&gt;:i&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="no"&gt;NSDate&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;methodFinish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;NSDate&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;NSTimeInterval&lt;/span&gt; &lt;span class="n"&gt;executionTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;methodFinish&lt;/span&gt; &lt;span class="n"&gt;timeIntervalSinceDate&lt;/span&gt;&lt;span class="ss"&gt;:methodStart&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;forTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;executionTime&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Standard For Loop : %f&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;forTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;




  &lt;span class="n"&gt;methodStart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;NSDate&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;enumerateKeysAndObjectsUsingBlock&lt;/span&gt;&lt;span class="ss"&gt;:^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BOOL&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;		
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;methodFinish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;NSDate&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;executionTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;methodFinish&lt;/span&gt; &lt;span class="n"&gt;timeIntervalSinceDate&lt;/span&gt;&lt;span class="ss"&gt;:methodStart&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;enumerableTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;executionTime&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Standard Enumerable Loop : %f&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enumerableTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="no"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Times Faster : %f&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;forTime&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;enumerableTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here we&amp;rsquo;re simply creating a dictionary with 100,000 key/value pairs, and iterating over it grabbing both key and value, for the two different methods. To attempt to eliminate any time fluctuations due to unpredictable things like caching or context switching, we&amp;rsquo;re running each loop 100 times. (Note: I tried to run it 1000+ times, but it crashed probably as there are memory leaks galore in the code. Having said that, the memory leaks probably affect the run time as well, but I&amp;rsquo;ll leave that as an exercise for the reader to figure out.)&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;And the results? Surprising.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;2011-04-01 18:42:49.163 Standard For Loop : 0.025023&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;2011-04-01 18:42:49.616 Standard Enumerable Loop : 0.004506&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;2011-04-01 18:42:49.616 Times Faster : 5.553028&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Using the block is more than 5 (FIVE!) times faster than the ugly, verbose for loop version. I know what I&amp;rsquo;ll be using from now on.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/jCfsf89RBuY" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/block-vs-for-in-objective-c-enumeration</feedburner:origLink></entry>
 
 <entry>
   <title>Backbone Js Html5</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/Xa3gNuEXRxE/backbone-js-html5" />
   <updated>2011-02-12T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/backbone-js-html5</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;As &lt;a href="http://www.oneofthesedaysblog.com/backbone-js/" title="Backbone.js | One Of These Days"&gt;stated previously&lt;/a&gt;, &lt;a href="http://documentcloud.github.com/backbone/" title="Backbone.js"&gt;Backbone.js&lt;/a&gt; is buckets of fun to work with. In the spirit of this praise, I thought I&amp;rsquo;d have a play with it, and see how it could work with HTML5 animation development.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The end result is a cannon that shoots a small cannon ball, &lt;a href="http://www.oneofthesedaysblog.com/serve/backbone-canvas/index.html" title="Backbone.js"&gt;viewable here&lt;/a&gt;. While the example is entirely contrived, it demonstrates a number of aspects of Backbone.js, and well, is kind of fun. This first post will look at the Backbone.js portion of the app, and a second post will follow to briefly go over the canvas animation.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2011/02/Screen-shot-2011-02-12-at-4.20.35-PM.png" alt="Keeping the backbone views and models in separate files and folders is a good way of helping you navigate your app. For larger scale projects, this is essential." /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The first file we need is our main html page, which will include all of the necessary javascript files, a bit of styling, and the 2 HTML elements we need for our cannon. The cannon image is what we click to fire the cannon, and the firingRange canvas is where the cannon ball will be animated.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Next, we&amp;rsquo;ll look at the main.js file, as this is what kicks off the app.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                
    &lt;span class="n"&gt;myCannon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;CannonView&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s it. This will be executed when the DOM is ready, and will ensure our cannon is fully functioning. This file is a clear example of how backbone can help organise and DRY up your code. There&amp;rsquo;s no doubt as to where the execution flow of the app starts, and from here, it is easy to follow through. This makes testing easier, debugging easier, and allows other people to get up to speed with the code in a far shorter period of time (including yourself!).&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CannonView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;

    &lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a.do&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Cannon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;            
        &lt;span class="s1"&gt;&amp;#39;click&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;lightFuse&amp;#39;&lt;/span&gt;            
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="kp"&gt;initialize&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bindAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;fire&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ball&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;BallView&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;loadingView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;LoadingView&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;beforeSync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loadingView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;afterSync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loadingView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="n"&gt;lightFuse&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fire&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="n"&gt;fire&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ball&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;One of three, the cannon view takes care of our event handling as well as setting up the other 2 views. It&amp;rsquo;s given the anchor tag as its element, allowing us to easily bind a click event. The model it&amp;rsquo;s given is Cannon, a simple model that will store the amount of ammo available, and persist this to a server.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;When the cannon is clicked, lightFuse is called, which subsequently calls fire on the Cannon model. We&amp;rsquo;re passing in the fire function, which the model will treat as a callback and call it once firing has completed at the model level. This call will cause animate to be called on the ball, resulting in fancy animation. Note that in the initialize method we are using the underscore.js bindAll method to ensure that when the callback is passed into the model, it is &lt;a href="http://stackoverflow.com/questions/183214/javascript-callback-scope" title="events - JavaScript Callback Scope - Stack Overflow"&gt;still scoped to CannonView&lt;/a&gt; and not Cannon.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The last area of interest in this view is the 3 lines regarding the loadingView. The loading view is essentially our fuse animation. It seems appropriate to illustrate the loading of the cannon ball with such a depiction, and so we need a way of showing this when we click the cannon, and hiding it after it&amp;rsquo;s fired. While we could use the success callback in the Backbone.sync object, doing so would require us to put code related to the loading down in the cannon model. In keeping with a strict-as-possible separation of concerns view, it would be more appropriate to have this logic higher up, in the view class itself. As the fire method on the model is asynchronous, we cannot simply show the fuse before calling fire, and hide it afterwards. To achieve the functionality we want, I thought it would be an interesting exercise to edit the Backbone.sync object itself, in the backbone source. While this may not be the most appropriate way of doing this, it should at least be educational.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Two lines are added to the top of backbone.js to allow us to set before and after callbacks.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;beforeSync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){};&lt;/span&gt;
&lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;afterSync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){};&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The last edit needs to go in the Backbone.sync object.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;            
        &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;here is where our after callbacks are run&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;          &lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;afterSync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;afterSync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
          
        &lt;span class="p"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;run our before callback&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;        &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;beforeSync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;         &lt;span class="vg"&gt;$.&lt;/span&gt;&lt;span class="n"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We&amp;rsquo;re modifying the success and error callbacks to also execute the afterSync callback. It&amp;rsquo;s included in both success and error, as even if there is an error we want to stop the fuse from showing. Before we execute the ajax request, we call beforeSync. This will occur synchronously, and so $.ajax will be called afterwards. On completion of the request, afterSync will be called. As we have hooked loadingView.render to before, and loadingView.remove to after, we will now see the fuse light on click, and stop after firing. &lt;em&gt;Note: if this way of doing things has made anyone cringe or question, I&amp;rsquo;d love to hear from you in the comments or email about an alternative way of achieving this. My justification is that the edits to the source are in no way connected semantically to anything in the app, and the functionality could fit a wide number of use cases.&lt;/em&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;While we&amp;rsquo;re on the topic of loading, let&amp;rsquo;s take a quick look at the loading view. It&amp;rsquo;s nothing fancy, just a view that on rendering, puts a span element containing and image into the DOM. We&amp;rsquo;ve bound render and remove to itself so that when inside the Backbone.sync callback, they still have  the correct scope.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LoadingView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;

   &lt;span class="n"&gt;tagName&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;span&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

   &lt;span class="n"&gt;className&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;loading&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

   &lt;span class="kp"&gt;initialize&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bindAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;render&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;remove&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;

   &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;body&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&amp;lt;img src=&amp;#39;images/fuse.gif&amp;#39;/&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The final view for this app is the ball view. This is where our HTML5 canvas magic happens, and so for brevity, this will be covered in a separate post. The view has an animate method however, which when called by the cannon view, will show the cannon ball flying out of the cannon. This is a great example of how backbone can hide away implementation details. The main app knows nothing of the canvas animation, and we can easily swap this out for whatever we want. All the cannon view cares about is that there is an animate method it can call, and the rest will follow.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Finally, we come to the cannon model itself. In order to demonstrate the loading functionality, it was necessary to create some form of delay between calling fire on the model, and having the model state update. A simple php script was used that would randomly sleep for between 0 and 2 seconds.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;and now our model:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Cannon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-19'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-19"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;our delay script&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;   &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;http://oneofthesedaysblog.com/server/backbone-canvas/sleep.php&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
   &lt;span class="n"&gt;defaults&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-20'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-20"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;a new cannon has 100 cannon balls&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;ammo&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; 
   &lt;span class="p"&gt;},&lt;/span&gt;

   &lt;span class="n"&gt;fire&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-21'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-21"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;only fire if we have positive ammo&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;       &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;ammo&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="n"&gt;ammo&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;ammo&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-22'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-22"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We&amp;rsquo;re giving the cannon model a single attribute called ammo, which will keep track of how many times the cannon has been fired. This is being set an arbitrary value of 100, such that after 100 firings, it won&amp;rsquo;t fire again until the page is reloaded. The fire method is also here, and we can see that it takes in the callback we pass in from the view. If there is positive ammo, the model will try and save this to the server, with a value of ammo decreased by one. In the case of our simple script, we&amp;rsquo;re not really concerned whether or not the sync is successful, so I&amp;rsquo;ve set the callback to run on either success or error.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-23'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-23"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If we look back at the Backbone.sync object, we can see that this callback is being executed in the line we modified:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;afterSync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-24'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-24"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;It&amp;rsquo;s running the callback we pass in to save(), as well as our afterSync callback.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-25'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-25"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That concludes the overview of the cannon app, with the exception of the ball view, which will be covered in another post. Hopefully this has been useful in, once again, highlighting the advantages of using simple MVC framework such as backbone. Our presentation and business logic is clearly separated, and the components are decoupled sufficiently such that we can easily make changes without breaking the rest of the app. The structure of the app is also useful for navigating the various areas of functionality, resulting in &lt;a href="http://www.osnews.com/story/19266/WTFs_m" title="WTFs/m"&gt;fewer WTFs per minute&lt;/a&gt;.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/Xa3gNuEXRxE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/backbone-js-html5</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 14 Tty L</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/zufyrJG0iag/ruby-day-14-tty-l" />
   <updated>2011-01-22T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-14-tty-l</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="http://upload.wikimedia.org/wikipedia/commons/8/89/WACsOperateTeletype.jpg" alt="Teletypwriter" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I left the 30 day to Ruby series with a rather abrupt and unfinished ending. This post is the next in the hopeful continuation and completion of the series.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Without further ado then, Input/Output (IO)! For any operations involving sending or receiving data (be it to a file, or another computer), you&amp;rsquo;re going to need some way of telling the operating system what to do. Any modern OS has a set of system calls that are used to interact with the computer&amp;rsquo;s hardware, while ensuring nothing untoward happens to it. For writing to a file, we might call fwrite in the C standard library. By passing in a file name and some data, the library will instruct the OS to put the given data in the given file. We can also instruct the OS to send and receive data to things other than files however, a process, for example.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Let&amp;rsquo;s look at an example to make the explanations easier.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;date&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="c1"&gt;# Sat Jan 22 22:01:35 NZDT 2011&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We can see that the output of the above line was today&amp;rsquo;s date and time, exactly as if we were to open the terminal and type in the UNIX &amp;lsquo;date&amp;rsquo; command. Note that we are calling the read command on the object we&amp;rsquo;ve created, exactly as if we were reading from a file. What&amp;rsquo;s going on here? popen, or process open, is a function in the Ruby IO library that instructs the OS to fire up a process with the given command (date, in this case). A process is similar to a file in that it can receive inputs and produce outputs, and so Ruby ensures theses two ends are available for use, just as if a file had been opened. The advantage of the IO library is that the API is fixed for whatever IO object you&amp;rsquo;re actually using. Be it a process, file, socket, pipe et al, the commands required for sending/receiving data are always the same. It can be seen then that after popen runs the date command, we are simply reading the output of this command, retrieving today&amp;rsquo;s date.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Let&amp;rsquo;s take another example, but to do so we must install a rather neat program called &lt;a href="https://github.com/drewcrawford/Phone-Pipe"&gt;phonepipe&lt;/a&gt;. You could run a similar example without this, a notifo account and a smart phone with push capabilities, but it&amp;rsquo;s rather satisfying to do it this way. Keep reading once you&amp;rsquo;ve got it set up and working.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;What we want to do is have Ruby send some text to our phone. A simple solution would be to simply have Ruby call &amp;lsquo;echo hello | phone&amp;rsquo;, which can be achieved using the shorthand %x syntax:
This simply has Ruby drop out the commands contained into a shell, causing the word hello to be sent to your phone. This doesn&amp;rsquo;t fit nicely with our IO examples however so let&amp;rsquo;s try something else.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="sx"&gt;%x{ echo hello | phone}&lt;/span&gt; 

&lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;phone&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Hello from process &amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;Process&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_s&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Just as we opened up a process for date, we&amp;rsquo;re opening up a process for the phone command. The difference is that we&amp;rsquo;re specifying that we want to be able to write to it, just as we would write to a file. By passing it a block, we can do things with the returned process handle. Inside the block, we&amp;rsquo;re simply using puts to send our message to the phone process. You should see a message along the lines of &amp;ldquo;Hello from process &amp;mdash;&amp;mdash;&amp;rdquo; appear on your phone, where &amp;mdash;&amp;mdash; is any number. While this is purely a contrived example, you can see just how easy it is to read and write to processes in Ruby. To show the flexibility of these, take a look at this example which opens up an interactive Ruby prompt inside the process and has it execute Ruby code:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;irb&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
   &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;puts &amp;#39;hello&amp;#39;&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We&amp;rsquo;re opening an irb process, and feeding it &amp;ldquo;puts &amp;lsquo;hello&amp;rsquo;&amp;rdquo; which gets run by irb and executed. &lt;br /&gt;&lt;em&gt;Note: if someone can turn this into a &lt;a href="http://en.wikipedia.org/wiki/Quine_(computing)" title="Quine (computing) - Wikipedia, the free encyclopedia"&gt;quine&lt;/a&gt;, it would be awesome to see in the comments! Here&amp;rsquo;s a start:&lt;/em&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;irb&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
   &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot; IO.popen(&amp;#39;irb&amp;#39;, &amp;#39;w&amp;#39;) do |f|&lt;/span&gt;
&lt;span class="s2"&gt;       f.puts &lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;puts &amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;&lt;/span&gt;
&lt;span class="s2"&gt;    end&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Hopefully this brief foray into the world of Ruby::IO has piqued your interested, but before I TTYL we must look at the TTY. In times past, there was the Teletypewriter (TTY): A magical device that would take digital input and type output onto physical paper via a typewriter. This name, tty, has stuck around in UNIX, such that any terminal window you open is named something along the lines of ttyn where n is the index nth terminal window you&amp;rsquo;ve opened (the first window would be tty0, and so on). While nothing to do with typewriters, the idea of printing text to a screen is somewhat similar to this, so if you don&amp;rsquo;t think about it too much it makes sense. On a typical OSX or Linux system, the tty interfaces appear as simple files on the filesystem, allowing us to read and write to them. I happen to be running OSX, and I know that the first tty lives at /dev/ttys001, so if I open this up and write to it, the data will be displayed on the first terminal window I have open.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/dev/ttys001&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Hello from Ruby!&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We&amp;rsquo;re using the File class in this case, not IO, to open up the tty file, but everything functions identically. If you have a terminal window up, you can check it&amp;rsquo;s tty file by viewing the terminal window&amp;rsquo;s properties. If it is indeed ttys001, you should see &amp;lsquo;Hello from Ruby!&amp;rsquo; appear on it. If it&amp;rsquo;s something else, replace the file name and similar results should occur.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This is all well and good, but what can you do with it? Well, here&amp;rsquo;s a feeble attempt and what could be the beginnings of a game of pong:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;
&lt;span class="n"&gt;fps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;

&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/dev/ttys001&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
   &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
   &lt;span class="n"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_f&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;fps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[2J&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[f&amp;quot;&lt;/span&gt; &lt;span class="c1"&gt;# clear screen character&lt;/span&gt;
      &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt;  &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot; &amp;quot;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;o&amp;quot;&lt;/span&gt;
      
      &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;
          &lt;span class="n"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
      &lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 
          &lt;span class="n"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s all for now, but give it a whirl, and be sure to post any cool uses/examples you have in the comments!&lt;/pre&gt;&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/zufyrJG0iag" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-14-tty-l</feedburner:origLink></entry>
 
 <entry>
   <title>2010 And Beyond</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/GVd8Ld_bR1U/2010-and-beyond" />
   <updated>2010-12-30T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/2010-and-beyond</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The year being just about over, it&amp;rsquo;s time to write one of those summary posts. I&amp;rsquo;ll keep it brief!&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;From starting my first company, to finishing a degree in Software Engineering, to starting full time freelancing, the year has been an almost overwhelming learning curve. I wouldn&amp;rsquo;t, however, change a second of it. Thanks especially to the people I&amp;rsquo;ve met through study, work, meetups, twitter, and everywhere else; you&amp;rsquo;ve all been amazing, and helped make the year an absolute blast.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Slightly drained now, I&amp;rsquo;m looking forward to a bit of recuperation, and then will be right back into it next year, 2011.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;To those of you who&amp;rsquo;ve visited me here I sincerely thank you, and I hope you&amp;rsquo;ll stick around for a hopefully more frequent and interesting year of posting to come!&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Sam&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/GVd8Ld_bR1U" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/2010-and-beyond</feedburner:origLink></entry>
 
 <entry>
   <title>A Simple Jquery Dropdown Menu</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/sYOm0wbSLJE/a-simple-jquery-dropdown-menu" />
   <updated>2010-12-20T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/a-simple-jquery-dropdown-menu</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A while ago I wrote about a &lt;a href="http://www.oneofthesedaysblog.com/pure-css-dropdown-for-firefox/" title="Pure CSS Dropdown for Firefox | One Of These Days"&gt;bare bones CSS dropdown&lt;/a&gt;. Given the widespread adoption of jQuery since then, there&amp;rsquo;s little reason to go for the CSS only approach (although it is perhaps more elegant). &lt;a href="http://twitter.com/#!/tedawebguy" title=""&gt;@tedawebguy&lt;/a&gt; suggested I write a post about a more modern version, so let&amp;rsquo;s go!&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;(Note: This should work in all browsers, although I&amp;rsquo;ve only tested it in Firefox and Chrome on OSX. Any problems with it will be simple styling issues.)&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here&amp;rsquo;s the HTML we require:&lt;/p&gt;

&lt;ul id="nav"&gt;
&lt;li&gt;
Home
&lt;ul&gt;
&lt;li&gt;Link 1&lt;/li&gt;
&lt;li&gt;Link 2&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;About&lt;/li&gt;
&lt;li&gt;
Blog
&lt;ul&gt;
&lt;li&gt;Blog 1&lt;/li&gt;
&lt;li&gt;Blog 2&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Contact&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;This is a simple nested list with 4 top level links : Home, About, Blog and Contact. Home and Blog have 2 sublists that we want hidden normally, but visible when their parent list element is hovered over. We can have an arbitrary number of links, but the current (read: lazy) version only allows nesting 1 level deep.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The styling we will need is fairly straight forward:
The first style removes the bullets from our list, gives it a reasonable height and padding to ensure that the hover area is user-friendly enough.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="ss"&gt;:none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Next we&amp;rsquo;re styling the list elements, making them display inline and giving them a bit of padding and spacing. The border and cursor is not essential, but it makes it easier to see where to hover. Lastly, we&amp;rsquo;re giving it a relative position so that the child lists will be position relative to where this element is i.e. directly below the element you are hovering over.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="ss"&gt;:inline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt; &lt;span class="c1"&gt;#555555;&lt;/span&gt;
        &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="ss"&gt;:pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="ss"&gt;:relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;To finish our CSS, we&amp;rsquo;re styling the second level list. It&amp;rsquo;s hidden to start with, and given an absolute position just below the top level list. The negative left position may cause some alignment problems in IE, but it&amp;rsquo;s there to offset the indent that browsers put on list elements.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="ss"&gt;:none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="ss"&gt;:absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="ss"&gt;:-&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;600&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now that we&amp;rsquo;ve structured and styled our navigation menu, we can sprinkle some javascript over it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;        &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#nav li&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;ul&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fadeIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt; 
                &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;ul&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fadeOut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We&amp;rsquo;re including the Google hosted jQuery library as per best practices, and then kicking off with the usual document.ready shortcut that jQuery gives us. We&amp;rsquo;re then setting up a hover event listener on each list element in the top level list. This takes 2 callbacks, one for mouse over and one for mouse out. On mouse over, we&amp;rsquo;re finding any list that may reside inside of the current element and subsequently showing it. The reverse happens on mouse out. Fading was used to make it a bit easier on the eyes, while also allowing the mouse pointer to leave the list area for a few milliseconds. This increases the usability as the user does not have to be quite as accurate when moving the mouse from top-level to sub-list elements.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Plugging these 3 components together gives us the following 2 states:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2010/12/Screen-shot-2010-12-20-at-5.27.58-PM.png" alt="Sample 1" /&gt;
&lt;img src="/uploads/2010/12/Screen-shot-2010-12-20-at-5.29.59-PM.png" alt="Sample 2" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If you want a more flexible solution, there&amp;rsquo;s a &lt;a href="http://www.1stwebdesigner.com/freebies/38-jquery-and-css-drop-down-multi-level-menu-solutions/" title="38 jQuery And CSS Drop Down Multi Level Menu Solutions"&gt;silly amount of libraries&lt;/a&gt; to be found on the web. Hopefully however, this post shows just how simple they are to construct, and may even be useful if you&amp;rsquo;re looking for a lightweight solution.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/sYOm0wbSLJE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/a-simple-jquery-dropdown-menu</feedburner:origLink></entry>
 
 <entry>
   <title>Backbone Js</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/ziMFyoIhl1M/backbone-js" />
   <updated>2010-11-20T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/backbone-js</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2010/11/Screen-shot-2010-11-20-at-7.13.37-PM.png" title="" alt="Backbone.js" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Backbone.js is a simple, very lightweight MVC framework built on jQuery and &lt;a href="http://documentcloud.github.com/underscore/" title="Underscore.js"&gt;Underscore.js&lt;/a&gt;, a &amp;ldquo;utility belt for Javascript&amp;rdquo;. As with any MVC framework, the core premise of Backbone is to separate an application&amp;rsquo;s representation and storage of data, from the presentation of it. 2 main classes provide this functionality, Backbone.Model and Backbone.View. The neat thing is that when you associate a view with a model, any changes to the model will be reflected in the view without you having to write any linking code.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This sounds all well and good, but is it really that useful? Yes. Keen to get my teeth into it, I rewrote &lt;a href="http://vitalgiftsapp.com" title="Vital Gifts - Good Gifts for Great People"&gt;Vital Gifts&lt;/a&gt; app with it. The process took less time than the initial version, and the end result was a marginally larger Javascript file that executed faster while saving a large proportion of my sanity. The reason for this usefulness stems from the modularity it lends to your application; you can easily create self contained visual elements that have all of the data persistence and manipulation they need, and place them onto the page with one line. To illustrate this modularity, let&amp;rsquo;s create a simple friend selector that could be easily fitted out to use the Facebook Graph API.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This creates an empty model that we can use to store information about the selected friend. Note that we&amp;rsquo;re not specifying any attributes such as name or photo. This is because the Backbone model is just a JSON object, and so no schema is necessary &amp;ndash; we can add and fetch attributes however we see fit.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In a typical MVC setup, the view would contain a template with rudimentary data manipulations. Any events received by the view are sent to the controller for processing, and then results are sent back to the view. Backbone &amp;lsquo;breaks&amp;rsquo; this model slightly by binding changes to the model directly with the view object. The results are still passable however, with the use of a template framework such as &lt;a href="http://mustache.github.com/" title="&amp;#123;&amp;#123; mustache }}"&gt;Mustache&lt;/a&gt;. We can use the Backbone view object for controller style logic, and keep the presentation in a template.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Backbone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;create a new model to store the selected user&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;the text field we&amp;rsquo;re using for the selector&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#friend-selector&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;we want to listen for 2 events on the text field&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;&amp;quot;focus&amp;quot;&lt;/span&gt;     &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;selectInput&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;&amp;quot;keyup&amp;quot;&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;friendSelected&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This first section is all we need to get our view up and running. We tell the view what model it represents, what DOM element it is going to use, and we define a set of event/callback pairs to handle user interaction. To do the actual autocompletion we will use the &lt;a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/" title="bassistance.de &amp;raquo; jQuery plugin: Autocomplete"&gt;jQuery autocomplete library&lt;/a&gt;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="kp"&gt;initialize&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;friends&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;set up autocomplete&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;autocomplete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;friends&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;minChars&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;310&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;matchContains&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;autoFill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;formatItem&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;lt;img src=&amp;#39;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;picture&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;#39; width=&amp;#39;50&amp;#39; height=&amp;#39;50&amp;#39; alt=&amp;#39;friend-picture&amp;#39; /&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;formatMatch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;formatResult&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;bind this object to the form, so we can access it from the result event&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;scope&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;formatted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;scope&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;autocompleteSelected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I won&amp;rsquo;t go into detail about the autocomplete library, as the documentation on it is fairly comprehensive. We are using the initialize method to take in an array of friends that we want to be searchable in the element &amp;ndash; this functions exactly as a typical object constructor. The caveat with this however is the last section, which is unfortunately an ugly solution. The callback for the autocomplete plugin is set with the &amp;lsquo;result&amp;rsquo; method. From within this, we need to be able to call a method on the view object so that the model can be updated. The view won&amp;rsquo;t be in the scope of the autocomplete plugin however, so we are using the data property of the text field to store a reference to the view object. Both the autocomplete and the view model are attached to the same element so the data property acts like a form of shared memory.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;selectInput&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="n"&gt;autocompleteSelected&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;friend&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;friend&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="n"&gt;friendSelected&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;see if they typed in a correct name&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;     &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;selectInput is purely a convenience for the user. It will result in the contents of the text field becoming highlighted when clicked on, making it easy to type in a new friend. It should save the user 1 click on average.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;autocompleteSelected is called when the user selects a friend that has been autocompleted. It calls &amp;lsquo;set&amp;rsquo; on the model and sets the name parameter to be the chosen friend&amp;rsquo;s name.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;friendSelected is called on the keyUp event. It is required in case the user wants to type in a friend that is not registered in the autocomplete. If this were to happen, the autocomplete plugin would never match it, and so the result callback would never be fired. This would leave the user model empty, even though the user has typed in a name. When the event is triggered, the model is set to be the current value of the text field, and &amp;lsquo;search&amp;rsquo; is called on the text field. Search is a method that comes from the autocomplete plugin, and it simply forces the plugin to see if the current value is in its list. If it can&amp;rsquo;t find it, we have already updated the model, keeping it consistent, and if it does find it, autocompleteSelected will be called and the model will still have the friend&amp;rsquo;s name.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The 2 objects above are all that we need to have a self contained friend selector. To render it on the page, we simply create a new UserView object and pass in a list of friends, assumed for this example to exist as a global variable called &amp;lsquo;friends&amp;rsquo;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;UserView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;friends&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s it!&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/ziMFyoIhl1M" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/backbone-js</feedburner:origLink></entry>
 
 <entry>
   <title>Hosting Your Rails App</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/O0_F5_u4aik/hosting-your-rails-app" />
   <updated>2010-08-27T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/hosting-your-rails-app</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So you&amp;rsquo;ve got this sweet rails app made, and it runs fine locally, but you&amp;rsquo;d like other people to be able to use it, right? For that, you can sign up for an account with a web hosting company, but with so many, how do you choose one?&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So far, I&amp;rsquo;ve used 2 different companies to host a rails app, &lt;a href="http://www.rackspace.com/index.php" title="Dedicated Server, Managed Hosting, Web Hosting by Rackspace Hosting"&gt;Rackspace&lt;/a&gt; and &lt;a href="http://heroku.com/" title="Heroku | Ruby Cloud Platform as a Service"&gt;Heroku&lt;/a&gt;. I&amp;rsquo;ve settled with Heroku, and couldn&amp;rsquo;t be happier.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2010/08/Dedicated-Server-Managed-Hosting-Web-Hosting-from-Rackspace.png" alt="Rackspace" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;What they offer is essentially a Virtual Private Server (VPS) solution, along with your run of the mill cloud storage, much like Amazon&amp;rsquo;s ubiquitous S3. Having an entirely blank server that you can install any OS and software on is a real charm. Instead of having too little control, you have too much control. If you&amp;rsquo;re a sysadmin geek or bash guru, you will have a blast, making this a service that probably wouldn&amp;rsquo;t suit someone with limited server experience.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I installed a typical LAMP stack, with Ubuntu 8.x as my distribution of choice. There really was no difference from setting up this server than if I were doing it on a local development machine, with the exception of DNS. Alongside setting up your /etc/hosts file, you will need to use a simple control panel to add in your domain.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This excess of freedom however does create a large amount of setup work to get your server running, especially when it comes to getting rails going. Following &lt;a href="http://hackd.thrivesmarthq.com/how-to-setup-a-linux-server-for-ruby-on-rails-with-github-and-phusion-passenger" title="How-To Setup a Linux Server for Ruby on Rails - with Phusion Passenger and GitHub - Hack'd"&gt;this tutorial&lt;/a&gt; on setting up Passenger and Capistrano, it took around 30 minutes and a couple of headaches.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;One downside with Rackspace is that it is down to the administrator of the account to ensure the server is secure. For an experience sysadmin this is probably a non-issue, but for those competent enough to get a server running but lacking any solid security experience, it can be a tad dangerous.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;To sum up, if you&amp;rsquo;re looking for complete control, extremely reasonable pricing, on demand support, and don&amp;rsquo;t mind getting your hands dirty in the command line, then you will probably love Rackspace.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2010/08/heroku-logo_big.png" alt="Heroku &amp;lt;3" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If you then navigate to the URL it gives you, you will see your rails app. The only caveat to this is that you must be in the root directory of your app, and it must be in a git repository. If you&amp;rsquo;re starting a brand new rails app, the setup would look like this:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;heroku&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="no"&gt;MyAwesomeRailsApp&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;heroku&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This will then have your new rails app running at http://skynet.heroku.com.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="no"&gt;Skynet&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="no"&gt;Skynet&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;commit&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Importing Skynet&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;heroku&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="n"&gt;skynet&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;heroku&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Heroku also comes with a swathe of plugins. From hosted MongoDB installations, to effortless DNS, to Cron jobs and auto backups, it&amp;rsquo;s like a Sith lord having the keys to the Padawan training room, and all the Jedis are away on holiday.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Essentially, Heroku has done to hosting what rails did for web development. Convention over configuration, excellent user experience, and having everything &amp;ldquo;just work&amp;rdquo; with the minimum amount of effort.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Another neat thing with Heroku is that all of the commands and tools it provides are accessible through a command line tool. You can add domain names, add more concurrency to your app, run rake commands, and just about every other feature they provide.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So if you&amp;rsquo;re looking for a no fuss, beautifully designed, effortless workflow, rubyesque, affordable and scalable solution, you might want to check out Heroku. (However, it&amp;rsquo;s built on Rails, which &lt;a href="http://canrailsscale.com/" title="Can Rails Scale?"&gt;can&amp;rsquo;t actually scale&lt;/a&gt;, so you may want to take that into consideration.)&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/O0_F5_u4aik" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/hosting-your-rails-app</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 13 Ruby On Rails</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/To6PPLkoEP0/ruby-day-13-ruby-on-rails" />
   <updated>2010-07-19T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-13-ruby-on-rails</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2010/07/Rails.png" alt="Rails" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Getting_Started'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Getting_Started"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;Getting Started&lt;/h2&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;a href='http://media.rubyonrails.org/video/rails_blog_2.mov'&gt;Creating a weblog in 15 minutes&lt;/a&gt;&lt;br /&gt;
This was the video I started with, and it blew my mind (as it said it would). It really doesn't exaggerate when it says create a blog in 15 minutes. Start with this; you may not understand the intricacies and details, but you will be exposed to the features and power of rails, and after watching you will want to dive right in.
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://guides.rubyonrails.org/getting_started.html" title="Getting Started with Rails"&gt;Getting Started with Rails&lt;/a&gt;&lt;br /&gt;
From the official rubyonrails.org website, this is the official quickstart. The site offers a wealth of information on all aspects of rails, with this particular guide taking you through the blog creation in the video above.
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.alistapart.com/articles/gettingstartedwithrubyonrails" title="A List Apart: Articles: Getting Started with Ruby on Rails"&gt;A List Apart: Getting Started with Ruby on Rails&lt;/a&gt;&lt;br /&gt;
If you're a little more cynical about this whole rails thing, A List Apart nicely outlines the who, where, whys and hows of rails. Comparing it to PHP, this is a good read if you come from that background.
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.ruby-forum.com/" title="Forum List - Ruby Forum"&gt;Ruby Forums&lt;/a&gt;&lt;br /&gt;
When you start running in to strange errors and bugs, try doing a search at the Ruby Forums, or even posting a topic about it. There is an absolute wealth of knowledge, and a wonderful group of people.
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Tools and Plugins&lt;/h2&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://docs.rubygems.org/read/chapter/1#page1" title="RubyGems User Guide |  RubyGems Manuals"&gt;RubyGems User Guide&lt;/a&gt;&lt;br /&gt;
While not part of rails, gems will become an essential part of your work. There is a wealth of functionality that you can add to your app with only a few commands. This guide will explain a little more about this tool.
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://adventuresincoding.com/2010/05/10-textmate-bundlesplugins-to-boost-your-ruby-on-rails-development-productivity/" title="10 TextMate bundles/plugins to boost your Ruby on Rails development productivity | Adventures In Coding"&gt;10 TextMate bundles/plugins to boost your Ruby on Rails development productivity&lt;/a&gt;&lt;br /&gt;
Hopefully by now you're getting the hang of rails. These Textmate plugins will make your time with rails even easier, and more enjoyable. (You &lt;em&gt;are&lt;/em&gt; using &lt;a href="http://macromates.com/" title="TextMate  The Missing Editor for Mac OS X"&gt;Textmate&lt;/a&gt;, aren't you?)
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://net.tutsplus.com/tutorials/ruby/how-to-build-an-unobtrusive-login-system-in-rails/" title="How to Build an Unobtrusive Login System in Rails | Nettuts+"&gt;Getting Started with User Authentication&lt;/a&gt;&lt;br /&gt;
Authlogic is an amazing plugin that lets you implement a fully functional user authentication system into your app with the absolute minimal amount of effort and code. This NETTUTS tutorial gives you a nice overview of how to implement it with an AJAX login form.
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://hackd.thrivesmarthq.com/how-to-setup-a-linux-server-for-ruby-on-rails-with-github-and-phusion-passenger" title="How-To Setup a Linux Server for Ruby on Rails - with Phusion Passenger and GitHub"&gt;How-To Setup a Linux Server for Ruby on Rails&lt;/a&gt;&lt;br /&gt;
This excellent and in-depth guide shows you how to set up a linux server to host your rails apps. It uses Capistrano, Phusion Passenger and GitHub to manage deployment in a way that, after you've done it once, you'll wonder why you ever did it any other way.
&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;/p&gt;

&lt;h2&gt;Podcasts / Screencasts&lt;/h2&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://itunes.apple.com/podcast/railscasts/id218282043" title="Railscasts - Download free podcast episodes by Ryan Bates on iTunes."&gt;Ruby on Rails Screencasts&lt;/a&gt;&lt;br /&gt;
With over 200 screencasts, Railscasts is a fantastic resource for new tools, best practices, tips, and more. 
&lt;/li&gt;
&lt;li&gt;    
&lt;a href="http://peepcode.com/products/rails-from-scratch-part-i" title="Rails 2 from Scratch Part I | PeepCode Screencasts for Web Developers and Alpha Geeks"&gt;Peepcode Rails from Scratch Screencast&lt;/a&gt;&lt;br /&gt;
At just $9, this is fantastic value. The screencast shows you how to create a complete rails app, touching all of the important areas. Even though there is a lot of free information and tutorials available, the money spent on this is well worth it for the complete picture it provides on how to use rails. After deciding rails was too difficult for a period of time, this screencast got me right back into it and over the bump.
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://5by5.tv/rubyshow" title="The Ruby Show | 5by5"&gt;The Ruby Show&lt;/a&gt;&lt;br /&gt;
If you spend a lot of time on public transport, The Ruby Show is a fantastic podcast to have with you. It goes over the latest Ruby news in a humorous and insightful way. A great laugh, and a great resource. 
&lt;/li&gt;
&lt;/ul&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s it for now, but I think these resources should definitely be sufficient to get you going, without completely overloading you with information.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/To6PPLkoEP0" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-13-ruby-on-rails</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 12 Unit Testing</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/v8nC_yIGLxA/ruby-day-12-unit-testing" />
   <updated>2010-07-10T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-12-unit-testing</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;em&gt;Before we get into unit testing in Ruby, I should apologise for the inconsistent posting of content. Amidst other work commitments, writing 1 post per day for 30 days is proving to be quite a challenge. I imagine at the end of this I will be able to write a post on &amp;ldquo;How Not to Write a Blog Post Series&amp;rdquo;. (Here&amp;rsquo;s a tip now: start writing at least a week before you start publishing)&lt;/em&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We have a reasonable HTTP server working for us now. It can handle multiple simultaneous requests, log important information out to a file, and handle query parameters in the requested URL. While we can see that it works by running it, it would be good to have a more thorough test which we can run often and easily to ensure that any future changes do not break it. We will be using the &lt;a href="http://ruby-doc.org/core/classes/Test/Unit.html" title="Module: Test::Unit"&gt;Ruby Unit Testing Framework&lt;/a&gt; to achieve this.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Writing unit tests is a bit like using source control. Once you&amp;rsquo;ve set it up and are using it regularly, your level of sanity is slightly higher than if you weren&amp;rsquo;t using it. Similarly, once you&amp;rsquo;ve written some good unit tests, they will always be there for you to check that your code runs ok. An example of where a good set of unit tests would be handy is in a series of text manipulation operations. On a UNIX machine you may simply be checking for the existence of &amp;lsquo;\n&amp;rsquo; for newlines, but if the same code is run on a Windows machine it may fail. By running the tests on each new machine, issues like this will become apparent with very little extra effort.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;As the synchronised buffer is a crucial component of the multi-threaded web server, we will create a set of unit tests specifically to test this class.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A unit test class at its most basic looks like this:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SyncBufferTest&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TestCase&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test1&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We are simply extending from the &amp;lsquo;Test::Unit::TestCase&amp;rsquo; class and then defining our test in the method &amp;lsquo;test1&amp;rsquo;. There is a convention that must be followed for defining new tests, but it is a simple one: each method must begin with &amp;lsquo;test&amp;rsquo;. You can have whatever you like afterwards, but with &amp;lsquo;test&amp;rsquo; at the front Ruby will know this is to be treated as a test case. The following are all valid test case names:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;test1&lt;/span&gt;
&lt;span class="n"&gt;test_my_test&lt;/span&gt;
&lt;span class="n"&gt;test_if_exception_thrown&lt;/span&gt;
&lt;span class="n"&gt;test123ABC&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In order to examine more of the Ruby Unit Test API, we will need to make a quick addition to the SynchronisedBuffer class. Add the following  line to the beginning of the initialize method. (i.e. before @capacity = capacity)&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Capacity must be greater than 0&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This does exactly what it says it will: raises an exception with the message &amp;ldquo;Capacity must be greater than 0&amp;rdquo; unless the capacity is greater than 0. This stops someone creating a new buffer with a capacity of 0 or -1, which simply does not make sense. We can then write test cases to check that this actually happens.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_zero_capacity&lt;/span&gt;
      &lt;span class="k"&gt;begin&lt;/span&gt;
          &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;rescue&lt;/span&gt; 
        &lt;span class="n"&gt;assert_equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Capacity must be greater than 0&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$!&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_negative_capacity&lt;/span&gt;
      &lt;span class="k"&gt;begin&lt;/span&gt;
          &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;rescue&lt;/span&gt; 
        &lt;span class="n"&gt;assert_equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Capacity must be greater than 0&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$!&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&amp;lsquo;test_zero_capacity&amp;rsquo; and &amp;lsquo;test_negative_capacity&amp;rsquo; are testing this line we just added in above. An exception should be thrown when the supplied capacity is less than or equal to 0. By using the error handling begin/rescue syntax we can catch any exceptions thrown. In the rescue section, we are calling  &amp;lsquo;assert_equal&amp;rsquo; which is a method provided by Test::Unit::TestCase to ensure that 2 values are equal. It is by convention that the expected value comes before the actual value, thus we are seeing if &amp;ldquo;Capacity must be greater than 0&amp;rdquo; is equal to the message contained in the thrown exception. &amp;lsquo;$!&amp;rsquo; is a global variable that contains the most recently thrown exception, and the message method returns the message that the exception contains.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If we now run this file we should see output like below
&lt;img src="/uploads/2010/07/Screen-shot-2010-07-09-at-11.24.10-PM.png" alt="Test output" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A dot, &amp;lsquo;.&amp;rsquo;, represents a test that has passed. We can see results in a sentence as well, &amp;lsquo;2 tests, 2 assertions, 0 failures, 0 errors&amp;rsquo;. We have 2 tests that asserted 1 statement each, and neither failed nor threw an error. If we remove the begin/rescue block from each test, and simply have &amp;lsquo;SynchronisedBuffer.new(0)&amp;rsquo; then there would appear an &amp;lsquo;E&amp;rsquo; for error instead of a &amp;lsquo;.&amp;rsquo; as the thrown exception is not being rescued. Alternatively, if in our SynchronisedBuffer class we did not put in the line to raise the exception, we would see an &amp;lsquo;F&amp;rsquo; in place of a &amp;lsquo;.&amp;rsquo; as no exception is being thrown, and so no assertion is true. (This idea of writing tests that will fail before the function has actually been implemented is known as Test Driven Development).&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The trick to unit testing is finding the right set of test cases that don&amp;rsquo;t overlap, but don&amp;rsquo;t miss anything out, either. We have tested the capacity of 0, which is neither a positive nor a negative number. It is in a class of it&amp;rsquo;s own and so we must include this test. -1, however, is a negative number just like -2 or -9,235,123. It is in a class with an infinite amount of other negative numbers. It would be impossible, and senseless, to test each negative number as we know that the condition only requires the number to be greater than 0. -1 is therefore sufficient to ensure that it will work with any negative number.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Let&amp;rsquo;s now test the empty? and full? methods.
Here we are creating a new buffer with a valid capacity and then calling the &amp;lsquo;assert&amp;rsquo; method. This simply checks if the value inside returns true. If it does, then the assertion is true. Without putting in any objects, empty? should return true. With a buffer of capacity 1, after putting in 1 item full? should return true.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_empty?&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_full&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;full?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;These tests check that full? does not return true when it shouldn&amp;rsquo;t, and likewise with empty?.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_not_full&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;full?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_not_empty&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We now have some simple test cases in place, but they only ensure a limited part of the functionality. What we really want to test are the put and get methods. As these methods can potentially put to sleep the thread that calls them, we have to be careful when testing them. The scenarios we must test for are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;calling put while the buffer is empty&lt;/li&gt;
&lt;li&gt;calling put while the buffer is full&lt;/li&gt;
&lt;li&gt;calling get while the buffer is empty&lt;/li&gt;
&lt;li&gt;calling get while the buffer is full&lt;/li&gt;
&lt;li&gt;calling get while a thread is waiting on put&lt;/li&gt;
&lt;li&gt;calling put while a thread is waiting on get&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;and we can translate them to the following test case names&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_put_while_empty&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_put_while_full&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_while_empty&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;  

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_while_full&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_waking_up_sleeping_thread&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_waking_up_sleeping_thread&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Our first test is rather simple
We create a new buffer with a capacity of 1. After putting in 1 item, the buffer should be full. The buffer was empty when we called put, so the current thread should not be put to sleep while waiting for the buffer to empty. Thus we are asserting that the status of the current thread is equal to &amp;lsquo;run&amp;rsquo;. This concept of checking thread status will be very important for the rest of the test cases.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_put_while_empty&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;assert_equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;run&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;As before, we are creating a new buffer and placing in an item so it becomes full. As the buffer is full, any subsequent calls of put will cause the calling thread to sleep. As we don&amp;rsquo;t want to put our main thread to sleep, we create a new thread and pass in the buffer. By calling &amp;lsquo;put&amp;rsquo; in this thread, we expect the thread to go to sleep. The line that comes after,&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_put_while_full&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 2&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pass&lt;/span&gt;  &lt;span class="c1"&gt;# run &amp;#39;thread&amp;#39; to ensure it sleeps&lt;/span&gt;
    &lt;span class="n"&gt;assert_equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;sleep&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Thread should be asleep waiting to put&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kill!&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Thread.pass is important as it without it, assert_equal might be called before the new thread has had a chance to actually call &amp;lsquo;put&amp;rsquo;. If this were the case, the thread would not yet be asleep, and so it&amp;rsquo;s status would be &amp;lsquo;run&amp;rsquo;, and the test would fail. &amp;lsquo;Thread.pass&amp;rsquo; says to the Ruby thread scheduler &amp;ldquo;I&amp;rsquo;ve had enough, let another thread run for a while&amp;rdquo;. This would give the thread to actually call buffer.put and then we can test if it&amp;rsquo;s state is set to &amp;lsquo;sleep&amp;rsquo;. Lastly, while perhaps not necessary, we are destroying this new thread.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-19'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-19"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Having written this test case, the subsequent ones will follow much the same pattern.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_while_empty&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pass&lt;/span&gt; &lt;span class="c1"&gt;# run &amp;#39;thread&amp;#39; to ensure it sleeps&lt;/span&gt;
    &lt;span class="n"&gt;assert_equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;sleep&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Thread should be asleep waiting to get&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kill!&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_while_full&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-20'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-20"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This last test case has a minor addition, &amp;lsquo;thread.join&amp;rsquo;. We are testing the case when we are trying to put an item into a full buffer. This call will make the thread sleep until someone else calls &amp;lsquo;get&amp;rsquo;. Once get is called, we want to ensure that the thread waiting actually completes calling &amp;lsquo;buffer.put&amp;rsquo; and so we use the join method. Join makes the thread behave synchronously, pausing the current thread until the joined thread has finished executing. If thread.alive? returns false, we know that this thread has died. For it to have died it must have completed executing, and for that to have happened it must have been woken up. Thus if the assertion is true, calling &amp;lsquo;get&amp;rsquo; successfully woke up our thread. The same applies for calling get on an empty buffer.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_waking_up_sleeping_thread&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 2&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pass&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alive?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_get_waking_up_sleeping_thread&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pass&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;item 1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alive?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-21'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-21"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The results of all our test cases running should look something like the following:
&lt;img src="/uploads/2010/07/Screen-shot-2010-07-10-at-12.31.51-AM.png" alt="Green is a happy colour" /&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-22'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-22"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;With all these test cases in place, we can be fairly certain that our implementation is correct and the any future bugs that may be introduced will be caught. Note that each test case only makes 1 assertion. While this is not entirely necessary, it is good practice. If a test case fails to pass, then there is no doubt as to which assertion failed. By placing 10 assertions into a test case &amp;lsquo;test_buffer&amp;rsquo; it  becomes much more difficult to find the exact cause of the failure as the name &amp;lsquo;test_buffer&amp;rsquo; failing tells us nothing and what was tested. If multiple assertions are made in the same test case, either one or more of them is redundant, or the test cases should be separated into multiple separate cases.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-23'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-23"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Hopefully these examples give you an understanding of how to use the Ruby Unit Testing Framework, as well as how to test synchronised and multi-threaded classes. As always, please leave a comment or send me an email with any feedback, criticisms, questions or comments.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/v8nC_yIGLxA" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-12-unit-testing</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 11 Zlib And Gzip</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/BwfYiUiMTcA/ruby-day-11-zlib-and-gzip" />
   <updated>2010-07-05T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-11-zlib-and-gzip</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;While high speed internet is gradually working its way into most homes, it&amp;rsquo;s not absolutely everywhere, and when it is, your ISP may be limiting speeds during certain times of the day or for certain types of internet traffic. To make things easier on users, we can have our web servers compress the data they send. Instead of sending a 200kb Javascript file, it can first be compressed on the server to ~50kb, sent via TCP, and then uncompressed by the web browser. The amount of data being sent is &amp;frac14; of the size, and so the total time is reduced significantly (time taken to compress and uncompress is considered negligible).&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Unfortunately, my attempts to wrangle &lt;a href="http://ruby-doc.org/core/classes/Zlib.html" title="Module: Zlib"&gt;Ruby&amp;rsquo;s Zlib compression library&lt;/a&gt; have not been successful. The documentation is patchy, at best, with comments such as &amp;ldquo;???&amp;rdquo;, &amp;ldquo;TODO: better comments&amp;rdquo;, and the occasional snippet of Japanese. Given today&amp;rsquo;s time constraints, I&amp;rsquo;m shipping an example that I feel should work, but does not. I will work on resolving the issue, and update this post when it works.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Connection: close&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Content-Type: application/gzip&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;

&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;  
    &lt;span class="n"&gt;gz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Zlib&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GzipWriter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Zlib&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BEST_COMPRESSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Zlib&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;FINISH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="n"&gt;gz&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="n"&gt;gz&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I have modified the header to say we are sending the browser compressed data, and created a new Gzip writer object inside the response loop which spits out the contents of the requested file into the gzip writer, which has been given the socket. The documentation states that Gzipwriter can take an IO object, which socket happens to be, however when it runs an exception is thrown saying that the IO file has been closed. While possibly something simple, the answer has thus far eluded me.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If you have any suggestions or solutions, leave a comment or send a tweet/email. Stay tuned for updates.&lt;/pre&gt;&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/BwfYiUiMTcA" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-11-zlib-and-gzip</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 10 Uri</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/2DcRLmfelPs/ruby-day-10-uri" />
   <updated>2010-07-04T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-10-uri</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Today we will take a brief look at Ruby&amp;rsquo;s &lt;a href="http://ruby-doc.org/core/classes/URI.html" title="Module: URI"&gt;Uniform Resource Identifier (URI) class&lt;/a&gt; in order to handle a wider range of requests in our web server. Currently, if we enter a URL such as &amp;lsquo;http://localhost:8080/test.html&amp;rsquo; into our web browser we will receive a request that looks like &amp;lsquo;GET /index.html HTTP/1.1&amp;rsquo;. If we want to send any extra parameters such as an id (http://localhost:8080/test.html?id=123) then our server will naively assume that the file we are requesting is named &amp;lsquo;index.html?id=123&amp;rsquo;, and so it will not be able to find it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;By treating the request as an &lt;a href="http://www.bernzilla.com/item.php?id=100"&gt;HTTP URI&lt;/a&gt;, we can interpret the request more intelligently, separating it into the filename and the query parameters.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We can modify our worker class like so&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;.&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;

&lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="no"&gt;URI&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The &amp;lsquo;split&amp;rsquo; method will take a URL and split it up into 9 components which will be explained by using the URL &amp;lsquo;http://www.google.com/index.html?user=123&amp;rsquo; as an example&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Scheme &amp;ndash; http
Userinfo &amp;ndash; nil
Host &amp;ndash; www.google.com
Port &amp;ndash; 80
Registry &amp;ndash; nil
Path &amp;ndash; /index.html
Opaque &amp;ndash; nil
Query = user=123
Fragment &amp;ndash; nil&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;(Userinfo, registry, opaque and fragment are not likely to be used for an HTTP request so we can ignore them.)&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The two components we are interested in are path and query, elements 5 and 7 respectively. By passing the middle part of our GET request to the split method, we can grab the 2 elements we need. &amp;lsquo;uri[5]&amp;rsquo; contains the file we want, so that will be passed to the File class. &amp;lsquo;uri[7]&amp;rsquo; contains the query parameters which can be dealt with later on.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/2DcRLmfelPs" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-10-uri</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 9 Logging</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/UuVDztvwZMo/ruby-day-9-logging" />
   <updated>2010-07-02T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-9-logging</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;With most applications, it is important to keep an accurate and comprehensive record of any problems or significant events that occur during its execution. This typically involves sending this information to a log file stored on the computer. The Ruby &lt;a href="http://ruby-doc.org/core/classes/Logger.html" title="Class: Logger"&gt;Logger&lt;/a&gt; class lets us easily take care of this.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;After importing &amp;lsquo;logger&amp;rsquo;, we can create a new logger like so:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;server.log&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;DEBUG&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We want to save all messages into the file &amp;lsquo;server.log&amp;rsquo;. Alternatively, we could have entered &amp;lsquo;STDOUT&amp;rsquo; to send all logging information to standard out. The second line sets the types of messages we want to log, which can be any of
FATAL:  an unhandleable error that results in a program crash
ERROR:  a handleable error condition
WARN:   a warning
INFO:   generic (useful) information about system operation
DEBUG:  low-level information for developers
(Taken from &lt;a href="http://ruby-doc.org/core/classes/Logger.html" title="Class: Logger"&gt;Ruby API&lt;/a&gt;)&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The level you set log.level to will mean that any messages logged equal to, or above this level, will be logged.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;WARN&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This would result in FATAL, ERROR and WARN messages being logged, but INFO and DEBUG being ignored. In a production environment you may only want to log fatal errors, while in development mode you would most likely want to see DEBUG information.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;To add a message to the log we can simply call a method that has the name of the level we are logging to.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fatal&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Program will now crash&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;An exception occurred, but we&amp;#39;re onto it&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;warn&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Memory running low&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;1000 customers have now signed up&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;The program is on line 25&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Let&amp;rsquo;s modify our multi-threaded server to include some logging.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;server.log&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;DEBUG&lt;/span&gt;
  
&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TCPServer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;8080&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Server started on 127.0.0.1:8080&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;workers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;workers&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Worker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;workers&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;worker&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_s&lt;/span&gt;  
   &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;#39;Worker &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;workers&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; created&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;New connection from &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;peeraddr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In each case we are printing out some useful, but non-critical information, so it is logged as info. We can also add logging to the &lt;a href="http://www.oneofthesedaysblog.com/serve/thread.rb" title="Thread.rb"&gt;worker class&lt;/a&gt; &amp;ndash;  the file has been attached for brevity&amp;rsquo;s sake.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If we run the web server with the changes made above, we will see entries in our server.log file like the following:
Logfile created on Fri Jul 02 19:57:47 +1200 2010 by logger.rb/22285&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I, [2010-07-02T19:57:54.231591 #14068]  INFO &amp;mdash; : New connection from localhost&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I, [2010-07-02T19:57:54.232061 #14068]  INFO &amp;mdash; : worker1 has received a new socket&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;As each message has a precise timestamp, it can useful for tracking down bugs and performance issues. While &amp;lsquo;puts&amp;rsquo; may be a simpler and quicker method for printing out this kind of information to standard out, the extra information and structure that logger provides makes it well worth using.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/UuVDztvwZMo" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-9-logging</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 8 Multi Threading Synchronisation And Buffers</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/PHEhTmGzGp4/ruby-day-8-multi-threading-synchronisation-and-buffers" />
   <updated>2010-07-02T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-8-multi-threading-synchronisation-and-buffers</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now that you&amp;rsquo;ve seen the &lt;a href="http://www.oneofthesedaysblog.com/ruby-day-7-http-protocol/ "&gt;HTTP protocol in action&lt;/a&gt;, we can move on to a more advanced version. We will turning the server into a multi-threaded one, allowing it to handle multiple requests simultaneously. Previously, once we had accepted a socket we had to finish processing it until we could accept another one. This meant that if another person tried to connect while it was processing an earlier request, it would have to wait. By sending each request to a separate thread, more than one can be processed at a time resulting in a more responsive server for everyone.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The approach we will take to implement this is to use so-called &lt;a href="http://en.wikipedia.org/wiki/Circular_buffer"&gt;circular buffer&lt;/a&gt;. It is essentially a ring with a start and an end. Items go into the end, and come out the front. As we add items, we move the back along to an empty slot. When we take items out, we also move the front along to an empty slot. When the front is the same as the back, we know the buffer is either completely full or completely empty. Lastly, the buffer will have a fixed capacity so that when the front is moved to a position greater than the capacity, it wraps back around and starts from 0.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;When we accept a connection the returned socket will be placed into the buffer. A group of threads will continually be checking the buffer for new sockets, and when one is found a thread will take it and process it. This process is useful because it allows us to completely separate our request handling and response generation. We will call these threads &amp;lsquo;Worker&amp;rsquo; threads, and the collection of them will be called a thread pool. Thus, a new request will be placed into the buffer and processed by a worker from the thread pool.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;There is a slight caveat to working with threads that use any form of shared data structure (each thread has access to the same circular buffer). A thread can be interrupted at any time by the scheduler and so a number of problems could ensue. For example, let&amp;rsquo;s say we have a method called &amp;lsquo;put&amp;rsquo;, which takes a given object and stores it, and then updates the count of the number of stored objects. If the thread is interrupted in between the storing of the object and the updating of the count, the data structure is no longer consistent with itself. If there were no elements added before &amp;lsquo;put&amp;rsquo; was called, the buffer will still say it is empty even though one object has been added. What&amp;rsquo;s worse is that these problems are wholly unpredictable, so we must find a way to ensure these problems can&amp;rsquo;t occur. We need a way of guaranteeing that once &amp;lsquo;put&amp;rsquo; is called, no one else will access the buffer until the method has finished.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Ruby provides a class called &amp;lsquo;Monitor&amp;rsquo; that will allow us to achieve this by letting us create &amp;lsquo;synchronised&amp;rsquo; blocks of code. A synchronised block is one that can only be accessed by one thread at a time. A thread can get a lock on the method, and no other thread will have access to it until the lock is released. Let&amp;rsquo;s go over the logic we will need to implement for our buffer.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We need to put items in, and get items out.
The buffer will have a fixed size, and when it is full we should not be able to put anything more in.
When it is empty, we should not be able to get anything out.
If we want to get something while it is empty, it should pause until something has been put in, and then return it.
If we want to put in something while it is full, it should pause until something has been removed.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Let&amp;rsquo;s look at the implementation of this, then go over it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;monitor&amp;#39;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SynchronisedBuffer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Monitor&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We are extending the Monitor class, which gives us access to &amp;lsquo;synchronize&amp;rsquo; and the wait/signal methods. Next, the buffer is initialised&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="vi"&gt;@capacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;
      &lt;span class="vi"&gt;@front&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="vi"&gt;@back&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="vi"&gt;@elements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
      &lt;span class="vi"&gt;@empty_cond&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_cond&lt;/span&gt;
      &lt;span class="vi"&gt;@full_cond&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_cond&lt;/span&gt;
      &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;After setting capacity, front position, back position, and creating a new array to hold our elements, we come to our first bit of the Monitor class. &amp;lsquo;new_cond&amp;rsquo; is a method that returns a ConditionVariable object. This condition variable is an important construct that ensures the code will only execute when the condition is satisfied. We have a condition that elements can&amp;rsquo;t be removed when the buffer is empty (empty_cond) and a condition that we can&amp;rsquo;t add new elements when the buffer is full (full_cond).
To have a section of code that is synchronised, we call the &amp;lsquo;synchronize&amp;rsquo; method and have the code inside a do-end block. Once inside, we call the &amp;lsquo;wait_while&amp;rsquo; method on the empty condition. We are passing in the method &amp;lsquo;empty?&amp;rsquo; as our condition, and so the line can be read as &amp;lsquo;have any thread that calls this method wait as long as the buffer is empty&amp;rsquo;. Alternatively, we could write &amp;lsquo;wait_unless { !empty? }&amp;rsquo; which would read &amp;lsquo;let any thread call this method unless the buffer is empty, in which case, make them wait&amp;rsquo;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;
       &lt;span class="n"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
           &lt;span class="vi"&gt;@empty_cond&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_while&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;empty?&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;   
           &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@elements&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@front&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
           &lt;span class="vi"&gt;@elements&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@front&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
           &lt;span class="vi"&gt;@front&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@front&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="vi"&gt;@capacity&lt;/span&gt;
           &lt;span class="vi"&gt;@full_cond&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt; 
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; 
        &lt;span class="k"&gt;end&lt;/span&gt;               
   &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;When the buffer is empty, any thread that calls get will be put to sleep. If the buffer isn&amp;rsquo;t empty, then the element at the front is removed, and the front position is moved along by 1. The line &amp;lsquo;@full_cond.signal&amp;rsquo; will wake up a thread that was put to sleep waiting for the full buffer to have some space. We have just removed an item, therefore there is now space to put in a new item, so we can wake up, or &amp;lsquo;signal&amp;rsquo; a sleeping thread.
&amp;lsquo;put&amp;rsquo; is the opposite of get. Threads must wait if the buffer is full, and when there is space, an element is placed at the back of the buffer. Once this is done, &amp;lsquo;@empty_cond.signal&amp;rsquo; is called and a thread who was put to sleep waiting for the buffer to have some items placed in it, is woken up. As only one thread is woken up at a time, it functions on a first come first served basis.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;             
       &lt;span class="n"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
           &lt;span class="vi"&gt;@full_cond&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_while&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;full?&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
           &lt;span class="vi"&gt;@elements&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@back&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;
           &lt;span class="vi"&gt;@back&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@back&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="vi"&gt;@capacity&lt;/span&gt;           
           &lt;span class="vi"&gt;@empty_cond&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;    
        &lt;span class="k"&gt;end&lt;/span&gt;        
   &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The final two methods we have already seen used above
If the front is the same position as the back we know that the buffer is either empty or full. We know it will be empty if the element at this position is nil, and full if the element at this position is not nil. These methods must also be synchronised as otherwise they could be interrupted in-between the two conditions, and a corrupted buffer state could result.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;full?&lt;/span&gt;
     &lt;span class="n"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@front&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="vi"&gt;@back&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="vi"&gt;@elements&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@front&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;end&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;empty?&lt;/span&gt;
       &lt;span class="n"&gt;synchronize&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
           &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@front&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="vi"&gt;@back&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="vi"&gt;@elements&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@front&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Sincere thanks must go to Robert Klemme from the &lt;a href="http://www.ruby-forum.com"&gt;Ruby Forum&lt;/a&gt; who helped me work out the bugs in this implementation, as well as Craig Taverner from &lt;a href="http://snippets.amanzi.org/2007/07/3-ruby-threads.html"&gt;his blog&lt;/a&gt; for introducing the monitor class.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So now we have a synchronised buffer which we can fill with incoming requests. What we need next is a thread pool full of workers who are ready to process the contents of the buffer. The class below should look fairly familiar to what was in the &lt;a href="http://www.oneofthesedaysblog.com/ruby-day-7-http-protocol"&gt;basic web server&lt;/a&gt;, with just a few additions.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;thread&amp;#39;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Worker&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
            &lt;span class="k"&gt;begin&lt;/span&gt;
                &lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
                    &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;

                    &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;

                    &lt;span class="n"&gt;validGET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/GET .* HTTP\/1\.1/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

                    &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;validGET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
                        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;HTTP/1.1 400 Bad Request&amp;quot;&lt;/span&gt;
                        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
                        &lt;span class="k"&gt;next&lt;/span&gt;
                    &lt;span class="k"&gt;end&lt;/span&gt;

                    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
                    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;.&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;

                    &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exists?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; 
                        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;HTTP/1.1 404 File Not Found&amp;quot;&lt;/span&gt;
                        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
                        &lt;span class="k"&gt;next&lt;/span&gt;
                    &lt;span class="k"&gt;end&lt;/span&gt;

                    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
                    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Connection: close&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
                    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Content-Type: text/html&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
                    &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; 
                        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
                            &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
                        &lt;span class="k"&gt;end&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;

                    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
                &lt;span class="k"&gt;end&lt;/span&gt;
            &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
                &lt;span class="vg"&gt;$stderr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="vg"&gt;$!&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;
            &lt;span class="k"&gt;end&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;          
    &lt;span class="k"&gt;end&lt;/span&gt;    
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The main difference is at the top, and the bottom&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Worker&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
            &lt;span class="k"&gt;begin&lt;/span&gt;
                &lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
                    &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
   				&lt;span class="vg"&gt;$stderr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="vg"&gt;$!&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;          
  &lt;span class="k"&gt;end&lt;/span&gt;    
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Extending the Thread class means we can treat Worker objects exactly as if they were threads. To do this however, we also need to implemenet the initialize method. When creating a regular thread, the instructions to execute are passed to it as a block&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;# do something }&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Therefore, all we need to do is call &amp;lsquo;super&amp;rsquo;, as this will call the initialize method on the parent class, Thread. Placing the block after this will mean that it is executed by the Thread class. We want to pass in the buffer for it to use, so this is passed in as an argument and then into the block.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;As each thread will be running outside of the main execution thread, we will not be informed of any errors that occur which can make bug finding difficult. If we wrap the code to be executed in Ruby&amp;rsquo;s equivalent of a try/catch block then we can grab any exceptions thrown and send them to standard error. &amp;lsquo;$stderr.puts $!.inspect&amp;rsquo; is a neat shortcut that uses the $! global variable. This contains the most recently thrown exception, and so we are calling inspect on this and sending it to stderr.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Lastly, instead of executing the processing code once, we are looping infinitely. We can to continually check the buffer for new sockets with &amp;lsquo;buffer.get&amp;rsquo;. When the buffer is empty, &amp;lsquo;buffer.get&amp;rsquo; will put the worker to sleep, and it will be woken up eventually when a new socket is placed into the buffer.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-19'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-19"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Last but not least, we need to create a number of workers, and set up our buffer.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TCPServer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;8080&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-20'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-20"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We are creating our buffer with a capacity of 100. This means that we can have 100 requests queued up until we have to start waiting for the buffer to be emptied. We are also creating 40 workers, and naming them so we can tell them apart. Lastly, we have our familiar &amp;lsquo;while socket = server.accept&amp;rsquo; loop but instead of doing any processing, we simply put it into the buffer.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SynchronisedBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;workers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;workers&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Worker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;workers&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;worker&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_s&lt;/span&gt;  
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-21'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-21"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;All things going well, your web server should now be capable of handling many simultaneous requests. Leave a comment, email me, send me a tweet or a message on Facebook with any questions, criticisms or comments.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/PHEhTmGzGp4" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-8-multi-threading-synchronisation-and-buffers</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 7 Http Protocol</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/ingaesNYkYo/ruby-day-7-http-protocol" />
   <updated>2010-06-29T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-7-http-protocol</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We have created a simple server that responds with whatever text it received when we connect to it via TELNET. The uses for this are limited and so we want now to begin implementing the &lt;a href="http://www.w3.org/Protocols/" title="HTTP - Hypertext Transfer Protocol Overview"&gt;HyperText Transfer Protocol (HTTP)&lt;/a&gt; to enable us to show a web page. Being a standard, and universally adopted protocol, a web browser knows that if it uses HTTP to talk to a web server, they will both understand each other. HTTP defines a set of actions that a web server must carry out. These are:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OPTIONS&lt;/strong&gt; - Returns information and options available for the server, or a specified resource. 'OPTIONS *' will tell you what the entire server is capable of, whereas 'OPTIONS filename' will tell you what it can do with that particular file.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GET&lt;/strong&gt; - Returns the contents of the specified file or directory. 'GET index.html' will return the file index.html. The GET method is what we will be implementing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HEAD&lt;/strong&gt; - Works the same as GET, except the contents of the file or directory is not returned. Instead, meta-information is returned, such as the file-type or when it was last modified.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;POST&lt;/strong&gt; - Sending information along with a POST request will associate with the specified file. 'POST index.php?foo=bar' will send the value 'bar' to the file 'index.php', storing it under the name 'foo.'&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PUT&lt;/strong&gt; - PUT is the opposite of GET: it sends a file and a location, and the server will store that file in that location.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DELETE&lt;/strong&gt; - You can remove a specified file with 'DELTE filename', however there is no guarantee that the server will actually do this.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TRACE&lt;/strong&gt; - Lists the exact servers a request must go through to reach the destination server. This may be a proxy server, for example.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CONNECT&lt;/strong&gt; - Not used in HTTP 1.1, CONNECT is reserved for future implementations that may wish to set up some form of secure communication.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;(Paraphrased and adapted from &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html" title="HTTP/1.1: Method Definitions"&gt;W3C&lt;/a&gt;)&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;For a basic web server we will only need to implement the GET method, as that will at least allow us to send content to the browser. We&amp;rsquo;ll also want to send back a few status codes. Status codes let the browser determine what the server has done. The most prevalent example is 404 &amp;ndash; File Not Found. Equally as important is 200 &amp;ndash; OK, which is returned when the server can successfully carry out the request. Along with these 2, we will send back 400 &amp;ndash; Bad Request if something other than a GET request is sent. The entire server code is below, and each step is explained in detail afterwards.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;socket&amp;#39;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The first 3 lines should look familiar.
We&amp;rsquo;re setting the program to listen on TCP port 8080, and once a connection is made, we&amp;rsquo;re storing the received information in the &amp;lsquo;request&amp;rsquo; variable.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TCPServer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;8080&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;After this we&amp;rsquo;re checking if the request is a valid HTTP 1.1 GET request.
We are using the regular expression &amp;lsquo;/GET .&lt;em&gt; HTTP\/1.1/&amp;rsquo; to check if the request fits the format &amp;lsquo;GET file HTTP/1.1&amp;rsquo;. &amp;lsquo;.&lt;/em&gt;&amp;rsquo; represents any sequence of characters, of any length. While this isn&amp;rsquo;t perfect, it should suffice for now.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;validGET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/GET .* HTTP\/1\.1/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&amp;lsquo;unless&amp;rsquo; functions identically to &amp;lsquo;if not&amp;rsquo;, so the code inside will only be executed if validGET is not equal to something other than nil. validGET will be nil if the request string did not match the regular expression. Hence, if the request doesn&amp;rsquo;t match we are sending back &amp;lsquo;HTTP/1.1 400 Bad Request&amp;rsquo;, and closing the socket. &amp;lsquo;next&amp;rsquo; stops Ruby from executing the remaining contents of the loop, and forces it to start the loop again.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;validGET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;HTTP/1.1 400 Bad Request&amp;quot;&lt;/span&gt;
        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
        &lt;span class="k"&gt;next&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here we are simply finding the name of the file they requested the server to load. The GET request consists of 3 parts, with a space in between each: &amp;lsquo;GET&amp;rsquo; &amp;lsquo;filename&amp;rsquo; &amp;lsquo;HTTP/1.1&amp;rsquo;. Thus, the second element in the array returned by split will be the filename. We prepend the filename with a &amp;lsquo;.&amp;rsquo; to ensure that the resulting file path is relative to the current directory. If they request index.html, the request will come through as /index.html. By prepending &amp;lsquo;.&amp;rsquo; the file becomes ./index.html which is relative to the current directory.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;.&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Just as we checked for a correct GET request, we need to check if the requested file can actually be sent. File.exists? will return true if the given file exists on the computer. If not, we send back the famous 404 error.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exists?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;HTTP/1.1 404 File Not Found&amp;quot;&lt;/span&gt;
        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
        &lt;span class="k"&gt;next&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Having got to this point in the code, we can say that we have received a correctly formed request for a file that we are able to send back. What we do next is part of the HTTP protocol, and is us telling the client that the request can indeed be processed. This is the 200 OK response. Following this, we send a &amp;lsquo;Connection: close&amp;rsquo; to indicate that we have finished sending back this message. &amp;lsquo;Content-Type: text/html&amp;rsquo; is the beginning of a new message, and it is telling the client that they can expect to receive an html file. If we want our web server to handle images, and other file types, we will have to add in logic to send back the correct content type. Today we will just stick with text/html.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Connection: close&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Content-Type: text/html&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Finally, we can send the contents of the html to the client. We simply open up the requested file and loop over each line in it. For each line, we send it back through the socket.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;    &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; 
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
            &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;To test the server, create an html file called test.html in the same directory as the Ruby file containing the above code. Run the program, and point your browser to &amp;lsquo;http://127.0.0.1:8080/test.html&amp;rsquo;. Hopefully you should see the contents of the html file rendered by the browser.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;There you have it. A web server that handles HTTP 1.1 GET requests to successfully send back an HTML file, while also handling malformed requests, and requests for non-existent files. Granted, there is a large amount of functionality that we didn&amp;rsquo;t implement, including some important error checking, but hopefully it highlights the overall mechanisms of HTTP and web servers.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/ingaesNYkYo" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-7-http-protocol</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 6 Sockets And Tcp</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/J-Lm8n7jfvo/ruby-day-6-sockets-and-tcp" />
   <updated>2010-06-28T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-6-sockets-and-tcp</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;To kick off this web-server series we&amp;rsquo;ll create a server which, at it&amp;rsquo;s simplest, consists of just 3 lines of Ruby code. It can be called an echo server, and it does just that; it sends back to you whatever you send to it. Find a large, empty room, yell out &amp;ldquo;Ruby is awesome&amp;rdquo; and you&amp;rsquo;ll hear it repeated (this also works in a small room filled with Ruby developers). If you&amp;rsquo;re not fond of going outside (or socialising) however, you can use an echo server to achieve the same effect. Send the text &amp;ldquo;Ruby is awesome&amp;rdquo; to this echo server, and it will reply with the same message.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This simple example expresses a concept that is fundamental to how any server works. They listen for requests, do something, and (sometimes) send something back. A web server is listening to requests to load a particular page, and in response, it sends back the contents of that page. A mail server listens for requests to download mail messages, and in response it sends the latest emails. As most servers follow this basic pattern, standards have been developed and adopted that describe in what format the requests and responses should be. Warning: You are now entering the world of acronyms.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;TCP, UDP, HTTP, STMP, NTS, FTP, ARP, SSH, IMAP, DNS, DHCP, IRC, to name but a few, are all protocols that describe requests and responses. Today we will be using the Transmission Control Protocol (TCP) to implement our simple echo server. To describe TCP, we can look at it&amp;rsquo;s simpler cousin: the Universal Datagram Packet (UDP). UDP is a protocol that specifies how data should be broken up into chunks, in order to send over a network. It also specifies how that data knows where it has to go. By pairing up a packet with an Internet Protocol (IP) address, network hardware will (all things going well) be able to send a packet it receives to the correct destination. The packet also contains the IP address of the sending machine, so that the receiver knows where to send its response. TCP does exactly what UDP does, except it does a few extra things to ensure that packets sent do actually arrive correctly. This includes error checking and correction, as well as ensuring that the sender isn&amp;rsquo;t sending more packets that the receiver can handle at once.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If we want to create an &amp;lsquo;echo&amp;rsquo; server, we will need to listen for packets that are sent to us via the TCP protocol. When we receive one, we need to extract the message, and send it back again. Here&amp;rsquo;s the server:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;socket&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TCPServer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;8080&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;
&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Firstly, we&amp;rsquo;re creating a new TCPServer object that will listen for requests at the IP address 127.0.0.1 and on port 8080. 127.0.0.1 is an IP address reserved to represent the local machine. If the operating system is asked to send a packet to this address, it realises that this is actually the same machine and so it is a good way of testing without requiring 2 computers. Port 8080 is a common port used for alternative web servers, which we will be creating eventually, so we may as well use it now. As the network card receives packets for the entire operating system, and not specific programs (mail client, chat client, etc.), a program must associate itself with a port number. The operating system can then look at the port number in the received packet and determine which program it should go to. Packets with port number 21 will be sent to your FTP client, port 110 packets will go to your mail client. We want packets with port 8080 to be sent to our Ruby program. To ensure there is no confusion, only one program can take one port number at a time, so if you receive an error about port 8080 being use, you may have another program already attached to it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;When you run this program, nothing will happen. The program will continue running forever and nothing will be displayed to the screen. Why doesn&amp;rsquo;t the program end? The answer is in the second line, server.accept. Accept is a method which causes the program to pause until the TCPServer object receives a packet. As soon as it does, it returns a Socket object which contains the received packet and information on the machine who sent it. This socket acts as our end of the connection created between us and them, &lt;a href='http://www.youtube.com/watch?v=f99PcP0aFNE'&gt;a tube of the internet&lt;/a&gt;, if you will allow the analogy. Whatever is sent down this tube, we will eventually receive, and vice versa. Therefore, to create our echo, we come to the third line.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We read the socket, which will contain the message they sent, and we use &amp;lsquo;puts&amp;rsquo; to sent it straight back. To see this in action, we can use telnet to connect to our running server and send it a message.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;telnet&lt;/span&gt; &lt;span class="mi"&gt;127&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The screencast below will show it working.&lt;/p&gt;

&lt;object width="600" height="450"&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=12910541&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=00ADEF&amp;amp;fullscreen=1" /&gt;&lt;embed src="http://vimeo.com/moogaloop.swf?clip_id=12910541&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=00ADEF&amp;amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="600" height="450"&gt;&lt;/embed&gt;&lt;/object&gt;


&lt;p&gt;&lt;a href="http://vimeo.com/12910541"&gt;A Simple TCP Echo Server in Ruby&lt;/a&gt; from &lt;a href="http://vimeo.com/user4148440"&gt;Sam Dalton&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Voila! Hopefully you managed to get it working, and understood why it works as well. While this is a server in just 3 lines, it&amp;rsquo;s not particularly useful. Once someone connects, the programs ends and the server closes. To make it run indefinitely we will need some kind of infinite loop.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accept&lt;/span&gt;
    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;
    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In Ruby, every operation returns something, even variable assignment. The returned value will be equal to whatever value was set. So in this case, when server.accept detects a connection and returns a socket object, the while loop will see that an object was returned. An object is not regarded as false, and so the loops is entered. When the loop ends, server.accept is called once more, and the process repeats indefinitely. The program will never end because server.accept will always wait until it gets a connection, and hence will always return a non-nil value to the while loop condition. Just as with files, we also have to close the socket. We didn&amp;rsquo;t do this in the first example because the program exited straight afterwards so there wasn&amp;rsquo;t much need.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So there you have it, a 3 (or 4) line echo server that uses the TCP protocol.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/J-Lm8n7jfvo" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-6-sockets-and-tcp</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 5 Building A Web Server</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/jH1mUEMFvz8/ruby-day-5-building-a-web-server" />
   <updated>2010-06-27T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-5-building-a-web-server</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Looking through the upcoming topics that I plan to cover, I noticed a connection: they all cover important aspects of a web server. Threads let us handle multiple requests simultaneously, queues let us store pending requests and a mutex ensures that it won&amp;rsquo;t become corrupted from multiple threads accessing it. We also need a way of logging requests, interpreting URLs, and certainly, we&amp;rsquo;d like to test it to be sure it all works. So instead of covering the remaining topics in my proposed order, I will instead create sections of the web server that each focus on a different part of the Ruby API. Not only will this provide a more hands-on and relevant example, but it will also serve as a basic tutorial on how the HTTP protocol and web servers work. The resulting server will by no means be any competition to existing Ruby servers such as &lt;a href='http://github.com/fauna/mongrel'&gt;Mongrel&lt;/a&gt; or &lt;a href='http://code.macournoyer.com/thin/'&gt;Thin&lt;/a&gt;, but it will serve us well for the purposes of teaching.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;First up will be sockets, as with out that we have no hope of communicating with anyone.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/jH1mUEMFvz8" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-5-building-a-web-server</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 4 Forwardable</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/X2ArNK3cvGw/ruby-day-4-forwardable" />
   <updated>2010-06-26T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-4-forwardable</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Today&amp;rsquo;s topic is a favourite of upper management: delegation. Delegation is when you make someone else do something for you, but you still get the credit for it. The &lt;a href='http://ruby-doc.org/core/classes/Forwardable.html'&gt;Forwardable&lt;/a&gt; class in Ruby does much the same thing. It lets an object call methods on another object, but appearing to the user like the first object is actually doing the work. To see the advantage of forwardable, we can look at an example where it&amp;rsquo;s not used. We will create two classes, one called MarsExplorer and a second called MissionControl. We want to be able to control the mars robot in a more suitable, oxygen filled environment, which would be Mission Control.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt;   &lt;span class="nc"&gt;MarsExplorer&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;forward&lt;/span&gt;
       &lt;span class="vi"&gt;@speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
       &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vi"&gt;@speed&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; 

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;backwards&lt;/span&gt;
       &lt;span class="vi"&gt;@speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
       &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vi"&gt;@speed&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;
       &lt;span class="vi"&gt;@speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
       &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vi"&gt;@speed&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Our robot can move forwards, backwards, and it can stop. After each action we ask it to print out the speed, so we can keep track of when the methods are called.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MissionControl&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
        &lt;span class="vi"&gt;@robot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MarsExplorer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;forward&lt;/span&gt;
        &lt;span class="vi"&gt;@robot&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; 

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;backwards&lt;/span&gt;
        &lt;span class="vi"&gt;@robot&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;backwards&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;
        &lt;span class="vi"&gt;@robot&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stop&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Phew, that was a lot of typing! Too much, in my opinion. For each method in MarsExplorer, we have had to write the same method definition in MissionControl. If we ever change our robot class to have a turn method, or we change &amp;lsquo;stop&amp;rsquo; to &amp;lsquo;brake&amp;rsquo;, then we will have quite a few changes to make in our MissionControl class. In addition, MissionControl is now very cluttered with method definitions that aren&amp;rsquo;t directly related to the class. It would be better if we could simply let MissionControl respond to the methods of MarsExplorer, in a simple and unobtrusive way. We can achieve this by using Forwardable, as you can see in our updated MissionControl.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;forwardable&amp;#39;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MissionControl&lt;/span&gt;
    &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Forwardable&lt;/span&gt;

    &lt;span class="n"&gt;def_delegators&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="vi"&gt;@robot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:forward&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:backwards&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:stop&lt;/span&gt;
    &lt;span class="n"&gt;def_delegator&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="vi"&gt;@robot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:stop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:brake&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
        &lt;span class="vi"&gt;@robot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MarsExplorer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;      
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Notice that all of the method definitions are gone, with the exception of initialize, which hasn&amp;rsquo;t changed. Firstly, we have said that we want to make MissionControl extend Forwardable&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Forwardable&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This gives us access to the methods in Forwardable that follow, def_delegators and def_delegator. When we want to provide access to multiple methods, and we don&amp;rsquo;t mind calling them by the same name, we can use def_delegators. The syntax is explained below.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;def_delegators :theObject, :method1, :method2, :method3&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;def_delegators&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="vi"&gt;@robot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:forward&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:backwards&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:stop&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We are saying that MissionControl should respond to the methods forward, backwards, and stop, and it should respond by calling the method of the same name on the robot object. Instead of 3 method definitions, we only need one line stating which object to delegate to, and what methods it should allow delegation of. If we want to change the apparent name of the method, we can use def_delegator.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;  &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;def_delegator :@theObject, :methodName, :desiredMethodName&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;def_delegator&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="vi"&gt;@robot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:stop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:brake&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now we can call the method &amp;lsquo;brake&amp;rsquo; from MissionControl which will call the &amp;lsquo;stop&amp;rsquo; method on our MarsExplorer. Neat! Let&amp;rsquo;s take it for a spin.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;control&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MissionControl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;control&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt; &lt;span class="c1"&gt;# 1&lt;/span&gt;
&lt;span class="n"&gt;control&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;backwards&lt;/span&gt; &lt;span class="c1"&gt;# -1&lt;/span&gt;
&lt;span class="n"&gt;control&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stop&lt;/span&gt; &lt;span class="c1"&gt;# 0&lt;/span&gt;
&lt;span class="n"&gt;control&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;brake&lt;/span&gt; &lt;span class="c1"&gt;# 0&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now, not all of us are engineers at NASA, so where might you use Forwardable in a real life? It is useful anywhere you find yourself writing methods similar to the ones in the first MissionControl implementation, simple wrapping of another object&amp;rsquo;s methods. It can also be used as a form of adapter. By defining your own class that delegates to another object, and only calling the methods on this class, the delegate object can be set to anything. For example, if you are writing an application that will be using a database, but you&amp;rsquo;re not sure if it will be MySQL, MSSQL, or PostgreSQL, you can create a wrapper class that has def_delegator set for each method you want to be able to call. Your implementation will use this wrapper class for all database access, with the method calls being delegated to whichever object you have set. If the underlying database object has different method names, you only need to update your def_delegator definitions, and all of your implementation code will remain the same. Here&amp;rsquo;s a contrived example to demonstrate this.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyDatabase&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Forwardabble&lt;/span&gt;
  
  &lt;span class="n"&gt;def_delegator&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="vi"&gt;@db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:newMySQL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;
  
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
    &lt;span class="vi"&gt;@db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MySQL&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;	

&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MyDatabase&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Suppose that there exists a &amp;lsquo;MySQL&amp;rsquo; class, which provides the method &amp;lsquo;newMySQL&amp;rsquo; to create a new database. If we build our implementation on the MyDatabase class instead of the MySQL class, we will be able to switch to PostgreSQL in the future by simply changing the def_delegator&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;def_delegator&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="vi"&gt;@db&lt;/span&gt; &lt;span class="ss"&gt;:newPostgreSQL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Our implementation code remains untouched, and we have only made one small change. While this example is entirely fictional, it shows how you can create a level of abstraction with Forwardable.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/X2ArNK3cvGw" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-4-forwardable</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 3 Files</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/FeIUgFnLF2U/ruby-day-3-files" />
   <updated>2010-06-25T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-3-files</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The importance of &lt;a href='http://ruby-doc.org/core/classes/File.html'&gt;files&lt;/a&gt; goes without saying, so let&amp;rsquo;s get straight into it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Creating_a_File'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Creating_a_File"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;Creating a File&lt;/h2&gt;


&lt;p&gt;This command will first look for a file in the current directory called myFile, creating it if it can&amp;rsquo;t be found. The &amp;lsquo;w&amp;rsquo; tells ruby that we want to write to it. We can then write to the file by using the &amp;lsquo;write&amp;rsquo; method.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;myFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;myFile&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;my awesome story&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;When a file is opened the operating system records which program opened it. If we don&amp;rsquo;t tell the operating system that we have finished with the file, it will keep this information until the program exits. While this may not cause a problem in a simple example, it could lead to errors with a large number of files, or multiple programs accessing the same file. Calling &amp;lsquo;close&amp;rsquo; on the file will take care of this. Whenever you open a file, you must close it when you are finished.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;You can probably guess how to read from the file&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;myFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;myFile&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;
&lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="c1"&gt;# my awesome story&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Identical to the first example, but instead of a &amp;lsquo;w&amp;rsquo; for writing to the file, we use an &amp;lsquo;r&amp;rsquo; for reading from the file. The letter &amp;lsquo;a&amp;rsquo; can be used if we want to open a file, and append content to the end of it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Reading_From_a_File'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Reading_From_a_File"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;Reading From a File&lt;/h2&gt;


&lt;p&gt;For those of you coming from a PHP background, this code below should look familiar&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$fp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;myFile&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;eof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$fp&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="n"&gt;fread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$fp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$fp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;which simply opens a file and reads each line of it until it reaches the end. This is rather ugly, to be honest. We have to create a file pointer, check that we haven&amp;rsquo;t reached the end, read from it, and then close it. That&amp;rsquo;s 4 Steps, but we can do it in just 1 line in Ruby.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;myFile&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;By placing a block at the end, we can catch the opened file, and loop over each line in the file. When the block ends, Ruby will automatically close the file for us. The block method of file handling is preferred as you won&amp;rsquo;t run the risk of forgetting to close a file.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Peculiarities'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Peculiarities"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Peculiarities&lt;/h3&gt;


&lt;p&gt;If you try writing a new line to a file with the typical \n character, you may find that it does not work.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;test&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;line 1 \n line 2&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;test&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="c1"&gt;# line 1 \n line 2&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The \n character is stored as a \ character followed by the letter n. Not quite what we wanted. So how can you create a new line in your file? If we use &amp;lsquo;puts&amp;rsquo; instead of &amp;lsquo;write&amp;rsquo;, then Ruby will automatically place a real newline character at the end, once for each time we call puts.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;test&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;line 1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;line 2&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;test&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;line 1
line 2
The file clearly has &amp;lsquo;line 1&amp;rsquo; and &amp;lsquo;line 2&amp;rsquo; on separate lines so we have succeeded.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-File_Methods'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-File_Methods"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;File Methods&lt;/h2&gt;


&lt;p&gt;If you are familiar with UNIX, bash in particular, then you will feel right at home with the File class. It offers just about every bash file operation, calling them by the same names and accepting the same arguments. For example, you can use &amp;lsquo;chmod&amp;rsquo; and &amp;lsquo;chown&amp;rsquo; to change permissions, or set who owns the file. Copy, move, delete, rename, stat, and more, are all available. Have a look at this &lt;a href='http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture2.html'&gt;UNIX tutorial&lt;/a&gt; if you want to learn about file operations.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-End'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-End"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;End&lt;/h2&gt;


&lt;p&gt;Hopefully you now have some familiarity with the way Ruby handles files.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;
&lt;em&gt;I clearly was not familiar enough with blocks at the time of writing this post, as David pointed out in the comments. I had an incorrect statement stating that a file handler passed into a block can only be used once. The post has been updated to reflect this.&lt;/em&gt;&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/FeIUgFnLF2U" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-3-files</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 2 Array Enumerable</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/6w7skhyGojo/ruby-day-2-array-enumerable" />
   <updated>2010-06-24T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-2-array-enumerable</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Without the array, programming would be terribly difficult. To store 10 items, you would need 10 different variable names, and any more than this makes things hopelessly inefficient. The array lets us store a number of items under a single variable name. We can reference a specific item simply by saying which variable it&amp;rsquo;s stored in, and in what position it lies. Like in C and PHP, Ruby starts indexing at 0. myArray[0] will give you the first element. In C, if you tried to access myArray[-1] you would meet a nasty runtime error. In Ruby, however, a negative index is treated as an index relative to the end of the array. Thus -1 becomes the last element, -2 the second to last, and so on.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;a href='http://ruby-doc.org/core/classes/Array.html'&gt;Ruby&amp;rsquo;s Array class&lt;/a&gt; has a lot of methods, so let&amp;rsquo;s get right in to it and explore some of what it has to offer.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Creation'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Creation"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Creation&lt;/h3&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# 1 2 3&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s all it takes! The square brackets let Ruby know that we want to create a new array, with the elements set to be the contents of the brackets. This syntax is an alias of the actual method, [].&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;[&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 1 2 3&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If we want to create an array, but we don&amp;rsquo;t have anything to store in it yet, we can use the &amp;lsquo;new&amp;rsquo; method.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If we know that we want to store n items, we can create the array with a capacity.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;# 10&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Size is a method of array which returns the number of elements. We created an empty array with space for 10 elements, and that is the size of the array.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Generation'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Generation"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Generation&lt;/h3&gt;


&lt;p&gt;The Array class gives us a powerful way of generating elements using a block.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="c1"&gt;# 0 2 4 6 8&lt;/span&gt;
    &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We have created an array called &amp;lsquo;even&amp;rsquo; that contains the first 5 even numbers. Firstly, we stated that we wanted a new array of size 5&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Secondly, we put a block at the end of it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This could also be written as&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;What is this voodoo magic, do I hear you ask? Well, the block is taking a number, i, and returning the sum of i with itself. The value of i that is passed is the index into each element created. Thus for an array of size 5, we will have the indices 0, 1, 2, 3, and 4. Working this out the long way gives us&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Using a block lets us define each element programatically, based on the the index of that element. Nifty!&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Interesting_Methods'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Interesting_Methods"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Interesting Methods&lt;/h3&gt;


&lt;p&gt;There are a lot of methods in the API, and most of them have some sample code to go with them. Therefore, I will look at some of the more interesting ones.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-*'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-*"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;*&lt;/h4&gt;


&lt;p&gt;While this method can be used to duplicate an array n times, like so&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;echo&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# echo echo echo&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;the more interesting usage is as a join operator. PHP offers the join() method which, given an array and a character will return a string consisting of each array element, followed by the given character. Ruby also has a join method, but the * operator is a great shortcut.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;s&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;v&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span class="c1"&gt;# c,s,v&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-abbrev'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-abbrev"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;abbrev&lt;/h4&gt;


&lt;p&gt;The API states that this method &amp;lsquo;calculates the set of unambiguous abbreviations for the strings in self&amp;rsquo;. The example given, while sensible, doesn&amp;rsquo;t relate to a particularly useful application.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;abbrev&amp;#39;&lt;/span&gt;
&lt;span class="sx"&gt;%w{ car cone }&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;abbrev&lt;/span&gt;   
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;ca&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;car&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;car&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;car&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;co&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;cone&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;con&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;cone&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;cone&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;cone&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Note that this method isn&amp;rsquo;t part of the core API, and so it needs to be explicitly included&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-19'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-19"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So why is this useful? The main application I can see for this is auto-completion. Given a set of names or commands, what are the fewest characters I can type to identify one in particular? Consider a lazy person using twitter. If I want to send a message someone, I don&amp;rsquo;t really want to type out their whole user id. Given an array of people I follow, abbrev can generate a list of possible abbreviations such that each abbreviation will only reference one person.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;tweeps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;jeff&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;steve&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;frank&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;james&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;stephen&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;tweeps&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;abbrev&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;frank&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;frank&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;stephe&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;stephen&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;jam&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;james&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;jef&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;jeff&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;steve&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;steve&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;step&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;stephen&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;ja&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;james&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;fr&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;frank&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;je&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;jeff&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;jame&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;james&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;frank&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;stephen&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;stephen&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;jeff&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;jeff&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;fran&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;frank&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;stev&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;steve&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;james&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;james&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;steph&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;stephen&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;&amp;quot;fra&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;frank&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-20'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-20"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Steve and Stephen have the same first 3 characters, and so you can see that the shortest abbreviation for each is stev and step. You may have noticed that I used &amp;lsquo;p&amp;rsquo; and not &amp;lsquo;puts&amp;rsquo;. This is because abbrev doesn&amp;rsquo;t return an array, but a hash of &amp;lsquo;abbreviation&amp;rsquo; =&gt; &amp;lsquo;originalString&amp;rsquo; pairs.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-collect'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-collect"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;collect&lt;/h4&gt;


&lt;p&gt;This method is similar to the element generation described above. Given an array, place a block at the end which takes each element of the array. In this case, instead of the index being passed in, the actual element is passed in. The resulting value can either be stored back in the array, or placed into a new array.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compact&lt;/span&gt;&lt;span class="c1"&gt;# [2, 4, 6, 8]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-22'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-22"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;By checking if each number is divisible by 2, we can create a new array containing only the even numbers. &amp;lsquo;compact&amp;rsquo; is used to remove elements that are &amp;lsquo;nil&amp;rsquo;. As collect will create a new array of the same size, when the number is odd no value will be returned and so that element will be empty. To overwrite the array instead of creating a new one, use the exclamation mark.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collect!&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-uniq'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-uniq"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;uniq&lt;/h4&gt;


&lt;p&gt;By using uniq you can quickly make your array behave like a set.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;uniq&lt;/span&gt;&lt;span class="c1"&gt;# [1, 2, 3, 4]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Enumerable'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Enumerable"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;Enumerable&lt;/h2&gt;


&lt;p&gt;Now that we have a basic understanding of Array, let&amp;rsquo;s take a look at it&amp;rsquo;s cousin class: Enumerable. Enumerable is all about iterating. The collect method that we have just seen is an example of an Enumerable function. Collect lets you perform some operation on each element of an array. Sorting is another example of what you can do with Enumerable. The key difference between Array and Enumerable however, is that while you may create an array, you don&amp;rsquo;t create an enumerable. Rather, by defining certain methods, a class can behave like an enumerable object. Before we look at how to do that, let&amp;rsquo;s see some of the methods available.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-inject'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-inject"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;inject&lt;/h4&gt;


&lt;p&gt;The inject method is similar to collect, but an extra value is sent to the block at each iteration containing the result of the previous iteration. This lets us compute the factorial of a number in a neat way:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="c1"&gt;# 10&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-sort'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-sort"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;sort&lt;/h4&gt;


&lt;p&gt;When we have an array of elements, we quite often want to sort them. It they are names, we&amp;rsquo;d like to sort them alphabetically; if they are numbers, well, numerically would make sense. The sort method gives us great flexibility when it comes to sorting by letting us define the sorting comparator as a block.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="sx"&gt;%w{a c b}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="c1"&gt;# [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-27'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-27"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;%w{a c b} is a short way of saying [&amp;lsquo;a&amp;rsquo;, &amp;lsquo;c&amp;rsquo;, &amp;lsquo;b&amp;rsquo;]. If each element of your array is a single word, then the shorthand will be quicker as you don&amp;rsquo;t have to worry about typing in quotes. So, given our array, [&amp;lsquo;a&amp;rsquo;, &amp;lsquo;c&amp;rsquo;, &amp;lsquo;b&amp;rsquo;] and our block, { |a,b| a &amp;lt; =&gt; b }, we see that they have become alphabetically sorted. The block operates on a pair of values, and compares them with the &amp;lt; =&gt; operator. &amp;lt; =&gt; returns -1 if the left value is lesser, 0 if they are equal, and +1 if the left value is greater. If we want to sort the array in reverse order, we simply swap the order of the values in our block.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="sx"&gt;%w{a c b}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="c1"&gt;# [&amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Custom_Enumerable_Classes'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Custom_Enumerable_Classes"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Custom Enumerable Classes&lt;/h3&gt;


&lt;p&gt;If you have your own class, you may want to do things with it as if it were an array. For example, let&amp;rsquo;s say we have a BookShelf class that maintains records of various books we own. One thing we may like to do is print out our books sorted by their author. If we include Enumerable into our BookShelf class, and define the method &amp;lsquo;each&amp;rsquo;, we will be able to write code that might look like the following.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;bookShelf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;author&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;author&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Great Expectations&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;author&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Dickens, C.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Linchpin&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;author&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Godin, S.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Frankenstien&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;author&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Shelly, M.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-29'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-29"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;As you can see, we have an array of hashes, containing an author and a title. They are ordered by the author&amp;rsquo;s name. Here&amp;rsquo;s the BookShelf class that lets us do this.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookShelf&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Enumerable&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
        &lt;span class="vi"&gt;@titles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;
        &lt;span class="vi"&gt;@authors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="n"&gt;bookTitle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bookAuthor&lt;/span&gt;
        &lt;span class="vi"&gt;@titles&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;bookTitle&lt;/span&gt;
        &lt;span class="vi"&gt;@authors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;bookAuthor&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;each&lt;/span&gt;
       &lt;span class="vi"&gt;@authors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each_with_index&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;author&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;title&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="vi"&gt;@titles&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;end&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;    
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-30'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-30"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We are including the Enumerable class, which means that if we define the method, &amp;lsquo;each&amp;rsquo;, we can call any Enumerable method on the BookShelf class.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-31'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-31"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The each method is defined as&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vi"&gt;@authors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each_with_index&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;author&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;title&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="vi"&gt;@titles&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-32'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-32"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In English, this says &amp;lsquo;For each author, get me the name of the author and the index. Then I&amp;rsquo;m going to stop (yield) and return the name of the author, and the title of the book that appears in the same position in the titles array&amp;rsquo;. As you can see, it is &amp;lsquo;yield&amp;rsquo; that is doing the magic here. It essentially stops the loop (each_with_index) and spits out the result of the statements that follow it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-33'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-33"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If you want to try it out yourself, you could add some books to the BookShelf like so&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;bookShelf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BookShelf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;bookShelf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Great Expectations&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Dickens, C.&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;bookShelf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Frankenstien&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Shelly, M.&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;bookShelf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Linchpin&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Godin, S.&amp;#39;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-End'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-End"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;End&lt;/h2&gt;


&lt;p&gt;Phew! That was a lot to take in. Hopefully you&amp;rsquo;ve seen some of the neat methods that the Array class provides, particularly &amp;lsquo;abbrev&amp;rsquo;, as well as how we can make our own classes behave like Enumerable objects. Let me know if you have any questions, comments, and especially criticisms.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/6w7skhyGojo" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-2-array-enumerable</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Day 1 Date</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/eHR92AWKTv8/ruby-day-1-date" />
   <updated>2010-06-23T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/ruby-day-1-date</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Welcome to the first instalment of 0 to Ruby in 30 days. Today will cover a simple, but useful class: Date.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Most web applications that let users contribute content will likely require the use of date and time functionality. Just consider how useful would a website be that helps people coordinate meetings if there was no way of specifying the time? Also consider how difficult it would be to coordinate a global event, with people in different time zones, if everyone saw the time in GMT +0. Confusion (and possibly hilarity) would undoubtably ensue. Clearly, we need a way to handle dates and times.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Slightly strange motivation aside, let&amp;rsquo;s get started. The format of this tutorial is as follows: I have opened the &lt;a href="http://ruby-doc.org/core/classes/Date.html"&gt;API reference for the Date class&lt;/a&gt; and will be going through it, testing methods of interest. I have not used the date class before and so at each step, I am guessing the usage based on my limited experience of Ruby gained from &lt;a href="http://mislav.uniqpath.com/poignant-guide/book/chapter-1.html"&gt;_Why&amp;rsquo;s (Poignant) Guide&lt;/a&gt;. You can consider this tutorial, then, to be an extra reference to the API page, with examples of usage and results of various important methods.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;One of the major differences between the Ruby date library and the PHP date library is the epoch used. PHP uses the date &amp;lsquo;January 1 1970 00:00:00 GMT&amp;rsquo;, the beginning of UNIX, as a reference point. Time is defined as the number of seconds after or before this time. Ruby uses January 1, 4713 BC as its reference point. This is known as the Julian Date, with any date defined by the number of days before or after this reference point. But this is a programming tutorial, not a history lesson, so let&amp;rsquo;s see some code.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Great! We can create a new date object by providing the year, month and day. (Date.new and Date::new seems to function identically). By printing it to the console with puts, we can see that, indeed, this Date object contains the date we specified.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2010&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;                      &lt;span class="c1"&gt;# 2010-06-22&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Lets get specific parts of the date&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;month&lt;/span&gt;    &lt;span class="c1"&gt;# 6&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;      &lt;span class="c1"&gt;# 22&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;     &lt;span class="c1"&gt;# 2010&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;cwday&lt;/strong&gt;
The method &amp;lsquo;cwday&amp;rsquo; gives the Commercial day of the week
Today is Tuesday, the 2nd day of the working week. Sunday is 7, the last day of the week.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cwday&lt;/span&gt;    &lt;span class="c1"&gt;# 2&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;What happens when we create an invalid date?&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mo"&gt;0325&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# ArgumentError: invalid date&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We have a basic understanding, so let&amp;rsquo;s start looking at some of the class methods&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;day_fraction_to_time&lt;/strong&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day_fraction_to_time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 18003.72423446248771e-17&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This is equal to 18 hours, 00 minutes, 03 seconds, 0.72.. milliseconds&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;time_to_day_fraction&lt;/strong&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time_to_day_fraction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mo"&gt;00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mo"&gt;03&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# 21601/28800&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;21601/28800 turns out to be 0.75, the fraction we passed in to day_fraction_to_time.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;today&lt;/strong&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;today&lt;/span&gt; &lt;span class="c1"&gt;# 2010-06-22&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;valid_time&lt;/strong&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valid_time?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# nil&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;25 hours is not a valid time, hence the result is nil.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;&amp;ndash;&lt;/strong&gt;
The &amp;ndash; operator should return a date that is n days earlier than the current one&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;today&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;  &lt;span class="c1"&gt;# 2010-03-19&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;+ adds days in the same way&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-19'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-19"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;&amp;lt;&amp;lt;&lt;/strong&gt;
The &amp;lt;&amp;lt; operator should return a date that is n months earlier than the current one&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;today&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;    &lt;span class="c1"&gt;# 2010-03-22&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-20'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-20"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&amp;gt;&amp;gt; adds months in the same way&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-21'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-21"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;leap?&lt;/strong&gt;
Is this a leap year?&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;today&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;leap?&lt;/span&gt;    &lt;span class="c1"&gt;# false&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-22'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-22"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;go back 2 years&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;    
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;leap?&lt;/span&gt;    &lt;span class="c1"&gt;# true&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-23'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-23"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;next&lt;/strong&gt;
The next day.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;today&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;     &lt;span class="c1"&gt;# 2010-06-23&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-24'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-24"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So, that&amp;rsquo;s the Date class. Pretty nifty, and very intuitive. But what good is the date without the time? Enter: DateTime&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;  &lt;span class="c1"&gt;# -4712-01-01T00:00:00+00:00&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-25'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-25"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Looks like that wasn&amp;rsquo;t sufficient, but note the term &amp;lsquo;-4712&amp;rsquo;. This was the year Ruby set to be the reference time.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;  &lt;span class="c1"&gt;# 2010-06-22T13:56:13+12:00&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-26'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-26"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;There we go
2010-06-22 is the date
T demarcates the time
13:56:13 is the current time
+12:00 is the time zone. I live in New Zealand which is GMT +12&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-27'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-27"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Let&amp;rsquo;s just check that the timezone is correct&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zone&lt;/span&gt; &lt;span class="c1"&gt;# +12:00&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-28'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-28"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now we&amp;rsquo;ve seen some of the various methods, lets put it to use in a practical example. If you&amp;rsquo;re developing a website, chances are you&amp;rsquo;ll be dealing with people from all over the world. This means you&amp;rsquo;ll be dealing with different time zones, and so every time you work with a date and time you must ensure you have the timezone correct. Let&amp;rsquo;s imagine you&amp;rsquo;re developing &amp;lsquo;Qwitter&amp;rsquo;, a social networking service designed for people who are trying to ween themselves off Twitter. When someone updates their status, their followers want to see the time in their own timezone, so lets define a function to convert a time from a certain timezone, into another&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;time_relative_to_timezone&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oldTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newTimeZone&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;oldTimeZone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;    
    &lt;span class="n"&gt;oldTimeZone&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;

    &lt;span class="n"&gt;timeZoneDifference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;oldTimeZone&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;newTimeZone&lt;/span&gt;

    &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeZoneDifference&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-29'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-29"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We can call it like so:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;time_relative_to_timezone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-30'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-30"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;So, what&amp;rsquo;s going on there? Firstly, we are taking the given time and finding it&amp;rsquo;s timezone. The &amp;lsquo;offset&amp;rsquo; method returns the timezone as a fraction of 24 hours. 0.5 would be 12 out of 24, for a timezone difference of +12. Thus, if we multiply this fraction by 24, we get the timezone offset in hours.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-31'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-31"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We then find the difference between this timezone, and the supplied one. We will assume for arguments sake (no pun intended) that the supplied timezone is already an offset given in hours. By subtracting them, we find how different they are.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-32'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-32"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Lastly, recall that when used with a DateTime object, the &amp;ndash; operator subtracts a number of days. If we subtract our time zone difference as a fraction of 24 hours (1 day), we will end up with a new DateTime object with a time that has been adjusted.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-33'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-33"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Obviously we would need to include error handling for invalid arguments, but this has been ocluded for clarity.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-34'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-34"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;strong&gt;End&lt;/strong&gt;
Hopefully this tutorial has shed some light on the Ruby Date, and DateTime classes. If you have any questions, comments, criticisms, praise, please leave a comment or send me an email.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/eHR92AWKTv8" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/ruby-day-1-date</feedburner:origLink></entry>
 
 <entry>
   <title>0 To Ruby In 30 Days</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/_O-cu1RgjQU/0-to-ruby-in-30-days" />
   <updated>2010-06-22T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/0-to-ruby-in-30-days</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In an effort to become competent with the Ruby on Rails framework, I&amp;rsquo;ve decided to write up 1 tutorial per day for the next 30 days. Coming from a background in PHP, I will also try to add commentary on similarities/diffe:rences between the two languages. While not all of the topics I intend to cover are relevant to the Rails framework, and web development in general, the experience gained from learning them will hopefully be beneficial by increasing familiarity with the language and its paradigms. I hope that these tutorials become useful to anyone else who is wanting to learn Ruby/Ruby on Rails.&lt;/p&gt;

&lt;p&gt;The topics I will cover are:&lt;/p&gt;

&lt;p&gt;  Array
  Enumerable&lt;/p&gt;

&lt;p&gt;  IO&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pipe
tty
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;  Thread&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Mutex
Process
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;  Marshall
  Logger
  Observable
  Queue
  Singleton
  Zlib
  Test
  URI
  Yaml
  ERB
  Rails&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ActiveRecord
    Migrations
    Validations
    Associations
ActionMailer
ActionView
    Layouts
    View Helpers
ActionController
    Routing &amp;lt;/pre&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Some topics, such as Array and Enumerable, may be combined into a single tutorial as they are quite similar.)&lt;/p&gt;

&lt;p&gt;These tutorials are designed for someone with reasonable programming experience. If you&amp;rsquo;ve been using a language such as PHP for web development, but you feel like something is missing, then this is for you. Before starting the tutorials here, I strongly suggest you sit down in a comfortable chair, make a nice drink, and read through . It is unlike any programming guide you have ever read, and probably will ever read.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/_O-cu1RgjQU" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/0-to-ruby-in-30-days</feedburner:origLink></entry>
 
 <entry>
   <title>Subversion For Code Management</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/4CQmCRXZk2I/subversion-for-code-management" />
   <updated>2010-06-18T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/subversion-for-code-management</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Gone are the days of a handful of simple html pages to make up a website. Projects can consist of many, many files and, especially with multiple people involved, this can become difficult to manage. From graphics to documentation to source code, a version control system (VCS) is an easy way to manage your project and ensure that nothing important is ever lost. This tutorial will take you through the basics of version control, setting up Subversion server on your PC, and lastly we&amp;rsquo;ll look at how you keep track of your files.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-What_is_a_Version_Control_System_(VCS)?'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-What_is_a_Version_Control_System_(VCS)?"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;What is a Version Control System (VCS)?&lt;/h2&gt;

&lt;p&gt;If you&amp;rsquo;ve ever asked yourself the question &amp;ldquo;how can I get back this change that I&amp;rsquo;ve overwritten&amp;rdquo;, &amp;ldquo;this program used to work better before we added that feature&amp;rdquo; or, &amp;ldquo;can you send me the latest copy of your code before I make changes&amp;rdquo;, then a VCS is your answer to them.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A VCS consists of a central repository which stores all of your project files. It keeps track of the files that are added and will store of all the changes that are made to them. This means that when you update a file, any previously saved work can be recovered by undoing particular updates. Multiple people can save to this same repository and so the VCS also takes care (with some user help, as we shall see) of managing files to ensure that no one can overwrite anyone elses work. Essentially, a VCS is turbo-charged undo/redo history for your computer.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Before we get into setting up a VCS for ourselves, let&amp;rsquo;s take a look at a common problem with VCS and how it&amp;rsquo;s solved. Called the &amp;ldquo;Lost Update&amp;rdquo; problem, it occurs when 2 people are editing the same file at the same time and both want to save their changes. The image below is from the Subversion Book, which is the user guide for a common VCS.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Obviously we don&amp;rsquo;t want something like this happening and fortunately, VCS provide us with 2 methods of ensuring that it doesn&amp;rsquo;t. The first method is called &amp;ldquo;Locking&amp;rdquo; (also known as &amp;ldquo;Reserved Checkouts&amp;rdquo;) and involves &amp;lsquo;locking&amp;rsquo; a file before editing it. &amp;lsquo;Locking&amp;rsquo; the file means that no one else is able to edit the file until it is released again. This ensures that changes cannot be overwritten as there is only ever one person editing the file at a time. While this can cause problems, it offers complete assurance that work will not be overwritten.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The second method is called &amp;ldquo;Merging&amp;rdquo; (also known as &amp;ldquo;Unreserved Checkouts&amp;rdquo;) and involves merging copies of work when the same file has been edited. If the parts of the file that were edited are in different locations then the two files can automatically be merged by the system. If &amp;lsquo;overlap&amp;rsquo; between edited parts occurs however then it is up to the user to manually combine the files. The code examples below illustrates two files that are to be merged both with, and without overlap.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Edit_without_overlap'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Edit_without_overlap"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Edit without overlap&lt;/h3&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Edit_1:'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Edit_1:"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;Edit 1:&lt;/h4&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$edit1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;The cat sat&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;rest of the code&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Edit_2:'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Edit_2:"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;Edit 2:&lt;/h4&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;rest of the code&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$edit2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;on the mat&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;As both edits are in different areas of the code, they can be merged with no conflicts.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$edit1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;The cat sat&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;rest of the code&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$edit2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;on the mat&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Edit_with_overlap'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Edit_with_overlap"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h3&gt;Edit with overlap&lt;/h3&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Edit_1:'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Edit_1:"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;Edit 1:&lt;/h4&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$edit1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;The cat sat&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Edit_2:'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Edit_2:"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h4&gt;Edit 2:&lt;/h4&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$edit1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="s2"&gt;&amp;quot;on the mat&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In this case, the same line has been changed in both edits and so they cannot be automatically merged. Differencing tools can be used to find out what exactly is different between the files and so the desired parts of each can be chosen.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now that you have a basic understanding of what a VCS is and how they can help, let&amp;rsquo;s look at setting one up and using it&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Installing_Subversion'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Installing_Subversion"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;Installing Subversion&lt;/h2&gt;

&lt;p&gt;There are 2 big version control systems (VCS) in use today, Concurrent Versions System (CVS) and Subversion (SVN). We&amp;rsquo;ll be using SVN because CVS seems to be going out of fashion.
SVN itself does not have a graphical user interface and so to make things more familiar, we&amp;rsquo;ll be using a program called TortoiseSVN to interact with it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-20'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-20"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If you have Apache 2.0 or greater installed then you can grab this release, &lt;a href="http://subversion.tigris.org/files/documents/15/44049/Setup-Subversion-1.5.3.msi"&gt;here&lt;/a&gt;
If you don&amp;rsquo;t have Apache then you can get a standalone version from Slik SVN, &lt;a href="http://www.sliksvn.com/pub/Slik-Subversion-1.5.4-win32.msi"&gt;here&lt;/a&gt;. This is the version that we&amp;rsquo;ll be using in this tutorial.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-21'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-21"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Also download TortoiseSVN from &lt;a href="http://downloads.sourceforge.net/tortoisesvn/TortoiseSVN-1.5.5.14361-win32-svn-1.5.4.msi?download"&gt;here&lt;/a&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-22'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-22"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Once your chosen version of SVN and TortoiseSVN have been downloaded, double click to start installing. Choose the typical install for both programs.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-23'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-23"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s all there is to it! You now have a working VCS on your system and a great graphical interface to go with it. There is no program to start up to use SVN as TortoiseSVN integrates straight into the context (right-click) menu in windows explorer. Now let&amp;rsquo;s get right into using it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-Using_SVN'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-Using_SVN"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;h2&gt;Using SVN&lt;/h2&gt;

&lt;p&gt;First of all we need to create a repository. This is where all of the saved project files and version information is kept. Create a new folder on your system and call it &amp;lsquo;MyProject&amp;rsquo;. Right click on this folder and click TortoiseSVN &amp;ndash;&gt; Create repository here.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-25'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-25"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now we need to do what is called &amp;ldquo;checking out&amp;rdquo; the repository. This will create a working directory for us to add files and make changes. Think of this as downloding files from your FTP server in order to work on them locally.
Right click on your desktop and click TortoiseSVN &amp;ndash;&gt; Checkout repository. A dialog box will open asking for the location of your repository. Enter in the path to where you created this. You can also specify what you would like to call your checked out folder. Click OK when you&amp;rsquo;re done. You should now see a newly created folder with a green tick on it. This green tick is a TortoiseSVN icon that means the contents of your folder have the latest versions from the repository.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-26'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-26"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;You may be wondering why we have to create our repository in one place, and check it out in another. The repository is not meant to be edited manually. It contains a number of files and folders which SVN uses to keep track of the changes. You could store this repository anywhere (including on another computer) and check it out in the same way.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-27'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-27"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now let&amp;rsquo;s create a file. We&amp;rsquo;ll deal with a very basic example to demonstrate the key actions so create a text file called numbers.txt in the checked out folder, and paste in the following text&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-28'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-28"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now that we&amp;rsquo;ve made some changes, we need to let SVN know about it. Right click in the folder and click on SVN Commit.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-29'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-29"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A dialog box will open asking you for a message to save along with the file. It is good practice to always comment on each commit you make. This will allow you to easily see what changes you&amp;rsquo;ve made and is especially helpful when you have multiple people working from the same repository. Enter in a quick sentance about what the file is (TortoiseSVN will give you hints for filenames as you type), tick the check box next to the file and then click OK.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-30'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-30"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Your file has now been saved to the repository and a dialog box will open confirming this. Note that it says &amp;ldquo;Completed at revision 1&amp;rdquo;, each time you commit the revision number will update.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-31'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-31"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now we&amp;rsquo;re able to keep track of our own changes, but what if we want to bring someone else into the project?. Right click on the desktop and checkout the repository again giving it the name &amp;lsquo;DifferentUser&amp;rsquo;. Notice how the icon is not a green tick this time but a red exclamation mark. This means that our folder is not in sync with the repository as we have added a file from the first checkout. Go into this new folder, right click and click SVN Update.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-32'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-32"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;You should now see the numbers.txt file in this folder. Let&amp;rsquo;s make a change to this file. Open it up and replace the number 5 with the number 6. Let&amp;rsquo;s also change the file in the first checkout we created, MyProject. Open the file and add the number 6 after the number 5 on a new line. We&amp;rsquo;ve now created a situation where 2 users have made different changes to the same file at the same time. Commit the changes in MyProject and the folder icon should have a green tick. Now go into the second folder and try to commit. You will get a dialog box pop up informing you that there is a conflict.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-33'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-33"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We can now use TortoiseSVN to help us resolve this conflict. Right click in the folder and click TortoiseSVN &amp;ndash;&gt; Resolve conflicts. This brings up the TortoiseMerge window with which we can do this. The file on the left labelled &amp;lsquo;Theirs&amp;rsquo; is what is in the repository. This should contain the file that we committed from MyProject. The file on the right labelled &amp;lsquo;Mine&amp;rsquo; is what we&amp;rsquo;re trying to commit. The area below labeled &amp;lsquo;Merged&amp;rsquo; is what will be committed once we&amp;rsquo;ve made our changes. There are various options such as appending the files together, taking just one file or manually edit changes. We will manually edit the file in the &amp;lsquo;Merged&amp;rsquo; window and take this to be our new version.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-34'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-34"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;In the &amp;lsquo;Merged&amp;rsquo; section, edit it so that it looks like the code below&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-35'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-35"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now we can save our changes.
We have successfully resolved the conflict and both folders should have green ticks.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-36'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-36"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;While this example was rather trivial, you should now have a grasp on what version control is and how you can use SVN to help manage your projects.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/4CQmCRXZk2I" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/subversion-for-code-management</feedburner:origLink></entry>
 
 <entry>
   <title>Juicy Fresh Marketing</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/13lfM95vyMw/juicy-fresh-marketing" />
   <updated>2010-06-16T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/juicy-fresh-marketing</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2010/06/Screen-shot-2010-06-16-at-10.04.14-PM.png" alt="Attractive marketing, literally." /&gt;&lt;/p&gt;

&lt;p&gt;It occurred to me today why &lt;a href="http://www.jucy.co.nz/"&gt;Jucy Car Rentals&lt;/a&gt; works. They have a brand that&amp;rsquo;s cool, saucy, new, that people will want to show off. No one cares if you hired your car from Avis, or Budget, or another boring company. But if it&amp;rsquo;s jucy, someone&amp;rsquo;s going to ask about it. (If you haven&amp;rsquo;t seen, they put suggestively posing women on the side of their campervans.)&lt;/p&gt;

&lt;p&gt;This means that they can put advertising on the cars they rent out. It lets the renter tell a story, so why would they mind?&lt;/p&gt;

&lt;p&gt;They can spend less on traditional (read: boring) advertising because they&amp;rsquo;re not renting cars, they&amp;rsquo;re renting mobile billboards, and so they can charge you less. Win-win.&lt;/p&gt;

&lt;p&gt;Just like you can&amp;rsquo;t out Amazon Amazon, you can&amp;rsquo;t out Avis Avis. But if you&amp;rsquo;re a backpacker, both the price point and the image is appealing, and so the choice is obvious.&lt;/p&gt;

&lt;p&gt;They&amp;rsquo;ve found their &lt;a href="http://sethgodin.typepad.com/seths_blog/2007/01/how_to_be_remar.html"&gt;remarkable&lt;/a&gt;.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/13lfM95vyMw" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/juicy-fresh-marketing</feedburner:origLink></entry>
 
 <entry>
   <title>Linchpin Meetup Day</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/3BLaizaNUE0/linchpin-meetup-day" />
   <updated>2010-06-15T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/linchpin-meetup-day</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;img src="/uploads/2010/06/CD-Audiobook-Cover.jpeg" alt="Linchpin, by Seth Godin" /&gt;&lt;/p&gt;

&lt;p&gt;June 14th was (and still is, for most of the world at the time of writing) World Linchpin Day.&lt;/p&gt;

&lt;p&gt;This was an event started by Seth Godin on &lt;a href="http://www.meetup.com/Linchpins-are-everywhere-raise-the-flag/"&gt;meetup.com&lt;/a&gt; for people who are interested in his latest book, &lt;a href="http://www.amazon.com/Linchpin-Are-Indispensable-Seth-Godin/dp/1591843162/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1276522638&amp;amp;sr=8-1"&gt;Linchpin&lt;/a&gt;. It&amp;rsquo;s a book that asks the question &amp;ldquo;are you indispensable?&amp;rdquo;. It challenges you to think about what you do each day, and see how it adds value or meaning to your life. Manual labour can be difficult, but &amp;lsquo;emotional labour&amp;rsquo; is even harder. Godin talks about how it is essential to put emotional labour into the work we do, to create art and give it away, and to give with no expectation of reciprocation.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://www.meetup.com/Linchpins-are-everywhere-raise-the-flag/1844/"&gt;meetup page for Auckland&lt;/a&gt;, New Zealand, showed 57 people who were interested. As we could link our Facebook accounts, people were able to read about (stalk) each other before hand. This meant that when I arrived, not only did I not have to give my name, but even my job and background were common knowledge! This really shows the power of social networking, and also how careful you have to be with what you share. I digress, however.&lt;/p&gt;

&lt;p&gt;I had come with expectations about discussing the book itself, and ideas related to it, but it turns out the many people hadn&amp;rsquo;t read it and were there for the networking, meeting people. That turned out to be pretty amazing. All of the people there were operating on a similar page. Entrepreneurship, social ventures, making connections, all things to do with people. This made for lot of interesting and stimulating conversation, sharing our ideas and backgrounds, and getting to know people.&lt;/p&gt;

&lt;p&gt;After the swapping of many business cards, it was eventually time to leave however. Fortunately, being a meetup for Linchpins, the initiative had been taken to put together some resources to keep and grow the connections made. Thanks Alan!&lt;/p&gt;

&lt;p&gt;Being a global event, there were many meetups happening simultaneously. One was a meetup in Japan that was running on &lt;a href="http://www.junto.cc"&gt;Junto&lt;/a&gt;. It was great to jump in there and meet some great people, and share my experience from the meetup here.&lt;/p&gt;

&lt;p&gt;There are many Linchpins in the world, and many who are but just don&amp;rsquo;t know it yet. I think, if anything, this event has highlighted just how prolific the desire to do meaningful work is. It also shows that sometimes all people need is a word to describe something, and then everything moves forward from there.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/3BLaizaNUE0" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/linchpin-meetup-day</feedburner:origLink></entry>
 
 <entry>
   <title>Getting Things Done The Linchpin Way</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/oCWI88rSXVs/getting-things-done-the-linchpin-way" />
   <updated>2010-06-11T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/getting-things-done-the-linchpin-way</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;&lt;a href="/uploads/2010/06/linchpin.jpeg"&gt;The old kind of linchpin&lt;/a&gt;
I wrote previously about how competitions are a &lt;a href="http://www.oneofthesedaysblog.com/getting-things-done-the-microsoft-way"&gt;great way of getting things done&lt;/a&gt;. There&amp;rsquo;s a new method that&amp;rsquo;s come up recently, and it&amp;rsquo;s from Seth Godin&amp;rsquo;s book, &lt;a href="http://www.amazon.com/Linchpin-Are-Indispensable-Seth-Godin/dp/1591843162"&gt;Linchpin&lt;/a&gt;. (If you haven&amp;rsquo;t read it yet, stop reading this, buy it, start reading it, and hold on tight).&lt;/p&gt;

&lt;p&gt;All you need is the desire to make a difference, and then the shipping will follow. Yukie is starting this on her blog, &lt;a href="http://www.wearethelinchpins.com/"&gt;We Are The Linchpins&lt;/a&gt; by encouraging people to show themselves. By creating an environment for people to make a difference, it&amp;rsquo;s easy to get things done. You feel compelled to work because the work you&amp;rsquo;re doing actually means something.&lt;/p&gt;

&lt;p&gt;By joining the Linchpin community you become surrounded by people who strive to make a difference, who live to ship, and who love what they do. This keeps you inspired and motivated, and helps you aspire to be one as well.&lt;/p&gt;

&lt;p&gt;If an excuse is what you need to get started, then have a look for a Linchpin meet up near you for &lt;a href="http://www.meetup.com/Linchpins-are-everywhere-raise-the-flag/"&gt;Linchpin Meetup Day on June 14&lt;/a&gt;.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/oCWI88rSXVs" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/getting-things-done-the-linchpin-way</feedburner:origLink></entry>
 
 <entry>
   <title>960 By 640</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/b-rLKz16KKA/960-by-640" />
   <updated>2010-06-09T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/960-by-640</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s the resolution of the newly announced &lt;a href="http://www.apple.com/iphone/"&gt;iPhone 4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5 years ago we were complaining that it was difficult to design websites that would fit old monitors. The old standard of 800x600 was simply too small to do anything useful with. The table layout, and then the fluid CSS layout helped us cope, but it was still hard to imagine that most people were seeing it through that size a window. Even today it&amp;rsquo;s still hard to imagine people viewing content through such a resolution, because now they&amp;rsquo;re doing it on a phone.&lt;/p&gt;

&lt;p&gt;Multi-tasking, video calling, and HD video are all amazing features, but this new screen is what really amazes me.&lt;/p&gt;

&lt;p&gt;We&amp;rsquo;ve gone from 480,000 pixels on a 15 inch, multiple kilogram weighing, ungainly behemoth of a screen, to over 600,000 pixels on something the size and weight of a pack of cards. Wow.
&lt;img src="/uploads/2010/06/CRT-4.jpg" alt="The iPhone 4 gives you better viewing resolution than a CRT monitor" /&gt;&lt;/p&gt;

&lt;p&gt;Bring on the 1600 x 1050 smartphone.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/b-rLKz16KKA" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/960-by-640</feedburner:origLink></entry>
 
 <entry>
   <title>Dropshorty File Sharing</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/BJO1IAwEB9Y/dropshorty-file-sharing" />
   <updated>2010-06-07T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/dropshorty-file-sharing</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here&amp;rsquo;s a quick demo of an awesome OSX app, &lt;a href="http://beefdisciple.com/software/dropshorty/"&gt;DropShorty&lt;/a&gt;&lt;/p&gt;

&lt;object width="600" height="468"&gt;&lt;param name="movie" value="http://www.youtube.com/v/uvhjZDfbe6Q&amp;hl=en_US&amp;fs=1&amp;hd=1"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/uvhjZDfbe6Q&amp;hl=en_US&amp;fs=1&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="600" height="468"&gt;&lt;/embed&gt;&lt;/object&gt;


&lt;p&gt;Once you&amp;rsquo;ve downloaded it, you need to enter you Dropbox user ID. You can find this on your Dropbox folder online by going in to your shared folder, right clicking on a file and selecting &amp;ldquo;Copy public link&amp;rdquo;. You then need to copy the number in the url, as shown below:&lt;/p&gt;

&lt;p&gt;&lt;img src="/uploads/2010/06/Screen-shot-2010-06-07-at-7.54.54-PM.png" alt="Right click on a file from your public folder and choose 'Copy public link'" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="/uploads/2010/06/Screen-shot-2010-06-07-at-7.55.07-PM.png" alt="Copy the number that's in the link. That's your Dropbox ID" /&gt;&lt;/p&gt;

&lt;p&gt;Once you&amp;rsquo;ve set this (in Preferences ? + ,) , you can then connect it to to one of your URL shortening accounts.&lt;/p&gt;

&lt;p&gt;Note: The link didn&amp;rsquo;t load first time round because Dropbox was trying to sync 5GB of files I had moved recently.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/BJO1IAwEB9Y" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/dropshorty-file-sharing</feedburner:origLink></entry>
 
 <entry>
   <title>The Limits Of Social Networks</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/3e_Kg8TqUpg/the-limits-of-social-networks" />
   <updated>2010-06-04T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/the-limits-of-social-networks</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;When we talk one-on-one and face-to-face with someone, there&amp;rsquo;s a chance they might not like what you say. They could disagree, they could be offended, they could misunderstand you. There&amp;rsquo;s a risk of embarrassment. They know what you said, and you know that they know. There&amp;rsquo;s no escaping it.&lt;/p&gt;

&lt;p&gt;With social networks, there is less risk. If you say something then many people will see it. Some will still disagree, but it&amp;rsquo;s also likely that someone will agree and like what you have to say. From behind your computer, you won&amp;rsquo;t know if someone disagrees until they say so, and so it becomes easier to say things. (Besides, if you&amp;rsquo;re on Facebook, you can always delete their comments).&lt;/p&gt;

&lt;p&gt;There&amp;rsquo;s also less meaning. We lose the raw nature of conversations, and we lose the feedback that drives us to improve. It is also open to all kinds of misinterpretation. There&amp;rsquo;s a limited sense of sarcasm, and other attitudes, that we effortlessly portray and interpret through intonation when we speak.&lt;/p&gt;

&lt;p&gt;&lt;img src="/uploads/2010/06/4-up-on-2010-06-04-at-15.27-2-300x224.jpg" alt="Perhaps I'm completely wrong. By posting this to my blog it means you don't have to look at such a scary sight all the time!" /&gt;&lt;/p&gt;

&lt;p&gt;The person who can fit body language, vocal pitch, intonation, facial expression, appearance, pace of speaking, volume, as well as the words we say, into 140 characters will well and truly change the world.&lt;/p&gt;

&lt;p&gt;NB just look at how many more text comments there are on youtube than video responses. Showing yourself is scary.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/3e_Kg8TqUpg" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/the-limits-of-social-networks</feedburner:origLink></entry>
 
 <entry>
   <title>10 Reasons To Buy Things</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/HMdQ-Ca0DPs/10-reasons-to-buy-things" />
   <updated>2010-06-04T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/10-reasons-to-buy-things</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;(By &amp;lsquo;Things&amp;rsquo; I&amp;rsquo;m mainly referring to software, music, movies and books.) &lt;a href="/uploads/2010/06/the-pirate-bay-logo.jpeg"&gt;Pirate Bay&lt;/a&gt;
&lt;strong&gt;For Buying&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;You're rewarding the creator of the content for their invested time and effort.&lt;/li&gt;
    &lt;li&gt;You're likely to get some physical packaging, especially if it's music. This is great if you like collecting things.&lt;/li&gt;
    &lt;li&gt;It's the right thing to do, really.&lt;/li&gt;
    &lt;li&gt;You might get freebies, like free upgrades or bonus tracks, or an author's signature.&lt;/li&gt;
    &lt;li&gt;You'll likely get a support service if you're buying software.&lt;/li&gt;
    &lt;li&gt;If you have a problem with what you've bought, you can ask the creator about it and get help.&lt;/li&gt;
    &lt;li&gt;You can get credit card rewards points.&lt;/li&gt;
    &lt;li&gt;It won't come with any nasty spyware or malicious parts.&lt;/li&gt;
    &lt;li&gt;Some things simply aren't available anywhere other than from the original seller.&lt;/li&gt;
    &lt;li&gt;It's not against the law.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&lt;strong&gt;For Pirating&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;If it's music, chances are the artist gets next to nothing. You're feeding blood sucking, soulless record companies.&lt;/li&gt;
    &lt;li&gt;Packaging just takes up space. Digital copies are the way to go.&lt;/li&gt;
    &lt;li&gt;It's not quite the right thing to do, but who's going to notice?&lt;/li&gt;
    &lt;li&gt;The entire thing is a freebie.&lt;/li&gt;
    &lt;li&gt;The given trial period or trial set of features might not be enough to convince you to buy it. After all, you don't want to buy something that's not quite right.&lt;/li&gt;
    &lt;li&gt;If you have a problem with what you downloaded, you can just find another version elsewhere.&lt;/li&gt;
    &lt;li&gt;I don't need a credit card.. the interest is outrageous!&lt;/li&gt;
    &lt;li&gt;It won't come with any nasty DRM.&lt;/li&gt;
    &lt;li&gt;Some things simply aren't available anywhere other than on torrent sites.&lt;/li&gt;
    &lt;li&gt;It's not against the law in Russia.&lt;/li&gt;
&lt;/ol&gt;


  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/HMdQ-Ca0DPs" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/10-reasons-to-buy-things</feedburner:origLink></entry>
 
 <entry>
   <title>Sam Likes This Blog Post</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/WOW1d7vnfao/sam-likes-this-blog-post" />
   <updated>2010-06-02T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/sam-likes-this-blog-post</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A look down my Facebook news feed shows me things like &amp;ldquo;Steve likes &amp;lsquo;I wish i was little again, when the hardest choice was picking a crayon&amp;rsquo; &amp;rdquo;. Other than liking people&amp;rsquo;s business pages, or events, or photos, people are starting to like sentences.&lt;/p&gt;

&lt;p&gt;It turns out there are websites dedicated to listing sentences and phrases that people can &amp;lsquo;like&amp;rsquo;, with the release of Facebook&amp;rsquo;s new Open Graph API.&lt;/p&gt;

&lt;p&gt;One example is &lt;a href="http://mylike.co.uk/superlike/"&gt;superlike&lt;/a&gt;. Another is &lt;a href="http://chelick.net"&gt;chelick&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of updating my Facebook status, I can &amp;lsquo;like&amp;rsquo; a series of phrases that other people have said. Due to Facebook&amp;rsquo;s new settings, these likes will be publicly available.&lt;/p&gt;

&lt;p&gt;We are so used to relying on content on the web, that the act of liking even everyday phrases and comments is becoming popular. While it existed before with the many Facebook pages that were available, now it is even simpler. This is similar to re-tweeting on twitter, in a way, except the phrases you &amp;lsquo;like&amp;rsquo; are listed anonymously so you are not celebrating what a person has said, just the content of it.&lt;/p&gt;

&lt;p&gt;Will the next step be personality archetypes that we subscribe to, which automatically publish content in line with what we would say? It&amp;rsquo;s an extreme example, and not likely, I simply find it interesting to watch the trends that appear on social networks.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/WOW1d7vnfao" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/sam-likes-this-blog-post</feedburner:origLink></entry>
 
 <entry>
   <title>Getting Things Done The Microsoft Way</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/CyXVK2_ioxE/getting-things-done-the-microsoft-way" />
   <updated>2010-06-02T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/getting-things-done-the-microsoft-way</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I recently took park in the world&amp;rsquo;s largest technology competition, the Microsoft Imagine Cup. It&amp;rsquo;s a competition that challenges students to use technology to solve the biggest problems of the world today. Quite a daunting and seemingly unsurmountble task, but clearly it seemed possible to the  300,000 students who entered worldwide.&lt;/p&gt;

&lt;p&gt;Throughout the 6 months I was involved in the competition I learned many things, how to present well, how to gather feedback on ideas, how to work in a new team, etcetera. But above all else, the most important thing it taught me was how to ship.&lt;/p&gt;

&lt;p&gt;(If any of you follow Seth Godin, you&amp;rsquo;ll know what I&amp;rsquo;m talking about.)&lt;/p&gt;

&lt;p&gt;The imagine cup was an excuse to do something. We all have ideas, and we&amp;rsquo;d love to seem them come to fruition. But doesn&amp;rsquo;t that take time, effort (and super powers)? Of course it does! But that&amp;rsquo;s the easy part.&lt;/p&gt;

&lt;p&gt;We put hours and dollars into writing that work report, or doing our tax returns. Boring things that we don&amp;rsquo;t want to do, but have to. Clearly we&amp;rsquo;re good at putting in time and effort when we must.&lt;/p&gt;

&lt;p&gt;So the hard part is getting the effort to go where we want it to. If we take that first step, and step onto the right platform however, it&amp;rsquo;s easy. A competition was my (and my team&amp;rsquo;s) step and platform, and it was fantastic.&lt;/p&gt;

&lt;p&gt;As a result, the idea made it to the top 4 in New Zealand, has now transformed into a real business, and has opened up many opportunities for us to invest time and effort into something worthwhile.&lt;/p&gt;

&lt;p&gt;So if you&amp;rsquo;re stuck with an idea and don&amp;rsquo;t know how or where to start, find a competition that is related to it (there&amp;rsquo;s bound to be one). This will give you the motivation, the environment, and an excuse, to start doing what you really want to do.&lt;/p&gt;

&lt;p&gt;Besides, not only will your idea be coming to life, but you may even win something for it.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/CyXVK2_ioxE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/getting-things-done-the-microsoft-way</feedburner:origLink></entry>
 
 <entry>
   <title>Japanese Learning Program</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/AO4aLf37aXU/japanese-learning-program" />
   <updated>2010-05-28T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/japanese-learning-program</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If anyone is trying to study Japanese, and wants a list of english:hiragana:kanji readings of common words from which to make a language training program, this may help:
&lt;a href="/uploads/2010/05/words.txt"&gt;Japanese Words&lt;/a&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;\ denotes a new definition
the english, hiragana and kanji readings are separated with a colon :&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;born&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="sc"&gt;?????:????&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
&lt;span class="p"&gt;\&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;die&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="sc"&gt;?????:????&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I wrote it for final exams for a high school Japanese course. I&amp;rsquo;d like to think that it helped but really it was just a way to justify writing code while I should have been studying.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/AO4aLf37aXU" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/japanese-learning-program</feedburner:origLink></entry>
 
 <entry>
   <title>A New Kind Of Labrat</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/drCHT24pFds/a-new-kind-of-labrat" />
   <updated>2010-05-28T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/a-new-kind-of-labrat</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The study of robotics is moving from exorbitantly funded research centre&amp;rsquo;s and organizations to the humble garage workshop. Hobbyist robotics is a growing field and is being adopted all over the world. LabRat is just one of the many platforms that is allowing this to happen.&lt;/p&gt;

&lt;p&gt;Designed to fit inside the body of a typical computer mouse, LabRat gives students, researchers and hobbyists a low-cost, but in depth look into some of the core fundamentals of robotics. From between $120 and $250 (USD) the LabRat provides an 8MHz 8bit AVR processor, optical sensor, touch sensors, Infrared (IR) sensors, IEEE 802.15.4 wireless extensibility, basic IO pins, Gumstix processor extensibility and Pulse Width Modulated (PWM) driving motors that fit neatly on to an open-sourced Printed Circuit Board (PCB). The development environment for the robot is an open-sourced C based library that gives the user exact control over the robot, allowing complete freedom of creativity.&lt;/p&gt;

&lt;p&gt;The open-source nature of the device has accelerated its own design as researchers and students were able to identify bugs and flaws which LabRat could take on board straight away. It has also led to some interesting projects with the device including autonomous charging and wireless control through a playstation controller.&lt;/p&gt;

&lt;p&gt;The majority of this work has come from the Missouri University of Science and Technology, where the LabRat was used as the basis of an introductory robotics course. The device was given to the university before its final release, giving the company time to act on any feedback they received.&lt;/p&gt;

&lt;p&gt;The robot has a unique selling point in its low price and open sourced nature. As it is the only hobby level robot with open-sourced hardware, it is the obvious choice for anyone interested in extending the basic functionalities of the device, which cleverly allies with the market they are targeting. Researchers want a device they can customise to suit their own research areas or courses; students want a device they can take apart and see precisely how it works; hobbyists want a device that they can extend, hack and use in other projects; and certainly all groups want a device that doesn&amp;rsquo;t cost a lot of money.&lt;/p&gt;

&lt;p&gt;The report that was released at the 2009 International Conference on Intelligent Robots and Systems portrays precisely these attributes, providing a perfect sales pitch for those who read it. The background on robotics it provides gives the reader a very clear context in which to read the remainder of the report, and serves to highlight its key benefits and features.&lt;/p&gt;

&lt;p&gt;(note: this was an article review written for a robotics class)&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/drCHT24pFds" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/a-new-kind-of-labrat</feedburner:origLink></entry>
 
 <entry>
   <title>The Influence Wall</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/ltz7-tz6PzQ/the-influence-wall" />
   <updated>2010-05-24T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/the-influence-wall</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;It used to be, that if you wanted information on something or someone, you would go to a website about it. Once you arrived, a large number of influencing factors fought for your attention to try and make you stay on the page. The website&amp;rsquo;s appearance, colour scheme, spacing, vibe; its quality of content, user interface experience, even the domain name, had an affect on the site&amp;rsquo;s credibility.&lt;/p&gt;

&lt;p&gt;Then RSS came along and people started syndicating their content into a single stream. The interface and other usability aspects were handled by your RSS reader, leaving only the content to influence you. This placed the focus on the content. All you had to judge someone by was their words.&lt;/p&gt;

&lt;p&gt;Fortunately, however, a person who was publishing content had a lot of words to use. With the advent of twitter, people have just 140 characters to influence someone.&lt;/p&gt;

&lt;p&gt;Profile pictures and twitter backgrounds can influence to an extent, but with so many users using a twitter client, the 140 characters is all there is. (perhaps the number of followers you have is also a factor)&lt;/p&gt;

&lt;p&gt;How do we write such compelling content in such a confined space? Well, we must write snippets. Lots of them. Single thoughts that, over time, build up to a personality. We can then recommend these personalities to people. Find one person who will listen to you, make them happy, and they will talk about you.&lt;/p&gt;

&lt;p&gt;What else can one do?&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/ltz7-tz6PzQ" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/the-influence-wall</feedburner:origLink></entry>
 
 <entry>
   <title>Reflection Based Data Layer On Zf 1 10</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/3MVRrYvMa1k/reflection-based-data-layer-on-zf-1-10" />
   <updated>2010-05-19T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/reflection-based-data-layer-on-zf-1-10</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Working on a new project, I came up with this simple data layer. It uses Zend_Db_Tables to do most of the heavy lifting, but the added layer makes persisting objects a breeze.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here&amp;rsquo;s how you use it&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Application_Model_Persistence&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="vg"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vg"&gt;$store&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;User&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="vg"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Not much going on, really. The fetch method takes 3 arguments:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;The&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt; &lt;span class="n"&gt;want&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;

&lt;span class="sr"&gt;  &amp;lt;li&amp;gt;The key you want to search on&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;The&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;specified&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;

&lt;span class="sr"&gt;&amp;lt;/u&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The object name matches the table name in the database. The fetch method is looking for a model class of the name Application_Model_User, instantiating it, and then querying the database with a query select * where id = 5, from the table Zend_Db_DbTable_User.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The Application_Model_User class is then populated with the returned database data, and subsequently returned to the user.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A &amp;lsquo;persist&amp;rsquo; method does the opposite. Given an object, it either creates a new entry in the database or updates an existing one.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;At the moment it can only handle primary key selects &amp;ndash; multiple rowsets will probably cause it to crash.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;It does really simplify the actual implementation however, as you only need a single object to fetch, update, or create all of your data objects.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Calling the fetch method 1000 times takes roughly 6 times longer than a raw sql query, but it&amp;rsquo;s vastly quicker and simpler to implement with, and more convenient as the data is returned as an object.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here&amp;rsquo;s the method
For each type of model that is fetched, the dbTable object is stored here for quick access. Speeds up subsequent accesses in a single request by a factor of 6 or more.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;  
&lt;span class="kp"&gt;protected&lt;/span&gt; &lt;span class="vg"&gt;$tableCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Create an Application_Model_Template object of the given type, by selecting from the database querying on the given key with the given value. For example
fetch me a location which has an id of 13
fetch(&amp;lsquo;Location&amp;rsquo;, &amp;lsquo;id&amp;rsquo;, 13);
@return Application_Model_Template
@param string $type
@param string $key
@param mixed $value
@throws Exception &amp;ndash; Class name does not match DbTable name&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="kp"&gt;public&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="vg"&gt;$dbTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vg"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getDbTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="vg"&gt;$dbTable&lt;/span&gt; &lt;span class="n"&gt;instanceof&lt;/span&gt; &lt;span class="no"&gt;Zend_Db_Table_Abstract&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kp"&gt;throw&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Class name does not match DbTable name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;		
    
    &lt;span class="vg"&gt;$select&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vg"&gt;$dbTable&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;select&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="vg"&gt;$select&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$key&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39; = ?&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="vg"&gt;$row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vg"&gt;$dbTable&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;fetchRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$select&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    
    &lt;span class="n"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="vg"&gt;$class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Application_Model_&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="vg"&gt;$type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="vg"&gt;$obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="vg"&gt;$class&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$row&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kp"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Exception&lt;/span&gt; &lt;span class="vg"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="vg"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;	
  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vg"&gt;$obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Return an Application_Model_DbTable_dbTableName object for the given dbTableName. The tableCache is checked first using dbTableName as the key; if not found it is created, and stored in the cache.
@return Application_Model_DbTable
@param string $dbTableName&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="kp"&gt;protected&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getDbTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$dbTableName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="vg"&gt;$dbTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Application_Model_DbTable_&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="vg"&gt;$dbTableName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;array_key_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$dbTableName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tableCache&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;cache hit&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vg"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tableCache&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vg"&gt;$dbTableName&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;cache miss&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;        &lt;span class="vg"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tableCache&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vg"&gt;$dbTableName&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="vg"&gt;$dbTable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vg"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tableCache&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="vg"&gt;$dbTableName&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Mistakes, flaws, and potential security threats would be greatly appreciated! It&amp;rsquo;s in no way complete, mainly proof of concept at present. I&amp;rsquo;d like to expand it with some real error checking, let it handle multiple rowsets, etc.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/3MVRrYvMa1k" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/reflection-based-data-layer-on-zf-1-10</feedburner:origLink></entry>
 
 <entry>
   <title>Meeting People</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/N4tYmHwhyQU/meeting-people" />
   <updated>2010-05-19T00:00:00+12:00</updated>
   <id>http://oneofthesedaysblog.com/meeting-people</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Meeting people is good to do. People love being recognised, and also recognising other people. It makes us human. Meeting people leads to great things. That new job you were looking for, sponsorship for your latest project, a new friend even.&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s simply amazing where you meet people. I was sitting in a meeting today (funny, that) and someone came in who I had met a few weeks ago at an event. Neither of us knew that we had anything to do with the group we were both meeting with, but it just so happened that we did. I looked good, he looked good, and the group in the middle looked good as well.&lt;/p&gt;

&lt;p&gt;All because of a chance meeting, which only occurred because of a previous chance meeting, which only occurred.. you get the idea.&lt;/p&gt;

&lt;p&gt;So next time someone wants to have coffee with you, or invites you to some event, just go! Who knows who you will meet there.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/N4tYmHwhyQU" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/meeting-people</feedburner:origLink></entry>
 
 <entry>
   <title>Cross Browser Css Dropdown</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/4aTbj4KVXgE/cross-browser-css-dropdown" />
   <updated>2009-02-28T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/cross-browser-css-dropdown</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The CSS dropdown from the last post works in all browsers except IE6! I&amp;rsquo;ve made a few minor changes to ensure it displays correctly in IE7 however.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Navigation&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    	&lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;#FFFFFF none repeat scroll 0 0;&lt;/span&gt;
    &lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt; &lt;span class="c1"&gt;#000000;&lt;/span&gt;
    &lt;span class="n"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="ss"&gt;:absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;720&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;overflow&lt;/span&gt;&lt;span class="ss"&gt;:visible&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="ss"&gt;:-&lt;/span&gt;&lt;span class="mi"&gt;135&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;A z-index was applied to keep it above any content that might be below, and a left position was added to move the dropdown over slightly.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now as for IE6, we can get it to work with some simple javascript. Not the most accessible solution but really, if you&amp;rsquo;re using IE6 and you have javascript disabled, you deserve it.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Using jQuery we can add the hover event to our list with a few lines */&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;.Navigation ul li&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;overflow&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;visible&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;overflow&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;hidden&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;and voila! Simply setting the overflow property when each list element is hovered is enough to make the dropdown work.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now if you don&amp;rsquo;t want the extra overhead of the jquery library you can use conditional comments to only grab it if they&amp;rsquo;re using IE.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;lt;!--[&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;IE&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;]&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js&amp;#39;&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;text/javascript&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&amp;gt;&lt;/span&gt;
&lt;span class="sr"&gt;&amp;lt;script type=&amp;#39;text/&lt;/span&gt;&lt;span class="n"&gt;javascript&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&amp;gt;&lt;/span&gt;
&lt;span class="s1"&gt;$(function() {&lt;/span&gt;
&lt;span class="s1"&gt;  $(&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Navigation&lt;/span&gt; &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;).hover(&lt;/span&gt;
&lt;span class="s1"&gt;  function () {&lt;/span&gt;
&lt;span class="s1"&gt;    $(this).css(&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;overflow&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class="n"&gt;visible&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;);&lt;/span&gt;
&lt;span class="s1"&gt;  },&lt;/span&gt;
&lt;span class="s1"&gt;  function () {&lt;/span&gt;
&lt;span class="s1"&gt;    $(this).css(&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;overflow&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class="n"&gt;hidden&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&amp;gt;&lt;/span&gt;
&lt;span class="sr"&gt;&amp;lt;![endif]--&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/4aTbj4KVXgE" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/cross-browser-css-dropdown</feedburner:origLink></entry>
 
 <entry>
   <title>Pure Css Dropdown For Firefox</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/CUq9INWWTN4/pure-css-dropdown-for-firefox" />
   <updated>2009-02-27T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/pure-css-dropdown-for-firefox</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Here&amp;rsquo;s a quick and dirty pure CSS dropdown that at present only works in Firefox.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Navigation&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="ss"&gt;:relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="ss"&gt;:block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="ss"&gt;:left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;overflow&lt;/span&gt;&lt;span class="ss"&gt;:hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="ss"&gt;:inline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="n"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Navigation&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="ss"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="ss"&gt;:orange&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="ss"&gt;:pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;overflow&lt;/span&gt;&lt;span class="ss"&gt;:visible&lt;/span&gt;&lt;span class="p"&gt;;}&lt;/span&gt;

&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Navigation&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;#FFFFFF none repeat scroll 0 0;&lt;/span&gt;
  &lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt; &lt;span class="c1"&gt;#000000;&lt;/span&gt;
  &lt;span class="n"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="ss"&gt;:absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;600&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;overflow&lt;/span&gt;&lt;span class="ss"&gt;:visible&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="n"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Navigation&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="ss"&gt;:inline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;#000; }&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;That&amp;rsquo;s all the CSS needed, along with this HTML:&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Navigation&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;Home&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;About&lt;/span&gt; &lt;span class="no"&gt;Us&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&amp;gt;&lt;/span&gt;
&lt;span class="sr"&gt;            &amp;lt;ul&amp;gt;&lt;/span&gt;
&lt;span class="sr"&gt;                &amp;lt;li&amp;gt;Sub Link 1&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;Sub&lt;/span&gt; &lt;span class="no"&gt;Link&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;
&lt;span class="sr"&gt;                &amp;lt;li&amp;gt;Sub Link 3&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="sr"&gt;        &amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;Products&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="sr"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;By floating each list element as a block element, we can hide the sub menu by hiding the overflow content of the div. Hovering over an element sets overflow to visible, and so the sub menu appears.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now if only it worked in IE..&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/CUq9INWWTN4" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/pure-css-dropdown-for-firefox</feedburner:origLink></entry>
 
 <entry>
   <title>Running Yui Rich Text Editor Inside Thickbox</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/msToTvoan3w/running-yui-rich-text-editor-inside-thickbox" />
   <updated>2009-02-04T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/running-yui-rich-text-editor-inside-thickbox</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="sr"&gt;//&lt;/span&gt;&lt;span class="no"&gt;The&lt;/span&gt; &lt;span class="n"&gt;latest&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;work&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="n"&gt;me&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;rich&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;RTE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="n"&gt;inside&lt;/span&gt; &lt;span class="n"&gt;jquery&lt;/span&gt; &lt;span class="n"&gt;thickbox&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="no"&gt;After&lt;/span&gt; &lt;span class="n"&gt;trying&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;few&lt;/span&gt; &lt;span class="n"&gt;different&lt;/span&gt; &lt;span class="no"&gt;RTEs&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;finally&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="no"&gt;Yahoo&lt;/span&gt; &lt;span class="no"&gt;YUI&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;looks&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;nicest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;easily&lt;/span&gt; &lt;span class="n"&gt;customized&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="sr"&gt;&lt;/span&gt;

&lt;span class="sr"&gt;//After 5 lines, YUI editor was up and running inside thickbox. Sweet, no problems there. However the insert image and link boxes failed completely, in all browsers. &lt;/span&gt;

&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="no"&gt;The&lt;/span&gt; &lt;span class="n"&gt;boxes&lt;/span&gt; &lt;span class="n"&gt;were&lt;/span&gt; &lt;span class="n"&gt;created&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;inserted&lt;/span&gt; &lt;span class="n"&gt;alongside&lt;/span&gt; &lt;span class="n"&gt;thickbox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;instead&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;inside&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="no"&gt;Thankfully&lt;/span&gt; &lt;span class="n"&gt;thanks&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="no"&gt;YUI&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;s events this was an easy fix.&lt;/span&gt;
&lt;span class="s1"&gt;editor.on(&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;windowInsertImageRender&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;, function() {&lt;/span&gt;
&lt;span class="s1"&gt;     document.getElementById(&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;dd&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;).appendChild(this.get(&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;panel&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;).element);&lt;/span&gt;
&lt;span class="s1"&gt;});&lt;/span&gt;

&lt;span class="s1"&gt;//Here, &amp;quot;dd-editor&amp;quot; is the ID of the div surrounding by YUI editor and &amp;#39;&lt;/span&gt;&lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; is the name of my YUI editor object. We simply set it to listen to the &amp;#39;&lt;/span&gt;&lt;span class="n"&gt;windowInsertImageRender&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; event and, when it occurs, attach it to the same div that the editor is in.&lt;/span&gt;

&lt;span class="s1"&gt;//That fixed, it seemed to be working very smoothly until, surprise, it fails in IE.&lt;/span&gt;

&lt;span class="s1"&gt;//![YUI Thickbox Bug](/uploads/2009/02/yui-thickbox-scroll-bug.png)&lt;/span&gt;

&lt;span class="s1"&gt;//When the thickbox is too small to hold all of the content and scrollbars appear, scrolling down creates all manners of positioning problems with the toolbar. Drilling down with the IE developer toolbar revealed that scrolling works down to the list element with a class of &amp;quot;yui-toolbar-groupitem&amp;quot;. It is the elements inside that bug out.&lt;/span&gt;

&lt;span class="s1"&gt;//After an hour of trying various css positionings and styles, I managed to fix the content area and title bar in place with a bit of jquery.&lt;/span&gt;
&lt;span class="s1"&gt;$(&amp;quot;#editor_editor&amp;quot;).attr(&amp;quot;position&amp;quot;, &amp;quot;static&amp;quot;)&lt;/span&gt;

&lt;span class="s1"&gt;//A fix for the buttons was eventually found as well, however it isn&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="n"&gt;so&lt;/span&gt; &lt;span class="n"&gt;much&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;fix&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;sluggish&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ungainly&lt;/span&gt; &lt;span class="n"&gt;hack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#TB_ajaxContent&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scroll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toolbar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collapse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toolbar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collapse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="sr"&gt;&lt;/span&gt;

&lt;span class="sr"&gt;//Hooking in to the scroll event on the thickbox window, each time it moves we collapse and then redisplay the toolbar. This causes the buttons to jump into the correct place at the cost of a very slow scrolling time. &lt;/span&gt;

&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="no"&gt;Note&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;above&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="n"&gt;need&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;called&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;fulled&lt;/span&gt; &lt;span class="n"&gt;rendered&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;IE&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="no"&gt;This&lt;/span&gt; &lt;span class="n"&gt;can&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;windowRender&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;jquery&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="n"&gt;so&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$.&lt;/span&gt;&lt;span class="n"&gt;browser&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;msie&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;windowRender&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#editor_editor&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;position&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;static&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#TB_ajaxContent&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scroll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toolbar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collapse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toolbar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collapse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/msToTvoan3w" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/running-yui-rich-text-editor-inside-thickbox</feedburner:origLink></entry>
 
 <entry>
   <title>Checking Your Account Data With Zend_http_client</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/yWr8phu80S8/checking-your-account-data-with-zend_http_client" />
   <updated>2009-01-26T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/checking-your-account-data-with-zend_http_client</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I live in a flat with 2 other avid internet users. Some months our bandwidth gets used up within a week yet my computer tells me I&amp;rsquo;ve only used a few hundred megabytes. In order to get accurate accountability for my internet usage, in comparrison to my flatmates, I wrote a PHP script to give me my usage as a percentage of the total usage for each day.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Our ISP provides a web portal which gives statistics such as daily and monthly usage. To get the statistics from my own usage, I installed &lt;a href="http://humdi.net/vnstat/"&gt;vnstat&lt;/a&gt;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Two combine the 2 I decided I would use PHP as an excuse to try out the Zend_Http_Client component.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;First off I needed to grab the stats from my computer. This is quite straightforward with the system() function.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="n"&gt;ob_start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nb"&gt;system&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;vnstat -d -s&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="vg"&gt;$vnstat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ob_get_contents&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;ob_end_clean&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Output buffering is used to capture the information that the system command normally sends to the browser. As the system command only returns the last line of output from the command, we can get the entire output by using ob_get_contents().&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The options for vnstat make it return statistics for just today (-d) and in short form (-s), looking like this&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;          &lt;span class="n"&gt;rx&lt;/span&gt;       &lt;span class="o"&gt;/&lt;/span&gt;         &lt;span class="n"&gt;tx&lt;/span&gt;      &lt;span class="o"&gt;/&lt;/span&gt;        &lt;span class="n"&gt;total&lt;/span&gt;    &lt;span class="o"&gt;/&lt;/span&gt;  &lt;span class="n"&gt;estimated&lt;/span&gt;
 &lt;span class="n"&gt;wlan0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
           &lt;span class="n"&gt;yesterday&lt;/span&gt;    &lt;span class="mi"&gt;162&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;76&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;  &lt;span class="o"&gt;/&lt;/span&gt;   &lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;  &lt;span class="o"&gt;/&lt;/span&gt;  &lt;span class="mi"&gt;241&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mo"&gt;00&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;
         &lt;span class="n"&gt;today&lt;/span&gt;    &lt;span class="mi"&gt;138&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;  &lt;span class="o"&gt;/&lt;/span&gt;   &lt;span class="mi"&gt;89&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;97&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;  &lt;span class="o"&gt;/&lt;/span&gt;  &lt;span class="mi"&gt;228&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;  &lt;span class="o"&gt;/&lt;/span&gt;     &lt;span class="mi"&gt;523&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now we need to get just the total for today:
make an array of the output&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$vnstatComponents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$vnstat&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;get today&amp;rsquo;s usage (2nd to last element of the array) and remove whitespace&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$netUsage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$vnstatComponents&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$vnstatComponents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-9'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-9"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;remove &amp;lsquo;MB&amp;rsquo;, giving us just the number&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$myUsage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$netUsage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$netUsage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-10'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-10"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;We&amp;rsquo;re now ready to start using Zend_Http_Client.
open a link to isp website&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Zend_Http_Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;https:// the.vodafone.co.nz/acnts/myaccounts.pl/login&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="vg"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Zend_Http_Client&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="vg"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setParameterPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;&amp;#39;login&amp;#39;&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;myusername&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;&amp;#39;password&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;mypassword&amp;#39;&lt;/span&gt;
&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="vg"&gt;$response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vg"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-11'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-11"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;grab our cookies&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$cookiejar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Zend_Http_CookieJar&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fromResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vg"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getUri&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-12'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-12"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The link we open a connection to is the link that the login form will submit to. Firebug can be handy to quickly find this out. You&amp;rsquo;ll also need to set the array keys in the setParameterPost method to match the names of the username and password fields on the login form.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-13'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-13"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;The last part of the code is what will enable us to persist our login credentials. Zend_Http_CookieJar is fairly self explanatory in it&amp;rsquo;s name. We pass in the request object and the uri of the site we&amp;rsquo;re posting to and it will store the cookies that the browser would receive if we were actually visiting the site.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-14'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-14"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now let&amp;rsquo;s try and log in to the usage page with our new cookies.
if we&amp;rsquo;re logged in ok, open account page&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$response&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;OK&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="vg"&gt;$client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Zend_Http_Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;https:# the.vodafone.co.nz/acnts/myaccount-int.pl/usage-data&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="vg"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setCookieJar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$cookiejar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="vg"&gt;$usageResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vg"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;	
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-15'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-15"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;And that&amp;rsquo;s it! We&amp;rsquo;ve now got an object that contains the entire content for the page we specified in the Zend_Http_Client constructor. Now this data might be a little unwieldy to dissect so we can use another handy Zend component, Zend_Dom_Query.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$dom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Zend_Dom_Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vg"&gt;$usageResponse&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getBody&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-16'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-16"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If you have any experience with CSS then the usage of this object for getting data will seem quite natural.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class="vg"&gt;$dom&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;.tdDataRight&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-17'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-17"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;This will return an array of all elements with the class &amp;lsquo;tdDataRight&amp;rsquo;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-18'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-18"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;While this example is rather specific, it shows how to use Zend_Http_Client in a way that will no doubt apply to many other situations.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/yWr8phu80S8" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/checking-your-account-data-with-zend_http_client</feedburner:origLink></entry>
 
 <entry>
   <title>New On The Blog Scene</title>
   <link href="http://feedproxy.google.com/~r/feedburner/ootdb/~3/4ktDXXYrwUY/new-on-the-blog-scene" />
   <updated>2009-01-24T00:00:00+13:00</updated>
   <id>http://oneofthesedaysblog.com/new-on-the-blog-scene</id>
   <content type="html">&lt;tr id='section-1'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-1"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;After reading many blogs for many months I have finally managed to enter the blogosphere.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-2'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-2"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;First, a little about myself. I&amp;rsquo;m currently in my third year of studying Software Engineering at the University of Auckland, New Zealand. I&amp;rsquo;ve also been doing freelance web development for the better part of 4 years. I also have a job as a developer/IT support guy at a company that deals with online learning solutions.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-3'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-3"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I love to work with PHP; it&amp;rsquo;s a very no fuss language that gives you a lot of flexibility, while still providing the strength required to make complex applications. As far as frameworks go, I&amp;rsquo;m all for them and use the Zend Framework.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-4'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-4"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I&amp;rsquo;ve also worked with the .NET framework with C# for both web and windows development and I have to say that I really enjoy using it. It&amp;rsquo;s a very powerful system that makes coding easy and fun. Unfortunately it&amp;rsquo;s not so great to develop from a Linux machine, which is my primary computer, and the licensing costs are only viable when you have a company paying for everything.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-5'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-5"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;If you&amp;rsquo;re interested in some of my work, check out my portfolio at &lt;a href="http://www.samdalton.com"&gt;www.samdalton.co.nz&lt;/a&gt;.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-6'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-6"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;Now that work is out of the way, some more about my personal self.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-7'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-7"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;I&amp;rsquo;m an absolute music nut and spend my days listening to the likes of Pink Floyd, Led Zeppelin, Beethoven, Bach, Miles Davis, BB King, Tool, The Doors, Don McLean and other such blues/classic rock/classical/jazz bands, artists and composers. I&amp;rsquo;ve been learning Japanese for about 8 years now and really enjoy doing so. I play guitar in a band too.&lt;/p&gt;
  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;tr id='section-8'&gt;
  &lt;td class=docs&gt;
    &lt;div class="pilwrap"&gt;
      &lt;a class="pilcrow" href="#section-8"&gt;&amp;#182;&lt;/a&gt;
    &lt;/div&gt;
    &lt;p&gt;What&amp;rsquo;s my reason for blogging? Many times I&amp;rsquo;ve found a solution to a problem that I simply was not able to find on the internet. I hope that by posting about problems and solutions that I come across, it will help others who find themselves in the same spot. As well as this, it&amp;rsquo;s a place for me to collect my thoughts and write about things that interest me, and hopefully others.&lt;/p&gt;

  &lt;/td&gt;
  &lt;td class=code&gt;
    &lt;div class='highlight'&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;img src="http://feeds.feedburner.com/~r/feedburner/ootdb/~4/4ktDXXYrwUY" height="1" width="1"/&gt;</content>
 <feedburner:origLink>http://oneofthesedaysblog.com/new-on-the-blog-scene</feedburner:origLink></entry>
 
 
</feed>

