<?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 &#187; Ruby</title>
	<atom:link href="http://squarism.com/category/development/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://squarism.com</link>
	<description>until lambs become lions</description>
	<lastBuildDate>Mon, 07 May 2012 17:52:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Ruby Resources</title>
		<link>http://squarism.com/2012/05/05/ruby-resources/</link>
		<comments>http://squarism.com/2012/05/05/ruby-resources/#comments</comments>
		<pubDate>Sat, 05 May 2012 15:50:07 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1496</guid>
		<description><![CDATA[destroyallsoftware.com $9 for his entire back catalog. You can subscribe monthly or just sign up, download all of them and then cancel. tryruby.org 15 minute ruby intro. Recently bought by the code school guys. codeschool.com Has a free 2 hour rails tutorial called rails for zombies. This is really fun but longer. It&#8217;s totally interactive. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://destroyallsoftware.com">destroyallsoftware.com</a><br />
$9 for his entire back catalog.  You can subscribe monthly or just sign up, download all of them and then cancel.</p>
<p><a href="http://tryruby.org">tryruby.org</a><br />
15 minute ruby intro.  Recently bought by the code school guys.</p>
<p><a href="http://codeschool.com">codeschool.com</a><br />
Has a free 2 hour rails tutorial called rails for zombies.  This is really fun but longer.  It&#8217;s totally interactive.  Very well done.  They have non-free classes too.  Focuses on rails 3 and some advanced stuff.</p>
<p><a href="http://railscasts.com">railscasts.com</a><br />
Not very hand-hold-y.  I regularly watch this site just to learn new things and watch someone work.  These are short in length and very focused.</p>
<p><a href="http://rubykoans.com">rubykoans.com</a><br />
Ruby Koans are practice exercises/meditations on different aspects of Ruby.  There are many similar projects (Python Koans etc).  It&#8217;s like an interactive cookbook.  Very valuable to do even if you&#8217;re comfortable with Ruby.  It&#8217;s a project that has failures built into it and it&#8217;s your job to fix them.  I did a blog post on it.  I&#8217;m not explaining this very well.  It&#8217;s a bunch of tests set up to fail and you fix the first test case and it continues on.  For example, you might set true = true and then the test suite will have 1 test passing and give you a progress bar.  Very cool and easy to pick up and put down.  Good for reference afterwards.</p>
<p><a href="http://confreaks.net">confreaks.net</a><br />
Lots of conference videos organized by events.  The HTML5 player sucks.  Use the flash one, even that one is a bit picky with pausing and other stuff.</p>
<p><a href="http://github.com/squarism/quickie_mart/blob/master/README.md">quickie_mart 20 min app</a><br />
A plug!  This is a talk that I did.  The README is very short but has all the steps to build a store rails app.  The repo is the completed app.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2012/05/05/ruby-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB CSV importing</title>
		<link>http://squarism.com/2012/05/03/mongodb-csv-importing/</link>
		<comments>http://squarism.com/2012/05/03/mongodb-csv-importing/#comments</comments>
		<pubDate>Thu, 03 May 2012 15:49:51 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1575</guid>
		<description><![CDATA[Following this tutorial over at mongovue about mapreduce in mongodb. They have you export data from MySQL to MongoDB using their .NET tool. I&#8217;m on a Mac so here&#8217;s what I did instead. If you followed the instructions, you have the .txt files (which are CSVs) in your MySQL data directory (weird but ok). Importing [...]]]></description>
			<content:encoded><![CDATA[<p>Following this tutorial over at <a href="http://www.mongovue.com/2010/11/03/yet-another-mongodb-map-reduce-tutorial/">mongovue</a> about mapreduce in mongodb.  They have you export data from MySQL to MongoDB using their .NET tool.  I&#8217;m on a Mac so here&#8217;s what I did instead.</p>
<p>If you followed the instructions, you have the .txt files (which are CSVs) in your MySQL data directory (weird but ok).  Importing CSV is really easy in MongoDB.  Just make sure you are in the directory with the .txt files.</p>
<p><code><br />
mongoimport -d geobytes -c cities --type csv --file cities.txt --headerline<br />
mongoimport -d geobytes -c regions --type csv --file regions.txt --headerline<br />
mongoimport -d geobytes -c dmas --type csv --file dmas.txt --headerline<br />
mongoimport -d geobytes -c countries --type csv --file countries.txt --headerline<br />
</code></p>
<p>Now you can continue with the tutorial.<br />
<code><br />
> db.cities.findOne();<br />
{<br />
	"_id" : ObjectId("4fa2a734779f0ea93dd13df6"),<br />
	"CityId" : 42231,<br />
	"CountryID" : 1,<br />
	"RegionID" : 833,<br />
	"City" : "Herat",<br />
	"Latitude" : 34.333,<br />
	"Longitude" : 62.2,<br />
	"TimeZone" : "+04:30",<br />
	"DmaId" : 0,<br />
	"Code" : "HERA"<br />
}<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2012/05/03/mongodb-csv-importing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Spork nil:NilClass fix</title>
		<link>http://squarism.com/2012/03/09/a-spork-nilnilclass-fix/</link>
		<comments>http://squarism.com/2012/03/09/a-spork-nilnilclass-fix/#comments</comments>
		<pubDate>Sat, 10 Mar 2012 00:40:08 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1571</guid>
		<description><![CDATA[This might not be your issue. I had a really weird problem that was ungoogleable so I thought I&#8217;d post it. With a combination of spork, guard and rspec, none of my testing stuff was working. The spork prefork block was throwing this error. Using RSpec Preloading Rails environment undefined method `gsub' for nil:NilClass (NoMethodError) [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2012/03/wtf_is_going_on.png" alt="" title="wtf_is_going_on" width="272" height="31" class="alignright size-full wp-image-1572" /><br />
This might not be your issue.  I had a really weird problem that was ungoogleable so I thought I&#8217;d post it.</p>
<p>With a combination of spork, guard and rspec, none of my testing stuff was working.  The spork prefork block was throwing this error.<br />
<code><br />
Using RSpec<br />
Preloading Rails environment<br />
undefined method `gsub' for nil:NilClass (NoMethodError)<br />
</code></p>
<p>The solution was to comment all my test gems one at a time.  And then I realized that my Ubuntu edited version of my Gemfile was conflicting with my Mac edited version of this same file.</p>
<p>So I made my Gemfile a little smarter.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
group <span style="color:#ff3333; font-weight:bold;">:test</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  gem <span style="color:#996600;">'rspec'</span>
  gem <span style="color:#996600;">'rspec-rails'</span>
  gem <span style="color:#996600;">'spork'</span>
  <span style="color:#008000; font-style:italic;"># </span>
  gem <span style="color:#996600;">'rb-inotify'</span> <span style="color:#9966CC; font-weight:bold;">if</span> RUBY_PLATFORM.<span style="color:#9900CC;">downcase</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;linux&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  gem <span style="color:#996600;">'rb-fsevent'</span> <span style="color:#9966CC; font-weight:bold;">if</span> RUBY_PLATFORM.<span style="color:#9900CC;">downcase</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;darwin&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  gem <span style="color:#996600;">'guard'</span>
  gem <span style="color:#996600;">'guard-rspec'</span>
  gem <span style="color:#996600;">'guard-spork'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then things started to work again.  With any weird bugs in Rails, try commenting out gems one at a time.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2012/03/09/a-spork-nilnilclass-fix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgrading Rbenv</title>
		<link>http://squarism.com/2012/02/27/upgrading-rbenv/</link>
		<comments>http://squarism.com/2012/02/27/upgrading-rbenv/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 02:53:22 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1560</guid>
		<description><![CDATA[I&#8217;ve recently switched from rvm to rbenv on most of my dev boxes. I loved RVM to death, no offense to all the hard work that Wayne did. He&#8217;s a great guy and I listen to him talk on podcasts. I just think RVM is a bit heavy handed in some things and dealing with [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2012/02/rbenv.png" alt="" title="rbenv" width="140" height="140" class="alignright size-full wp-image-1566" style="padding:10px"/><br />
I&#8217;ve recently switched from rvm to rbenv on most of my dev boxes.  I loved RVM to death, no offense to all the hard work that Wayne did.  He&#8217;s a great guy and I listen to him talk on podcasts.  I just think RVM is a bit heavy handed in some things and dealing with readline failures (despite doing the same steps as I&#8217;ve done many times before) and other things was getting tiresome.  I&#8217;m not sure how complete all my testing is (such as Textmate or Sublime Text 2 support) so a bit of this is not &#8220;I&#8217;ve converted!&#8221; it&#8217;s more of a &#8220;I&#8217;m currently converting&#8221;.</p>
<p>The biggest change is how to pull new rubies in.  What I mean is, let&#8217;s say a future version comes out.  Let&#8217;s call it Ruby 14.0 so that this blog post doesn&#8217;t look dated for a really long time (#wat).  If you wanted to update your global ruby with RVM, it&#8217;d go something like this: 1) rvm get latest 2) rvm install ruby-14.0  Then you&#8217;d migrate your gemsets or play with .rvmrc files.</p>
<p>Rbenv is a bit easier in this regard but it requires slightly more typing.  I&#8217;ll also show you a little trick on how to incorporate some plugins neatly.</p>
<p><code><br />
> rbenv versions<br />
  1.8.7-p357<br />
* 1.9.3-p0 (set by /home/user/.rbenv/version)<br />
</code></p>
<p>Bah.  We already dated this blog post.  Anyway.  1.9.3-p125 is currently out.  So let&#8217;s try to pull it in.</p>
<p><code><br />
> rbenv install 1.9.3-p125<br />
ruby-build: definition not found: 1.9.3-p125<br />
</code></p>
<p>&#8220;What?  It&#8217;s out!  I even just did this on another rbenv install!  What is going on?&#8221;<br />
[Keep raging, don't ship apps.]</p>
<p>So what&#8217;s happening here is the rbenv is so old that it doesn&#8217;t know what p125 is.  So let&#8217;s update our rbenv install.</p>
<p><code><br />
> cd ~/.rbenv<br />
> git pull<br />
</code></p>
<p>Great.  But that&#8217;s not the equivalent of &#8216;rvm get latest&#8217; because we are using ruby-build to do the &#8216;rbenv install&#8217;.  Now the default documentation has you checkout ruby-build in your home directory.  So if you copied and pasted (like me) then you have a ~/ruby-build dir.  Let&#8217;s move that to ~/.rbenv/plugins (make sure plugins is a directory).  If you are using rbenv-gemsets to mimic rvm gemsets then you already have a plugin directory.</p>
<p><code><br />
> ls ~/.rbenv/plugins<br />
rbenv-gemset  ruby-build</p>
<p>> cd ~/.rbenv/plugins/ruby-build<br />
git pull<br />
</code></p>
<p>If you installed <a href="https://github.com/sstephenson/ruby-build">ruby-build</a> into /usr/local then you can leave off the PREFIX variable.<br />
<code><br />
> PREFIX=~/local ./install.sh<br />
</code></p>
<p>If you installed it into your home, you&#8217;ll need to modify your PATH variable:<br />
# for ruby build and other PREFIX overridden builds<br />
<code><br />
if [ -d "$HOME/local/bin" ] ; then<br />
    PATH="$HOME/local/bin:$PATH"<br />
fi<br />
</code></p>
<p>If you want it in /usr/local, then leave off the above if statement etc and just run sudo ./install.sh like the ruby-build docs say.</p>
<p>Now rbenv knows what you&#8217;re talking about:<br />
<code><br />
> rbenv install --definitions 2>&#038;1 | grep p125<br />
</code></p>
<p>And now you can install the new ruby and set it to be your default.  The good thing here is, unless you&#8217;re switching ruby versions, you don&#8217;t need to update all your gemset files like you need to in RVM.  So for me, there&#8217;s less impact when keeping Ruby up-to-date.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2012/02/27/upgrading-rbenv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting Defaults in Ruby</title>
		<link>http://squarism.com/2012/02/21/setting-defaults-in-ruby/</link>
		<comments>http://squarism.com/2012/02/21/setting-defaults-in-ruby/#comments</comments>
		<pubDate>Wed, 22 Feb 2012 04:52:28 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1504</guid>
		<description><![CDATA[Let&#8217;s start out with a plain old method. def hello puts &#34;Hi!&#34; end Now let&#8217;s un-hardcode that string in the puts by adding a parameter. def hello&#40;greeting=&#34;Hi!&#34;&#41; puts greeting end &#160; &#62;&#62; hello Hi! &#62;&#62; hello&#40;&#34;Hola!&#34;&#41; Hola! Great. We have a default String. But what about something more complex. What if we want a hash [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2012/02/ruby_defaults.png" alt="" title="ruby_defaults" width="709" height="202" class="aligncenter size-full wp-image-1557" /><br />
Let&#8217;s start out with a plain old method.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> hello
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hi!&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now let&#8217;s un-hardcode that string in the puts by adding a parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> hello<span style="color:#006600; font-weight:bold;">&#40;</span>greeting=<span style="color:#996600;">&quot;Hi!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> greeting
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> hello
Hi!
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> hello<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Hola!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
Hola!</pre></div></div>

<p>Great.  We have a default String.  But what about something more complex.  What if we want a hash of options.  Say we have a little piece of an IRC client.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> connect<span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  defaults = <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:server</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;irc.freenode.net&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
  options = defaults.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Connecting to #{options[:server]} ...&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">&lt;</span>pre lang=<span style="color:#996600;">&quot;ruby&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>
&nbsp;
Now <span style="color:#9966CC; font-weight:bold;">when</span> we use it like this, we can connect to a default server <span style="color:#9966CC; font-weight:bold;">or</span> override it.
<span style="color:#006600; font-weight:bold;">&lt;</span>pre lang=<span style="color:#996600;">&quot;ruby&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> connect
Connecting to irc.<span style="color:#9900CC;">freenode</span>.<span style="color:#9900CC;">net</span> ...
&nbsp;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> connect<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:server <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;irc.efnet.net&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
Connecting to irc.<span style="color:#9900CC;">efnet</span>.<span style="color:#9900CC;">net</span> ...</pre></div></div>

<p>Now a more complicated example.  All we&#8217;re doing here is loading defaults from a YAML file and doing the same thing as before.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'yaml'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Preferences
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#9966CC; font-weight:bold;">if</span> !<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;preferences.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># example file</span>
      options = <span style="color:#006600; font-weight:bold;">&#123;</span>:server <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;irc.efnet.net&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">save</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0066ff; font-weight:bold;">@values</span> = <span style="color:#CC00FF; font-weight:bold;">YAML</span>::<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;preferences.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_hash
    <span style="color:#0066ff; font-weight:bold;">@values</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> save!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
    preferences = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;preferences.yml&quot;</span>, <span style="color:#996600;">&quot;w&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
      f.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>options.<span style="color:#9900CC;">to_yaml</span><span style="color:#006600; font-weight:bold;">&#41;</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:#9966CC; font-weight:bold;">def</span> connect<span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  defaults = <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:server</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;irc.freenode.net&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:username</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;CHANGE-USER-NAME, see README.txt&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:channel</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#chat&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
  options = defaults.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Connecting to #{options[:server]} as #{options[:username]}...&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
<span style="color:#008000; font-style:italic;"># Main</span>
prefs = Preferences.<span style="color:#9900CC;">new</span>
&nbsp;
options = <span style="color:#006600; font-weight:bold;">&#123;</span>:channel <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#meow&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
connect<span style="color:#006600; font-weight:bold;">&#40;</span>prefs.<span style="color:#9900CC;">to_hash</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:username</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Bob&quot;</span>
options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:server</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;irc.efnet.net&quot;</span>
prefs.<span style="color:#9900CC;">save</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Go ahead and give it a try and play with it.  It&#8217;s a good recipe with many uses.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2012/02/21/setting-defaults-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>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>
	</channel>
</rss>

