<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQUARISM</title>
	<atom:link href="http://squarism.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://squarism.com</link>
	<description>until lambs become lions</description>
	<lastBuildDate>Fri, 03 Feb 2012 05:13:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Rubygems Size, Bad Algorithms and a Bad Data Structure</title>
		<link>http://squarism.com/2012/01/30/rubygems-size-bad-algorithm/</link>
		<comments>http://squarism.com/2012/01/30/rubygems-size-bad-algorithm/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 04:01:42 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1507</guid>
		<description><![CDATA[I mirrored ruby gems just to see how big it would be. I used the rubygems-mirror gem. It&#8217;s pretty simple. Just cd into a directory with a lot of space (ie: /opt/gems or something) and type `gem mirror`. After a massive initial load of 155k gems, the size was about 45GB (currently, it grows pretty [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2012/01/pixel_disk.png" alt="" title="pixel_disk" width="157" height="150" class="alignright size-full wp-image-1513" /><br />
I mirrored ruby gems just to see how big it would be.  I used the  rubygems-mirror gem.  It&#8217;s pretty simple.  Just cd into a directory with a lot of space (ie: /opt/gems or something) and type `gem mirror`. </p>
<p>After a massive initial load of 155k gems, the size was about 45GB (currently, it grows pretty quick per week).  The rubygem and gem mirror command is smart enough to just download just the deltas when you run it again:</p>
<p><code><br />
$ gem mirror<br />
Fetching: http://rubygems.org/specs.4.8.gz<br />
Total gems: 170843<br />
Fetching 16176 gems<br />
................................................................<br />
</code></p>
<p>Then I <a href="http://twitter.com/#!/squarism/status/162574507817697280">wanted to know</a> the size of all the latest gems only.  If I had to do a lazy sneakernet, this might be one method of grabbing a whole bunch of dependencies (of course this would never work).  Regardless of that, I still wanted to know what percentage of ruby gems space is old versions.</p>
<p>So I wrote a ruby program to find all the latest versions of the gem files and total up their size.  I was not very happy about my experiments with #sort and #sort_by.  The biggest problem is that it took <strong>64 HOURS</strong> to run.  I knew it had lots of problems but I didn&#8217;t want to kill it.  I wanted to see how bad it really ran.</p>
<p>I&#8217;m not going to post the actual code.  You can see the old version at this <a href="https://github.com/squarism/sandbox/commit/7208efed6eba5ea588e4c76e8f34626feac003d2">git commit url</a>.  The basic gist of the crappy algorithm was something like this:</p>
<pre>
Find all the files in the gem mirror off the filesystem.
Get the basename of the file name (ie: strip the path).  /tmp/foo-0.1.gem -> foo-0.1.gem
Go through all the basenames (gem names) find the gem family.
</pre>
<p>Here&#8217;s the problem.  I had a massive list of 170k gems and then I&#8217;m trying to do a find_all right here to sort the gems into gem families.  For example: there might be foo-0.1.gem, foo-0.2.gem and foo-async-0.1.gem.  In this example, there are two gem families out of the three gems.  Foo-async and foo are two different gems with their own versions.  Later on, I would:</p>
<pre>
Do a version compare.
Push the latest version name to an array.
Delete the gem family name from the gem_names array.
</pre>
<p>Sounded good on paper.  And then it took 65 hours to run (227305.19 seconds) and CPU was absolutely pegged the entire time.  This algorithm was easy to come up with in IRB using a small test data set but scaling up in the real use case completely sucked.  So I pushed it to github for versioning and rewrote the loop.</p>
<p>The <a href="https://github.com/squarism/sandbox/blob/master/latest_gem_sizes.rb">latest version</a> runs in 8.5 seconds and spits out a total size of all the latest ruby gems at 6.5GB.  Of course, this information is useless since it&#8217;s not going to check compatibility or anything.  I was just curious to know how much space is back versions.</p>
<p>The real key to the new version is the fact that I&#8217;m using a proper &#8220;grouped&#8221; data structure (Hash) instead of a massive flat Array.  This allows the regexes and other operations to work on a smaller data set.  The compound nature of the previous inefficiency is pretty amazing (hours to seconds).</p>
<p><img src="http://squarism.com/wp-content/uploads/2012/01/gems_algorithm_1.png" alt="" title="gems_algorithm_1" width="650" height="375" class="aligncenter size-full wp-image-1520" /></p>
<p><img src="http://squarism.com/wp-content/uploads/2012/01/gems_algorithm_2.png" alt="" title="gems_algorithm_2" width="650" height="375" class="aligncenter size-full wp-image-1521" /></p>
<p><img src="http://squarism.com/wp-content/uploads/2012/01/gems_algorithm_3.png" alt="" title="gems_algorithm_3" width="650" height="375" class="aligncenter size-full wp-image-1522" /></p>
<p>So hopefully you see above that a huge array of a File glob is flat and makes regex&#8217;s or grouping operations very time consuming.  Ruby&#8217;s magic group_by method sorts and groups the data structure once and then it&#8217;s much easier to regex out versions and do other things.</p>
<p>See below for the code inline or <a href="https://github.com/squarism/sandbox/blob/master/latest_gem_sizes.rb">take a look at the github repo</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># refactored the 63 hour version to a much better 8.5 second version</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'action_view'</span>
<span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActionView::Helpers</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># change to location of rubygems mirror</span>
GEM_DIR = <span style="color:#996600;">&quot;/opt/rubygems/gems&quot;</span>
&nbsp;
gems = <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">glob</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{GEM_DIR}/**/*.gem&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#006666;">1</span>
gems = gems.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> g.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">last</span><span style="color:#006600; font-weight:bold;">&#125;</span>; 
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Version
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Comparable</span>
  attr_reader <span style="color:#ff3333; font-weight:bold;">:major</span>, <span style="color:#ff3333; font-weight:bold;">:feature_group</span>, <span style="color:#ff3333; font-weight:bold;">:feature</span>, <span style="color:#ff3333; font-weight:bold;">:bugfix</span>, <span style="color:#ff3333; font-weight:bold;">:version_string</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>version=<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@version_string</span> = version
    <span style="color:#0066ff; font-weight:bold;">@major</span> = <span style="color:#996600;">&quot;0&quot;</span>; <span style="color:#0066ff; font-weight:bold;">@feature_group</span> = <span style="color:#996600;">&quot;0&quot;</span>; <span style="color:#0066ff; font-weight:bold;">@feature</span> = <span style="color:#996600;">&quot;0&quot;</span>; <span style="color:#0066ff; font-weight:bold;">@bugfix</span> = <span style="color:#996600;">&quot;0&quot;</span>
&nbsp;
    v = version.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># puts v.join(&quot;|&quot;)</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#0066ff; font-weight:bold;">@major</span> = v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#9966CC; font-weight:bold;">else</span>; <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Major number blank.&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#0066ff; font-weight:bold;">@feature_group</span> = v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#0066ff; font-weight:bold;">@feature</span> = v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#0066ff; font-weight:bold;">@bugfix</span> = v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># strangely enough .to_i works even for</span>
  <span style="color:#008000; font-style:italic;"># &gt;&gt; &quot;6-mswin32&quot;.to_i</span>
  <span style="color:#008000; font-style:italic;"># =&gt; 6</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>other<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@major</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">major</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>@major.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">major</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@feature_group</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">feature_group</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>@feature_group.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">feature_group</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@feature</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">feature</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>@feature.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">feature</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@bugfix</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">bugfix</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>@bugfix.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other.<span style="color:#9900CC;">bugfix</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># we probably have two things equal here</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;FALLING THROUGH in &lt;=&gt;, not good&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">sort</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">sort</span>!<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span> a <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> b<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_s
    <span style="color:#0066ff; font-weight:bold;">@version_string</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># temporary benchmarking</span>
RubyProf.<span style="color:#9900CC;">start</span>
&nbsp;
group_r = <span style="color:#CC00FF; font-weight:bold;">Regexp</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span>\.\d<span style="color:#006600; font-weight:bold;">+</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>\.<span style="color:#9900CC;">gem</span>$<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
gems_grouped = gems.<span style="color:#9900CC;">group_by</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> g.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>group_r<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; {&quot;firewool&quot;=&gt;[&quot;firewool-0.1.0.gem&quot;, &quot;firewool-0.1.1.gem&quot;}], ... }</span>
&nbsp;
latest_gems = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
gems_grouped.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span>
  versions = g<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>ver<span style="color:#006600; font-weight:bold;">|</span> ver.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>group_r<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#008000; font-style:italic;"># =&gt; [&quot;0.1.0&quot;, &quot;0.1.1&quot;, &quot;0.1.2&quot;]</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    latest = versions.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> Version.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">sort</span>.<span style="color:#9900CC;">reverse</span>.<span style="color:#9900CC;">first</span>
    <span style="color:#008000; font-style:italic;"># =&gt; &quot;0.1.2&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> g
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">NoMethodError</span>
    <span style="color:#008000; font-style:italic;"># somebody's got some crazy gem naming conventions</span>
    <span style="color:#008000; font-style:italic;"># for example: chill-1.gem</span>
    gems_grouped.<span style="color:#9900CC;">delete</span> g
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  latest_gems <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;#{g[0]}-#{latest}.gem&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
total = <span style="color:#006666;">0</span>
latest_gems.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>gem<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    total = total <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{GEM_DIR}/#{gem}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Errno</span>::ENOENT <span style="color:#006600; font-weight:bold;">=&gt;</span> e
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;WTF no #{gem}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Total size of newest gems in #{GEM_DIR} is #{number_to_human_size(total)}&quot;</span></pre></div></div>

<p>Algorithm win.  Rubygem mirror size curiosity complete.  6.5GB is current gems out of 45GB (right now).</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2012/01/30/rubygems-size-bad-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sparkfun Free Day 2011</title>
		<link>http://squarism.com/2012/01/13/sparkfun-free-day-2011/</link>
		<comments>http://squarism.com/2012/01/13/sparkfun-free-day-2011/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 23:09:28 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1498</guid>
		<description><![CDATA[Tried filling out hours and hours of captchas while watching the newborn. It didn&#8217;t go so well. I got sore hands and didn&#8217;t get $100 of free stuff. I ended up ordering a new arduino r3 anyway and a stepper motor to hopefully build a quiet baby rocker. I built a few previous iterations of [...]]]></description>
			<content:encoded><![CDATA[<p>Tried filling out hours and hours of captchas while watching the newborn.  It didn&#8217;t go so well.  I got sore hands and didn&#8217;t get $100 of free stuff.  I ended up ordering a new arduino r3 anyway and a stepper motor to hopefully build a quiet baby rocker.  I built a few previous iterations of a baby rocker out of Lego technic pieces but the motor was a little bit too loud and I&#8217;ve scrapped the project.</p>
<p>Anyway, here&#8217;s a screenshot to remind me of the captcha fun.</p>
<p><img src="http://squarism.com/wp-content/uploads/2012/01/sparkfun_captcha_forever.png" alt="" title="sparkfun_captcha_forever" width="544" height="640" class="aligncenter size-full wp-image-1499" /></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2012/01/13/sparkfun-free-day-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quickie Mart</title>
		<link>http://squarism.com/2011/12/18/quickie-mart/</link>
		<comments>http://squarism.com/2011/12/18/quickie-mart/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 19:38:28 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1493</guid>
		<description><![CDATA[A 20 minute Rails demo that I used as part of a &#8220;What is Ruby on Rails?&#8221; talk. The store was not designed or developed from scratch in 20 minutes but serves as a Cooking Show style demo of what is possible in a very short amount of time. Quickie Mart on Github]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2011/12/quickie_mart.png" alt="" title="quickie_mart" width="468" height="292" class="aligncenter size-full wp-image-1494" /></p>
<p>A 20 minute Rails demo that I used as part of a &#8220;What is Ruby on Rails?&#8221; talk.  The store was not designed or developed from scratch in 20 minutes but serves as a Cooking Show style demo of what is possible in a very short amount of time.</p>
<p><a href="https://github.com/squarism/quickie_mart" title="Quickie Mart on Github">Quickie Mart on Github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/12/18/quickie-mart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rbenv bash prompt</title>
		<link>http://squarism.com/2011/12/05/rbenv-bash-prompt/</link>
		<comments>http://squarism.com/2011/12/05/rbenv-bash-prompt/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 21:05:27 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1479</guid>
		<description><![CDATA[Sam Stephenson has a pretty looking bash prompt screenshotted in his rbenv project&#8217;s README. All you have to do is: Set your Terminal theme to Basic. Make sure to re-set any preferences you might have (like no audible bell etc). Set your Terminal font to 13pt Inconsolata. This isn&#8217;t the exact font he uses but [...]]]></description>
			<content:encoded><![CDATA[<p>Sam Stephenson <a href="https://github.com/sstephenson/rbenv">has a pretty looking bash prompt</a> screenshotted in his rbenv project&#8217;s README.  All you have to do is:</p>
<ul>
<li>Set your Terminal theme to Basic.  Make sure to re-set any preferences you might have (like no audible bell etc).</li>
<li>Set your Terminal font to 13pt Inconsolata.  This isn&#8217;t the exact font he uses but it&#8217;s as close as I could find.</li>
<li>Set your ANSI Color for Normal White(7) to Tin (Crayons tab in color picker)</li>
<li>Put this bash code from <a href="https://gist.github.com/1423532">this gist</a> at the end of your .profile file.</li>
<li>Install rbenv so you don&#8217;t get the errors I got below because I&#8217;m still on RVM.  :)</li>
</ul>
<p><img src="http://squarism.com/wp-content/uploads/2011/12/rbenv_wannabe.png" alt="" title="rbenv_wannabe" width="613" height="353" class="aligncenter size-full wp-image-1480" /></p>
<p>I covet, I steal.</p>
<p>Even though I don&#8217;t show it there, the bash script will do the git repo status magic for you on OSX.  You need to brew install git and have the shell completion scripts in /usr/local (<a href="http://mxcl.github.com/homebrew/">homebrew</a> will do this).</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/12/05/rbenv-bash-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hash of Hashes and Captain Planet</title>
		<link>http://squarism.com/2011/12/04/hash-of-hashes-and-captain-planet/</link>
		<comments>http://squarism.com/2011/12/04/hash-of-hashes-and-captain-planet/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 05:09:36 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1457</guid>
		<description><![CDATA[Don Cheadle is the best version of Captain Planet there is. I hope you&#8217;ve already seen the video. If not, go now. I&#8217;ll wait. As it typically happens, I was creating some nested data structure and was reminded of all the different combinations that there are. For example: An array of arrays.[ [1,2,3], [4,5,6] ] [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2011/11/Don-Cheadle-Captain-Planet.jpg" alt="" title="Don-Cheadle-Captain-Planet" width="150" height="150" class="alignright size-full wp-image-1458" /></p>
<p>Don Cheadle is the best version of Captain Planet there is.  I hope you&#8217;ve already seen <a href="http://www.funnyordie.com/videos/5876f2aced/don-cheadle-is-captain-planet">the video</a>.  If not, go now.  I&#8217;ll wait.</p>
<p>As it typically happens, I was creating some nested data structure and was reminded of all the different combinations that there are.  For example:</p>
<ul>
<li>An array of arrays.<br /><code>[ [1,2,3], [4,5,6] ]</code></li>
<li>A hash of arrays.<br /><code>{ :lucky => [77,42], :unlucky => [666,13] }</code></li>
<li>An array of hashes.<br /><code>[ {:cat=>"meow"}, {:dog=>"ruff"} ]</code></li>
<li>A hash of hashes.<br /><code>{ :best_in_life => {:enemies => "crushed"}, :worst => { :meatloaf => "old"} }</code></li>
</ul>
<p>And I realized that I hadn&#8217;t really played with a hash of hashes much.  And now I realize why.  It&#8217;s really pretty useless.  It&#8217;s hard to work with and the additional key really isn&#8217;t all that useful.  I found it much better to just denormalize the key into the data attributes.  Anyway, you can see what I mean by reading and running what&#8217;s below.</p>
<p>We&#8217;re going to create Captain Planet and the planeteers in a 2D hash of hashes and do some searching, iterating and other simple things.  This should illustrate also how an array of hashes is a bit better.  You&#8217;ll see halfway through the program we redefine the planeteers.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># search a 2d hash</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> spacer<span style="color:#006600; font-weight:bold;">&#40;</span>msg<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;-&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">50</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> msg
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;-&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">50</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># this is not really a good data structure but we'll use it anyway.</span>
planeteers = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:kwame</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;earth&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Ghana, Africa&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;LeVar Burton&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:wheeler</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;fire&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Brooklyn, NY&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Joey Dedio&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:linka</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;wind&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Soviet Union&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Kath Soucie&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:gi</span>       <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;water&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Thailand&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Janice Kawaye&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:ma_ti</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;heart&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Brazil&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Scott Menville&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
spacer <span style="color:#996600;">&quot;Here are our planeteers and their elements:&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> planeteers.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> planeteers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:element</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Find the fire planeteer:<span style="color:#000099;">\n</span>&quot;</span>
fire = planeteers.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> planeteers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:element</span><span style="color:#006600; font-weight:bold;">&#93;</span>==<span style="color:#996600;">&quot;fire&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; [{:kwame=&gt;false}, {:wheeler=&gt;true}, {:linka=&gt;false}, {:gi=&gt;false}, {:ma_ti=&gt;false}]</span>
&nbsp;
only_fire = fire.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>h<span style="color:#006600; font-weight:bold;">|</span> h.<span style="color:#9900CC;">values</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; {:wheeler=&gt;true}</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># print just one</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> only_fire.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">first</span>
&nbsp;
&nbsp;
spacer <span style="color:#996600;">&quot;Let's do this a bit cleaner with a better data structure.&quot;</span>
planeteers = <span style="color:#006600; font-weight:bold;">&#91;</span>
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;kwame&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;earth&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Ghana, Africa&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;LeVar Burton&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;wheeler&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;fire&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Brooklyn, NY&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Joey Dedio&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;linka&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;wind&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Soviet Union&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Kath Soucie&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;gi&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;water&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Thailand&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Janice Kawaye&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;ma_ti&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;heart&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Brazil&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:actor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Scott Menville&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
planeteers.<span style="color:#9900CC;">max_by</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; {:name=&gt;&quot;ma_ti&quot;, :element=&gt;&quot;heart&quot;, :from=&gt;&quot;Brazil&quot;, :actor=&gt;&quot;Scott Menville&quot;}</span>
&nbsp;
planeteers.<span style="color:#9900CC;">max_by</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:element</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; {:name=&gt;&quot;linka&quot;, :element=&gt;&quot;wind&quot;, :from=&gt;&quot;Soviet Union&quot;, :actor=&gt;&quot;Kath Soucie&quot;}</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Find the heart planeteer:&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> planeteers.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:element</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">&quot;heart&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">first</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># first we'll put a fake planeteer on the end for cpt planet</span>
planeteers <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;all&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:element</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;go planet&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
spacer <span style="color:#996600;">&quot;Let's summon Captain Planet!&quot;</span>
planeteers.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>planeteer<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{planeteer[:name].capitalize}: #{planeteer[:element].capitalize}!&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># pop off fake guy</span>
planeteers.<span style="color:#9900CC;">pop</span></pre></div></div>

<p>Here&#8217;s what it spits out:</p>
<pre>
--------------------------------------------------
Here are our planeteers and their elements:
--------------------------------------------------
{:kwame=>"earth"}
{:wheeler=>"fire"}
{:linka=>"wind"}
{:gi=>"water"}
{:ma_ti=>"heart"}

Find the fire planeteer:
wheeler

--------------------------------------------------
Let's do this a bit cleaner with a better data structure.
--------------------------------------------------
Find the heart planeteer:
ma_ti

--------------------------------------------------
Let's summon Captain Planet!
--------------------------------------------------
Kwame: Earth!
Wheeler: Fire!
Linka: Wind!
Gi: Water!
Ma_ti: Heart!
All: Go planet!
</pre>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/12/04/hash-of-hashes-and-captain-planet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HBase Shell Color</title>
		<link>http://squarism.com/2011/12/03/hbase-shell-color/</link>
		<comments>http://squarism.com/2011/12/03/hbase-shell-color/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 00:59:29 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1418</guid>
		<description><![CDATA[Since the hbase shell is irb, I wanted to get color output because that&#8217;s what I&#8217;m used to. Although the appropriate place to put this is in an .irbrc file, that would conflict with any ruby development environment already on the system and luckily jruby and hbase don&#8217;t seem to invoke it anyway. First find [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2011/11/hbase_color.png" alt="" title="hbase_color" width="586" height="365" class="aligncenter size-full wp-image-1455" /><br />
Since the hbase shell is irb, I wanted to get color output because that&#8217;s what I&#8217;m used to.  Although the appropriate place to put this is in an .irbrc file, that would conflict with any ruby development environment already on the system and luckily jruby and hbase don&#8217;t seem to invoke it anyway.</p>
<p>First find a copy of wirble.  If you don&#8217;t have it anywhere, <a href="https://raw.github.com/blackwinter/wirble/master/lib/wirble.rb">download it from github</a>:</p>
<p><code><br />
cd ${hbase_home}/lib/ruby<br />
wget https://raw.github.com/blackwinter/wirble/master/lib/wirble.rb<br />
</code></p>
<p>Now edit ${hbase_home}/bin/hirb.rb.  Add to the end but above IRB.start</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">begin</span>
  <span style="color:#008000; font-style:italic;"># load wirble</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'wirble'</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># start wirble (with color)</span>
  Wirble.<span style="color:#9900CC;">init</span>
  Wirble.<span style="color:#9900CC;">colorize</span>
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">LoadError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> err
  warn <span style="color:#996600;">&quot;Couldn't load Wirble: #{err}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
IRB.<span style="color:#9900CC;">conf</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:HISTORY_FILE</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;#{ENV['HOME']}/.hbase-history&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># add right before end but above this line</span>
IRB.<span style="color:#9900CC;">start</span></pre></div></div>

<p>Now when you start hbase shell, you&#8217;ll have lovely color output.  Why would you want this?  I don&#8217;t know.  You probably don&#8217;t want it.  But I was happy to understand how the hbase shell works.  It&#8217;s just jruby irb that loads hirb automatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/12/03/hbase-shell-color/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Schemaless Data Collection</title>
		<link>http://squarism.com/2011/11/25/schemaless-data-collection/</link>
		<comments>http://squarism.com/2011/11/25/schemaless-data-collection/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 15:32:54 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1421</guid>
		<description><![CDATA[I&#8217;ve had this idea for schemaless data collection for a while now. It seems like everyone is trying to ETL data in. Inevitably, people start writing mappers programs and documentation trying to build a massive Rosetta stone. &#8220;They call person_name name? We&#8217;ll call everything name. Let&#8217;s write this all down. Person_name = name, so say [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2011/11/schemaless.png" alt="" title="schemaless" width="442" height="119" class="aligncenter size-full wp-image-1430" /></p>
<p>I&#8217;ve had this idea for schemaless data collection for a while now.  It seems like everyone is trying to ETL data in.  Inevitably, people start writing mappers programs and documentation trying to build a massive Rosetta stone.  <em>&#8220;They call person_name name?  We&#8217;ll call everything name.  Let&#8217;s write this all down.  Person_name = name, so say we all.&#8221;</em>  What happens is a lot of investigation and work in determining their ENTIRE schema just to make a copy of it.  In the case of XML parsing, sometimes I actually do need to know what the entire source looks like just so I can loop through it.  What a pain.  Not to mention if the source changes formats, I have to do all this work to re-understand their schema and change my mappers to reflect their change.  Maybe I even have to do a mass migration on my end to bring everything up to date.  Schemaless data collection will let you copy the data when the source changes and even be able to historically tell you when the schema changed.  In an RDBMS, this is impossible without blobs or something else horrible.</p>
<p>What this example shows is simply the collection of the data.  But the advantage here, I will show that any kind of querying can be done later very easily.  What I won&#8217;t show is that normalization can be done in parallel and in batches later too and the whole thing lives in a horizontally scalable database.  Of course, the catch is, you need keys and a structured data source to start with.  This won&#8217;t work with CSV and simple formats.</p>
<p>For example, if we wanted to load XML files into a database.  MongoDB is great for this because it can possibly make coding dead simple.  For each attribute and children, create attributes and children in the database.</p>
<p>In a normal database, I would have to parse out the XML file and create normalized rows all over the place or use blobs.  Of course blobs are useless for search.  Let me show you an example.</p>
<p>First, let&#8217;s take a look at the XML returned by a <a href="http://en.wikipedia.org/wiki/Special:Export/Ford_Motor">Wikipedia exporter URL</a> (trimmed the XSD line a bit for readability):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mediawiki</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.mediawiki.org/xml/export-0.5/&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.mediawiki.org/xml/export-0.5.xsd&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;0.5&quot;</span> <span style="color: #000066;">xml:lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;siteinfo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sitename<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Wikipedia<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sitename<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;base<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://en.wikipedia.org/wiki/Main_Page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/base<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;generator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MediaWiki 1.18wmf1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/generator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>first-letter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespaces<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;-2&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Media<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;-1&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Special<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>User<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>User talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Wikipedia<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Wikipedia talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;6&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>File<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;7&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>File talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;8&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>MediaWiki<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;9&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>MediaWiki talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Template<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;11&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Template talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;12&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Help<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;13&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Help talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;14&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Category<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;15&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Category talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Portal<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;101&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Portal talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;108&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Book<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;109&quot;</span> <span style="color: #000066;">case</span>=<span style="color: #ff0000;">&quot;first-letter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Book talk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespace<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespaces<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/siteinfo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Ford Motor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>255240<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;redirect</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;revision<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>16130856<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;timestamp<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2003-06-30T02:32:03Z<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/timestamp<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;contributor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Infrogmation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4444<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/contributor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;minor</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;comment<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>redir<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/comment<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;text</span> <span style="color: #000066;">xml:space</span>=<span style="color: #ff0000;">&quot;preserve&quot;</span> <span style="color: #000066;">bytes</span>=<span style="color: #ff0000;">&quot;32&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>#REDIRECT [[Ford Motor Company]]<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/revision<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mediawiki<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Even though this page is just a redirect (see the text element at the end in mediawiki markup), it&#8217;s still very long.  For a multitude of posts or documents, creating a mapper and tightly handling the document might be very annoying.  Even worse, we might delay sucking data in because there is so much mapping work to do.  We might even start writing documents detailing the source format, what we will call the attributes internally and document schema changes as they occur.</p>
<p>What I propose is to forget all that mapping and just load the document as-is.  We will use a document database (MongoDB) to make this magic happen.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># we are going to intentionally use the vanilla mongo driver</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mongo'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'nokogiri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/core_ext'</span> <span style="color:#008000; font-style:italic;"># from rails</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">include</span> Mongo
pages = Connection.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'localhost'</span>, <span style="color:#006666;">27017</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">db</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'loadtest'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'pages'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
wikipedia_page = <span style="color:#996600;">&quot;http://en.wikipedia.org/wiki/Special:Export/Ford_Motor&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># noblanks is magic here?  had problems without it</span>
doc = <span style="color:#6666ff; font-weight:bold;">Nokogiri::XML</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>wikipedia_page<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span> config.<span style="color:#9900CC;">noblanks</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
page = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">from_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span>doc.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>    <span style="color:#008000; font-style:italic;"># here's the magical method from rails</span>
pages.<span style="color:#9900CC;">insert</span> page</pre></div></div>

<p>Ok this is pretty cool.  In 10 lines of ruby, I&#8217;m downloading an XML file and inserting it into a new collection called &#8216;pages&#8217; in a database that hasn&#8217;t even been created (as long as mongodb is running).  Great!  But it quickly falls apart.</p>
<p>If you run it again, you now have two documents (rows) in Mongo.  Boo.  Not to mention, if you actually query mongo you see this (trimmed the XSD line a bit for readability):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">pages</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #3366CC;">&quot;_id&quot;</span><span style="color: #339933;">:</span>ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4ec6c07e5a498d64a1000001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
   <span style="color: #3366CC;">&quot;mediawiki&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;xmlns&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://www.mediawiki.org/xml/export-0.5/&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;xmlns:xsi&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;xsi:schemaLocation&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://www.mediawiki.org/xml/export-0.5.xsd&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;version&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;0.5&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;xml:lang&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;en&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;siteinfo&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
         <span style="color: #3366CC;">&quot;sitename&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Wikipedia&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;base&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://en.wikipedia.org/wiki/Main_Page&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;generator&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;MediaWiki 1.18wmf1&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;case&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;first-letter&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;namespaces&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;namespace&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
               <span style="color: #3366CC;">&quot;Media&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Special&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #009900;">&#123;</span>
                  <span style="color: #3366CC;">&quot;key&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;case&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;first-letter&quot;</span>
               <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;User&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;User talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Wikipedia&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Wikipedia talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;File&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;File talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;MediaWiki&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;MediaWiki talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Template&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Template talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Help&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Help talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Category&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Category talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Portal&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Portal talk&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Book&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Book talk&quot;</span>
            <span style="color: #009900;">&#93;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
         <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Ford Motor&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;255240&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;redirect&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;revision&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;16130856&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;timestamp&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2003-06-30T02:32:03Z&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;contributor&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
               <span style="color: #3366CC;">&quot;username&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Infrogmation&quot;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;4444&quot;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;minor&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;comment&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;redir&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;text&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#REDIRECT [[Ford Motor Company]]&quot;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There&#8217;s a whole lot of metadata in there and really all I care about is the content (maybe).  So in some cases, you might want to filter incoming data.  I have to actually look at my data and pick which attributes I want.  Now I&#8217;m tightly bound to the source document and have to worry about it changing etc.</p>
<p>But let&#8217;s do it anyway.  All we have to do is change one line:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">pages.<span style="color:#9900CC;">insert</span> page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;mediawiki&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;page&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Now our inserted document looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">pages</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #3366CC;">&quot;_id&quot;</span><span style="color: #339933;">:</span>ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4ec6c1775a498d64f2000001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
   <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Ford Motor&quot;</span><span style="color: #339933;">,</span>
   <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;255240&quot;</span><span style="color: #339933;">,</span>
   <span style="color: #3366CC;">&quot;redirect&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
   <span style="color: #3366CC;">&quot;revision&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;16130856&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;timestamp&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;2003-06-30T02:32:03Z&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;contributor&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
         <span style="color: #3366CC;">&quot;username&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Infrogmation&quot;</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;4444&quot;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;minor&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;comment&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;redir&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;text&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#REDIRECT [[Ford Motor Company]]&quot;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of course, we had to clear out the pages collection and re-run it.  That&#8217;s just because we haven&#8217;t written any logic yet to check for existence yet.  But let&#8217;s take a break here and talk about what we could do even with this piddly little bit of 10 lines of Ruby running.  We can query:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">pages</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'title'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4ec6c1775a498d64f2000001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;title&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Ford Motor&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4ecafffd5a498d0136000001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;title&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Nissan&quot;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here we are just showing that we have two pages from Wikipedia stored.  The title:1 is like <code>SELECT title FROM pages;</code> in SQL.  So if we wanted to search on attributes, it&#8217;s pretty easy:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">pages</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> title<span style="color: #339933;">:/^</span>N<span style="color: #339933;">/</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;4ecafffd5a498d0136000001&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;title&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Nissan&quot;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s pretty forgiving on the key quotes.</p>
<p>In the next part, we&#8217;ll dive into handling updates and other formats.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/11/25/schemaless-data-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rspec output formats</title>
		<link>http://squarism.com/2011/11/17/rspec-output-formats/</link>
		<comments>http://squarism.com/2011/11/17/rspec-output-formats/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 22:59:16 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1403</guid>
		<description><![CDATA[Some examples of rspec2 output formats. If you are using Guard and Spork to speed up your test suite, you pass &#8211;format blah in the Guardfile. For example: guard 'rspec', :version =&#62; 2, :cli =&#62; '--drb --color --format doc' do watch&#40;%r&#123;^spec/.+_spec\.rb$&#125;&#41; ... end You can specify multiple formats with --format one --format two. Anyway, here [...]]]></description>
			<content:encoded><![CDATA[<p>Some examples of rspec2 output formats.  If you are using Guard and Spork to speed up your test suite, you pass &#8211;format blah in the Guardfile.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">guard <span style="color:#996600;">'rspec'</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>, <span style="color:#ff3333; font-weight:bold;">:cli</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'--drb --color --format doc'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  watch<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>^spec<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">+</span>_spec\.<span style="color:#9900CC;">rb</span>$<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can specify multiple formats with <code>--format one --format two</code>.</p>
<p>Anyway, here are some shots of what the output looks like:</p>
<p><code>--format doc</code><br />
<img src="http://squarism.com/wp-content/uploads/2011/11/rspec_format_doc.png" alt="" title="rspec_format_doc" width="480" height="113" class="aligncenter size-full wp-image-1404" /></p>
<p><code>--format progress</code><br />
<img src="http://squarism.com/wp-content/uploads/2011/11/rspec_format_progress.png" alt="" title="rspec_format_progress" width="348" height="69" class="aligncenter size-full wp-image-1407" /></p>
<p><code>--format nested --format progress</code><br />
<img src="http://squarism.com/wp-content/uploads/2011/11/rspec_format_progress_nested.png" alt="" title="rspec_format_progress_nested" width="487" height="130" class="aligncenter size-full wp-image-1408" /></p>
<p><code>--format nested</code><br />
<img src="http://squarism.com/wp-content/uploads/2011/11/rspec_format_nested.png" alt="" title="rspec_format_nested" width="485" height="91" class="aligncenter size-full wp-image-1406" /></p>
<p><code>--format html</code><br />
<img src="http://squarism.com/wp-content/uploads/2011/11/rspec_format_html.png" alt="" title="rspec_format_html" width="483" height="103" class="aligncenter size-full wp-image-1405" /></p>
<p>HTML format is also the same as the Textmate format.  I couldn&#8217;t get the output to go to a file like the documentation says.  Maybe it hasn&#8217;t been updated for rspec2?</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/11/17/rspec-output-formats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Messing with Method Missing</title>
		<link>http://squarism.com/2011/11/12/messing-with-method-missing/</link>
		<comments>http://squarism.com/2011/11/12/messing-with-method-missing/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 16:46:00 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1377</guid>
		<description><![CDATA[We&#8217;re going to play with method_missing and less so, monkey patching. All of this code is designed to work in one source file or irb session. It will run procedurally from beginning to end. So you can copy it in pieces into a single .rb file or follow along in irb. No need to break [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2011/11/method_missing-150x150.png" alt="" title="method_missing" width="150" height="150" class="aligncenter size-thumbnail wp-image-1400" /><br />
We&#8217;re going to play with method_missing and less so, monkey patching.  All of this code is designed to work in one source file or irb session.  It will run procedurally from beginning to end.  So you can copy it in pieces into a single .rb file or follow along in irb.  No need to break it out into separate files or restart irb.  </p>
<p>We&#8217;ll start with a simple person class that is initialized with a name and an age.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Person
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:age</span>, <span style="color:#ff3333; font-weight:bold;">:problems</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>name, age<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@name</span> = name
    <span style="color:#0066ff; font-weight:bold;">@age</span> = age
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Creating a person is as simple as passing &#8220;James&#8221; and 99 as arguments.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> Person.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;James&quot;</span>, <span style="color:#006666;">99</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
<span style="color:#008000; font-style:italic;"># =&gt; #&lt;Person:0x007f9e2a136878 @name=&quot;James&quot;, @age=99&gt;</span></pre></div></div>

<p>Now I have a Person object as expected.  The twist comes in when you look at this line by itself and realize that you can&#8217;t get what 99 is.  Is it the age?  Is it the problems?  Of course, you might opt to simply get rid of the constructor and set the instance variables manually.  But let&#8217;s try to do something more fancy.</p>
<p>First, we&#8217;ll try to invoke a method that doesn&#8217;t exist.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">begin</span>
  Person.<span style="color:#9900CC;">create_with_name_and_problems</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;James&quot;</span>, <span style="color:#006666;">99</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># =&gt; undefined method `create_with_name_and_problems'</span>
  <span style="color:#008000; font-style:italic;">#    for Person:Class (NoMethodError)</span>
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">NoMethodError</span>
  <span style="color:#008000; font-style:italic;"># just continue</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We will get an exception here.  For the sake of our single source file, we&#8217;ll catch the exception and continue.</p>
<p>So when we try to call #create_with_name_and_problems so that 99 is clearly a problem and not an age argument, the method doesn&#8217;t exist.  We could create that method but that&#8217;s not very scalable, we&#8217;d have to create every permutation of possible construction options.</p>
<p>Instead what we are going to do is use method_missing to handle calls to unknown methods and at the same time set the instance variables and return an object.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Person
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">method_missing</span><span style="color:#006600; font-weight:bold;">&#40;</span>meth, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;OH NO!  No method!&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> meth.<span style="color:#9900CC;">to_s</span> =~ <span style="color:#006600; font-weight:bold;">/</span>^create_with_<span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">run_create_with_method</span><span style="color:#006600; font-weight:bold;">&#40;</span>$1, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#9966CC; font-weight:bold;">super</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> respond_to?<span style="color:#006600; font-weight:bold;">&#40;</span>meth, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">meth</span>.<span style="color:#9900CC;">to_s</span> =~ <span style="color:#006600; font-weight:bold;">/</span>^create_with_.<span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#9966CC; font-weight:bold;">super</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">run_create_with_method</span><span style="color:#006600; font-weight:bold;">&#40;</span>attrs, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    attrs = attrs.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'_and_'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># #transpose will zip the two arrays together like so:</span>
    <span style="color:#008000; font-style:italic;">#   [[:a, :b, :c], [1, 2, 3]].transpose</span>
    <span style="color:#008000; font-style:italic;">#   # =&gt; [[:a, 1], [:b, 2], [:c, 3]]</span>
    attrs_with_args = <span style="color:#006600; font-weight:bold;">&#91;</span>attrs, args<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">transpose</span>
    attributes = <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span>attrs_with_args<span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">new</span>
    attributes.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">instance_variable_set</span> <span style="color:#996600;">&quot;@#{a}&quot;</span>, attributes<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#CC0066; font-weight:bold;">p</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>First we reopen the Person class (monkey patch) and redefine a parameter-less initialize method.  Next we create a method_missing method on the class object that looks for any method that starts with &#8220;create_with_&#8221;.  If it does then it creates a new object with the correct instance variables set.  Finally, the respond_to? method ensures that our Person class is advertising that #create_with_ methods are valid to outside calls.</p>
<p>Ok, so now our Person object is ready to be used.  We can create James again this time with a name and a number of problems.  We can even create a person with all three attributes and vary the order.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> Person.<span style="color:#9900CC;">create_with_name_and_problems</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;James&quot;</span>,<span style="color:#006666;">99</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
<span style="color:#008000; font-style:italic;"># &lt;Person:0x007ffc8a835250 @name=&quot;James&quot;, @problems=99&gt;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> Person.<span style="color:#9900CC;">create_with_age_and_problems_and_name</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">55</span>, <span style="color:#006666;">99</span>, <span style="color:#996600;">&quot;Jay-Z&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
<span style="color:#008000; font-style:italic;"># &lt;Person:0x007ffc8b0ae990 @name=&quot;Jay-Z&quot;, @age=55, @problems=99&gt;</span></pre></div></div>

<p>So in actuality, this is a bit contrived.  It&#8217;s cool to have these dynamic methods created for us but doing this way is a little too much work just to get parameterized constructors.  The better way would be to use a hash for initialization.  See below:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Person
  attr_reader <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:age</span>, <span style="color:#ff3333; font-weight:bold;">:problems</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize args
    args.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span>
      instance_variable_set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@#{k}&quot;</span>, v<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> v.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;James&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:age</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">99</span>, <span style="color:#ff3333; font-weight:bold;">:problems</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">99</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># &lt;Person:0x007fda421044f0 @name=&quot;James&quot;, @age=99, @problems=99&gt;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span>       <span style="color:#008000; font-style:italic;"># James</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">age</span>        <span style="color:#008000; font-style:italic;"># 99</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">problems</span>   <span style="color:#008000; font-style:italic;"># 99</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/11/12/messing-with-method-missing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lego mount for Arduino</title>
		<link>http://squarism.com/2011/09/09/lego-mount-for-arduino/</link>
		<comments>http://squarism.com/2011/09/09/lego-mount-for-arduino/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 23:13:17 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1345</guid>
		<description><![CDATA[I got to thinking (that&#8217;s another story) about the old Technics set that I had when I was a kid and how useful it would be if I could tear down and build things without having to hacksaw stuff (i.e. like with the Microrax stuff). We had major flooding recently (after an earthquake and a [...]]]></description>
			<content:encoded><![CDATA[<p>I got to thinking (that&#8217;s another story) about the old Technics set that I had when I was a kid and how useful it would be if I could tear down and build things without having to hacksaw stuff (i.e. like with the <a href="http://www.microrax.com/">Microrax</a> stuff).  We had major flooding recently (after an earthquake and a hurricane) and the closed roads funneled me to a toy store.  Was this a sign?  I&#8217;m not sure.  All I know is I have a Mindstorms kit now.  :)</p>
<p>So I built an Arduino frame using nothing but the parts from the Mindstorms kit (it&#8217;s all just Technics parts).  It&#8217;s pretty sturdy and the little rubber ends hold the board down pretty good.  I figure it&#8217;s good enough to put on wheels or a quad-ped bot.  I don&#8217;t know if I&#8217;d send it up in the air.  I&#8217;d probably want to add a few more cross bars in there.  Anyway, I did a lot of refactoring (if you could call it that) and I think this design is pretty good.  Here&#8217;s the money shot with the rest of the build shots so you can copy it.  I&#8217;m fresh brand-new to the Lego scene so leave some comments if I did something stupid.</p>
<p><img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_1-580x433.png" alt="" title="lego_arduino_1" width="580" height="433" class="aligncenter size-large wp-image-1346" /></p>
<p><span id="more-1345"></span></p>
<p>Top down without the board.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_2-580x433.png" alt="" title="lego_arduino_2" width="580" height="433" class="aligncenter size-large wp-image-1347" /></p>
<p>Bottom face without the board.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_3-580x433.png" alt="" title="lego_arduino_3" width="580" height="433" class="aligncenter size-large wp-image-1348" /></p>
<p>Room and support for plugs.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_4-580x433.png" alt="" title="lego_arduino_4" width="580" height="433" class="aligncenter size-large wp-image-1349" /></p>
<p>Doesn&#8217;t shake loose.<br />
<img src="http://squarism.com/wp-content/uploads/2011/09/lego_arduino_5-580x433.png" alt="" title="lego_arduino_5" width="580" height="433" class="aligncenter size-large wp-image-1350" /></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/09/09/lego-mount-for-arduino/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

