<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Official CitrusByte Blog</title>
	
	<link>http://blog.citrusbyte.com</link>
	<description>CitrusByte</description>
	<pubDate>Mon, 18 Aug 2008 18:00:01 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/Citrusbyte-Home" type="application/rss+xml" /><item>
		<title>Los Angeles Web Application Developers 10th Meetup Rocked</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/362217567/</link>
		<comments>http://blog.citrusbyte.com/2008/08/11/los-angeles-web-application-developers-10th-meetup-rocked/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 19:30:05 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[meetup]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.citrusbyte.com/?p=101</guid>
		<description><![CDATA[We had a great meetup this week on the 5th. There was an attendance of about eighty-five people this time. This meetup also marked the 10th meetup for us. We couldn&#8217;t have done it without your eagerness to be part of an evolving tech-community in Los Angeles.
We also have to thank Rubicon for hosting the [...]]]></description>
			<content:encoded><![CDATA[<p>We had a great <a href="http://web.meetup.com/34/">meetup</a> this week on the 5th. There was an attendance of about eighty-five people this time. This meetup also marked the 10th meetup for us. We couldn&#8217;t have done it without your eagerness to be part of an evolving tech-community in Los Angeles.</p>
<p>We also have to thank Rubicon for hosting the 10th meetup. They had an amazing looking location, and it even has a bit of Hollywoodesque history associated with it. Apparently, their space was used for the show 24, and we got to see Jack Bauer&#8217;s office which is pretty damn cool. We also overheard that Snoop Dogg laid some tracks down in one of the other rooms. Rubicon is definitely one of my favorite web-venues that I&#8217;ve seen.</p>
<p>The following was presented at the meetup:</p>
<blockquote><p>Ari Lerner - Auto-Scaling cloud-computing on amazon EC2 + S3</p>
<p>Nick Merwin - Rolling with Red5 (open source Flash server)</p>
<p>Erik Osterman - Faster Response Times By Using the Starling Queue Server</p></blockquote>
<p>To get the slides and example code, check out this <a href="http://web.meetup.com/34/messages/boards/thread/5200801">post</a> in the meetup boards.</p>
<p>Each presentation was equally entertaining and informative. Thanks for providing quality content to the community guys!</p>
<p>Here&#8217;s a photo I found off the meetup group for your enjoyment:</p>
<div class="wp-caption alignnone" style="width: 353px"><img src="http://photos2.meetupstatic.com/photos/event/7/4/c/0/event_5069888.jpeg" alt="" width="343" height="256" /><p class="wp-caption-text">Jack Bauer&#39;s office is the office on the top right in this image</p></div>
<p>Looking for some videos? Why of course!</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="437" height="371" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="id" value="viddler" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="src" value="http://www.viddler.com/player/null/" /><embed id="viddler" type="application/x-shockwave-flash" width="437" height="371" src="http://www.viddler.com/player/null/" wmode="transparent" allowfullscreen="true" allowscriptaccess="always"></embed></object></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="437" height="371" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="id" value="viddler" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="src" value="http://www.viddler.com/player/null/" /><embed id="viddler" type="application/x-shockwave-flash" width="437" height="371" src="http://www.viddler.com/player/null/" wmode="transparent" allowfullscreen="true" allowscriptaccess="always"></embed></object></p>
<p><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="437" height="371" id="viddler"><param name="movie" value="http://www.viddler.com/player/null/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent"/><embed src="http://www.viddler.com/player/null/" width="437" height="371" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" wmode="transparent" name="viddler" ></embed></object></p>
<p>What are people saying?</p>
<blockquote><p>The meeting was great, all the presentations were very informative and interesting, good choice of topics. The new format definitely work, let&#8217;s keep it. The Rubicon office was pretty fancy and served good food ;-) Thanks to Will for arranging this event, it was very well organized, like usual. - <a href="http://web.meetup.com/34/members/7276182/">Oleg Baranovsky</a></p>
<p>I enjoyed this event very much! Thanks Will for organizing this event! The presentations were engaging, and it was great meeting all of you web gurus! The food and beer was awesome too! :) - <a href="http://web.meetup.com/34/members/3282604/">Rob Stathem</a>
</p></blockquote>
<p>Take a look at our <a href="http://web.meetup.com/34/">meetup page</a> for more details.</p>
<p>Were you able to attend the meetup? What did you think of it?</p>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/362217567" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/08/11/los-angeles-web-application-developers-10th-meetup-rocked/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/08/11/los-angeles-web-application-developers-10th-meetup-rocked/</feedburner:origLink></item>
		<item>
		<title>TakeFive Interviews Ari Lerner</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912234/</link>
		<comments>http://blog.citrusbyte.com/2008/07/28/takefive-interviews-ari-lerner/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 22:35:39 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ari lerner]]></category>

		<category><![CDATA[interview]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=6</guid>
		<description><![CDATA[Ari Lerner, a Solutions Architect here at CitrusByte recently got interviewed by the guys at TakeFive.
Welcome to this week&#8217;s Rails TakeFive interview, our weekly discussion about Ruby on Rails with noted developers from throughout our community. This week, Ari Lerner of CitrusByte and among other things, creator of PoolParty, a framework for maintaining and running [...]]]></description>
			<content:encoded><![CDATA[<p>Ari Lerner, a Solutions Architect here at CitrusByte recently got interviewed by the guys at TakeFive.</p>
<blockquote><p>Welcome to this week&#8217;s Rails TakeFive interview, our weekly discussion about Ruby on Rails with noted developers from throughout our community. This week, Ari Lerner of CitrusByte and among other things, creator of PoolParty, a framework for maintaining and running auto-scalable applications on Amazon&#8217;s <span class="caps">EC2</span> cloud.</p></blockquote>
<p>&#8230;</p>
<blockquote><p><strong>FiveRuns:</strong> Amazon + Rails seems to be a prevalent choice right now, looking way back to the first instances as early as 2006, and seeing how far we have come. Can you talk a little bit about the benefits and challenges here?</p></blockquote>
<blockquote><p><strong>Ari Lerner:</strong> Amazon&#8217;s Web Services is probably the technology that excites me the most at the moment. The unprecedented flexibility enables developers to play, explore and harness the growing landscape of cloud computing. The last time I felt the joy of working with a new technology, I was writing &#8216;Drug Warz&#8217; on my Ti-83. If you have not yet looked into it, I highly suggest you take a peek.</p></blockquote>
<blockquote><p>Now to answer your question!</p></blockquote>
<blockquote><p>Cloud computing is all the rage today. Just as Rails revolutionized the web development framework, cloud computing is the next logical step in hosting, but it&#8217;s not going to solve all hosting issues. The non-technical challenge is to recognize it is just another weapon in your development tool-belt. The developer is the real secret sauce.</p></blockquote>
<blockquote><p>The largest technological challenges are configuration and maintenance of the <span class="caps">EC2</span> instances. there is software on all sides of the spectrum that can aid in automating this process, from my own open-source PoolParty to commercial RightScale that aids in support.</p></blockquote>
<blockquote><p>Amazon&#8217;s <span class="caps">EC2</span> makes it tempting to throw away proven techniques of deployment and management, but breaking the encapsulation of the methodologies just spells disaster. I am always in favor of using all the right tools available, but in their proper place.</p></blockquote>
<blockquote><p>I think it would be a mistake to have your application aware of its own hosting environment. You don&#8217;t want your software knowing it is running on a cloud, it should do what it does best&#8230; be the application.</p></blockquote>
<p>That’s just a snippet of the interview. Go ahead and take a look at the full interview <a href="http://blog.fiveruns.com/2008/7/28/rails-takefive-five-questions-with-ari-lerner">here.</a></p>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912234" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/07/28/takefive-interviews-ari-lerner/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/07/28/takefive-interviews-ari-lerner/</feedburner:origLink></item>
		<item>
		<title>Automate Your Rails Deployment</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912236/</link>
		<comments>http://blog.citrusbyte.com/2008/07/18/automate-your-rails-deployment/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 22:41:22 +0000</pubDate>
		<dc:creator>Mitchell Hashimoto</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[capistrano]]></category>

		<category><![CDATA[deployment]]></category>

		<category><![CDATA[passenger]]></category>

		<category><![CDATA[phusion]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[sprinkle]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=10</guid>
		<description><![CDATA[I&#8217;m a big fan of researching and playing with new technologies (hey! I remember rails 0.7) so its no surprise that I&#8217;ve been playing a lot with Ruby Enterprise Edition and Phusion Passenger (mod_rails) and researching it as a viable rails deployment solution. For those of you who are staying with the tried and true [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of researching and playing with new technologies (hey! I remember rails 0.7) so its no surprise that I&#8217;ve been playing a lot with <a href="http://www.rubyenterpriseedition.com/">Ruby Enterprise Edition</a> and <a href="http://www.modrails.com/">Phusion Passenger (mod_rails)</a> and researching it as a viable rails deployment solution. For those of you who are staying with the tried and true Nginx + Mongrel, let me offer a brief introduction to Phusion Passenger (mod_rails). But first&#8230; let&#8217;s take a trip down memory lane:</p>
<p><img src="http://farm4.static.flickr.com/3137/2737015422_ca8c091f40_o.png" alt="Rails Deployment History" /></p>
<p>Phusion Passenger is super easy to install and setup as an Apache mod and immediately simplifies the deployment of rails. It allows you to deploy a rails application without splitting your attention between the web server and app server, such as Nginx and Mongrel. You literally upload a configuration which looks like the following block of code to Apache and it manages your rails application for you.</p>

<div class="wp_syntax"><div class="code"><pre class="apache">&lt;VirtualHost *:<span style="color: #ff0000;">80</span>&gt;
  <span style="color: #00007f;">ServerName</span>  myrailssite.com
  <span style="color: #00007f;">ServerAlias</span> www.myrailssite.com
&nbsp;
  <span style="color: #00007f;">DocumentRoot</span> /var/www/rails-project/current/public
&lt;/VirtualHost&gt;</pre></div></div>

<p>Passenger gives rails the same ease of deployment as mod_php and mod_perl. And when you want, it will automatically reload your app if you deploy changes.</p>
<p>But the real gem is using Phusion Passenger with Ruby Enterprise Edition, which is a rewrite of the Ruby <span class="caps">MRI</span> with optimization for mod_rails in mind. The <a href="http://www.rubyenterpriseedition.com/comparisons.html">initial benchmarks</a> show that using both together results in lower memory usage and increased requests per second. Since then, passenger has had a myriad of early adopters reporting their results. <a href="http://blog.dreamhost.com/2008/05/13/passenger-for-ruby-on-rails/">DreamHost praised Phusion</a> for finally solving their rails deployment, after <a href="http://blog.dreamhost.com/2008/01/10/rails-is-as-rails-does/">complaining only 4 months earlier of how rails deployment options were a joke.</a> Other <a href="http://lstoll.net/2008/04/12/passenger-a-k-a-mod-rails">websites</a> <a href="http://simpson.mine.nu/?tag=mod_rails">have</a> <a href="http://alloycode.com/2008/4/26/another-passenger-on-the-mod_rails">boarded</a> the Phusion Passenger train too, reporting easy setup and even easier deployment.</p>
<p>The results look good: <strong>Dead easy rails deployment with great performance.</strong></p>
<p>So how do you get started? For dead easy server provisioning, check out <a href="http://github.com/crafterm/sprinkle/tree/master">Sprinkle</a>, a gem which provisions servers for you. I&#8217;ve been hard at work implementing new features and patches to Sprinkle, which <a href="http://github.com/mitchellh/sprinkle/tree/master">you can see in my fork.</a> I have <a href="http://mitchellhashimoto.com/wp-content/movies/ree_passenger_rails/package.zip">already created the sprinkle scripts</a> to automatically setup this entire stack on a fresh ubuntu server:</p>
<ul>
<li>Rails (and Ruby, Rubygems)</li>
<li>sqlite3 or mySQL</li>
<li>Apache2</li>
<li>Phusion Passenger (mod_rails)</li>
<li>Ruby Enterprise Edition</li>
</ul>
<p>If you&#8217;re interested in the automatic setup solution, I recommend <a href="http://mitchellhashimoto.com/wp-content/movies/ree_passenger_rails/REE+Passenger.mov">checking out the screencast I made</a> detailing the entire process. It requires a <strong>custom version of the sprinkle gem</strong> (from my fork), which the screencast covers.</p>
<p>If you plan on trying it out and have any questions, leave a comment or feel free to contact me at <strong>mitchell.hashimoto [at] citrusbyte [dot] com</strong> and I&#8217;ll happily provide any assistance you may need.</p>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912236" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/07/18/automate-your-rails-deployment/feed/</wfw:commentRss>
<enclosure url="http://mitchellhashimoto.com/wp-content/movies/ree_passenger_rails/REE+Passenger.mov" length="50927657" type="video/quicktime" />
		<feedburner:origLink>http://blog.citrusbyte.com/2008/07/18/automate-your-rails-deployment/</feedburner:origLink></item>
		<item>
		<title>Keep your site as fresh as your code</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912239/</link>
		<comments>http://blog.citrusbyte.com/2008/06/27/keep-your-site-as-fresh-as-your-code/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 22:59:51 +0000</pubDate>
		<dc:creator>ari</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[git]]></category>

		<category><![CDATA[github]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=17</guid>
		<description><![CDATA[I am actively working on PoolParty, but the website doesn&#8217;t necessarily show that the project is in active development.
Luckily, it is hosted at github and they have an API to access their changesets. Now, on PoolParty&#8217;s website, I am surfacing the latest changeset&#8217;s time, message and author as well as a link to the changeset.
How?
Code [...]]]></description>
			<content:encoded><![CDATA[<p>I am actively working on <a href="http://poolpartyrb.com/">PoolParty</a>, but the website doesn&#8217;t necessarily show that the project is in active development.</p>
<p>Luckily, it is hosted at github and they have an <span class="caps">API</span> to access their changesets. Now, on PoolParty&#8217;s website, I am surfacing the latest changeset&#8217;s time, message and author as well as a link to the changeset.</p>
<p>How?</p>
<p>Code (shortened for brevity purposes):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">$:.<span style="color:#9900CC;">unshift</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rubygems&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;yaml&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;open-uri&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Grab
  <span style="color:#0066ff; font-weight:bold;">@url</span> = <span style="color:#996600;">&quot;http://github.com/api/v1/yaml/auser/pool-party/commits/master&quot;</span>
  <span style="color:#0066ff; font-weight:bold;">@refresh_after</span> = <span style="color:#006666;">20</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">latest_commit_object</span>
    o = yaml<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;commits&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</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;">&#123;</span>
      <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> o<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;message&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\n<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">&quot;&lt;br /&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:date</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>o<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;committed_date&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">timeago</span>,
      <span style="color:#ff3333; font-weight:bold;">:by</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> o<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;committer&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;name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> o<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;url&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The github <a href="http://github.com/guides/the-github-api">api</a> is slick. As you can see from above, the format for the api url looks like</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">http:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>api<span style="color:#006600; font-weight:bold;">/</span>version<span style="color:#006600; font-weight:bold;">/</span><span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">/</span>username<span style="color:#006600; font-weight:bold;">/</span>repository<span style="color:#006600; font-weight:bold;">/</span>type<span style="color:#006600; font-weight:bold;">/</span>object</pre></div></div>

<p>Where the variables there correspond to:</p>
<ul>
<li>Current version: v1</li>
<li>Acceptable formats: json, xml, yaml</li>
<li>Acceptable types: commits, commit</li>
</ul>
<p>Grabbing the latest changesets from the url as <span class="caps">YAML</span> format, we can quickly and easily parse the message into a useful hash.</p>
<p>I personally like the &#8220;# hours ago&#8221; syntax that 2.0 sites are popping up with everywhere, so I just added a timeago method that is very similar to the one included with rails. The source is attached.</p>
<p>Since I don&#8217;t want poolpartyrb.com&#8217;s load time to be dependent upon how fast it can hit github, I cache the variable so every 20 views it refreshes.</p>
<h3>Use in Rails</h3>
<p>Save both the files (attached below) to your lib directory.</p>
<p>Add this to your environment.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'grab'</span></pre></div></div>

<p>Then add this helper to your application_helper.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> show_latest_commit
    <span style="color:#0066ff; font-weight:bold;">@latest_commit</span> = Grab.<span style="color:#9900CC;">latest_commit_object</span>
&nbsp;
    <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS
      <span style="color:#006600; font-weight:bold;">&lt;</span>div <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">'note'</span><span style="color:#006600; font-weight:bold;">&gt;&lt;</span>strong<span style="color:#006600; font-weight:bold;">&gt;</span>Latest commit<span style="color:#006600; font-weight:bold;">&lt;/</span>strong<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;</span>
      <span style="color:#006600; font-weight:bold;">&lt;</span>a href=<span style="color:#996600;">&quot;#{@latest_commit[:url]}&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#008000; font-style:italic;">#{@latest_commit[:message]}&lt;/a&gt;</span>
      <span style="color:#006600; font-weight:bold;">&lt;</span>div <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;highlight&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>by <span style="color:#008000; font-style:italic;">#{@latest_commit[:by]}&lt;/div&gt;</span>
      <span style="color:#008000; font-style:italic;">#{@latest_commit[:date]}</span>
    EOS
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>How to use in <a href="http://sinatrarb.com">sinatra</a></h3>
<p>I use this on <a href="http://poolpartyrb.com">poolpartyrb.com</a>, which is a sinatra site. I do basically the same as above, but generally render the code in haml instead</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  $:.<span style="color:#9900CC;">unshift</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;sinatra&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'grab'</span>
&nbsp;
  get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@latest_commit</span> = Grab.<span style="color:#9900CC;">latest_commit_object</span>
    haml <span style="color:#ff3333; font-weight:bold;">:home</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:layout</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then in my home.haml</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  .<span style="color:#9900CC;">note</span>
    <span style="color:#006600; font-weight:bold;">%</span>strong
      Latest commit
    <span style="color:#006600; font-weight:bold;">%</span>br
    ==<span style="color:#006600; font-weight:bold;">&lt;</span>a href=<span style="color:#996600;">&quot;#{@latest_commit[:url]}&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>
    = <span style="color:#0066ff; font-weight:bold;">@latest_commit</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#006600; font-weight:bold;">&lt;/</span>a<span style="color:#006600; font-weight:bold;">&gt;</span>
    .<span style="color:#9900CC;">highlight</span>
      == by <span style="color:#008000; font-style:italic;">#{@latest_commit[:by]}</span>
    = <span style="color:#0066ff; font-weight:bold;">@latest_commit</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:date</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p><a href="http://blog.citrusbyte.com/wp-content/uploads/2008/08/grab_code.zip">Get the code here</a></p>
<p>edit: updated location of code</p>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912239" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/06/27/keep-your-site-as-fresh-as-your-code/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/06/27/keep-your-site-as-fresh-as-your-code/</feedburner:origLink></item>
		<item>
		<title>Announcing… PoolParty! An Open Source tool for managing EC2 clusters</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912240/</link>
		<comments>http://blog.citrusbyte.com/2008/06/05/announcing-poolparty-an-open-source-tool-for-managing-ec2-clusters/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 23:02:11 +0000</pubDate>
		<dc:creator>tim</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[code]]></category>

		<category><![CDATA[gem]]></category>

		<category><![CDATA[opensource]]></category>

		<category><![CDATA[poolparty]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=20</guid>
		<description><![CDATA[
Is your EC2 server pool starting to look like this?

PoolParty can help you turn it into this:

Why would I need PoolParty?
Amazon&#8217;s Elastic Compute Cloud has made it cheap and straightforward to run your own server pool, but there are various issues with volatility, lack of load balancing between instances, no persistent instance storage (currently in [...]]]></description>
			<content:encoded><![CDATA[<br />
<h3>Is your <span class="caps">EC2</span> server pool starting to look like this?</h3>
<p><img src="http://farm2.static.flickr.com/1132/1020541364_593fdff72c.jpg?v=0" alt=""></p>
<h3>PoolParty can help you turn it into this:</h3>
<p><img src="http://farm1.static.flickr.com/43/122451696_1568c215d3.jpg?v=0" alt=""></p>
<h3>Why would I need PoolParty?</h3>
<p>Amazon&#8217;s <a href="http://aws.amazon.com/ec2">Elastic Compute Cloud</a> has made it cheap and straightforward to run your own server pool, but there are various issues with volatility, lack of load balancing between instances, no persistent instance storage (currently in <a href="http://aws.typepad.com/aws/2008/04/block-to-the-fu.html">private beta</a>).  Also there is no mechanism for scaling your instances dynamically (ie, adding or removing instances as your load changes).</p>
<p>That is all taken care of by <a href="http://poolpartyrb.com">PoolParty</a>, a recently open sourced <a href="http://poolparty.rubyforge.org/">Ruby gem</a> and the brainchild of fellow <img src="http://farm4.static.flickr.com/3129/2736231221_e7ded1ecdf.jpg?v=0" style="display: inline; margin-left: 4px; margin-right: -2px;"> CitrusByte developer Ari Lerner.  PoolParty takes care of the following:</p>
<ul>
<li>Automatic scaling based on demand and load</li>
<li>Starting/stopping instances</li>
<li>Self-healing</li>
<li>Provisioning and bootstrapping initial software</li>
<li>Setting up S3Fuse</li>
<li>Load balancing with HAProxy</li>
<li>Built-in monitoring</li>
<li>Plug-in architecture for extendability / customization</li>
</ul>
<h3>How?</h3>
<p>PoolParty is based on established open source technologies that have been widely used in production.  It takes your <span class="caps">EC2</span> server pool, and invites <a href="http://sourceforge.net/projects/s3fuse/">S3Fuse</a>, <a href="http://www.tildeslash.com/monit/">monit</a>, and members of the <a href="http://www.linux-ha.org/">High Availability Linux Project</a> .  Now you don&#8217;t just have a pool.  You have a rocking pool party.  All you have to do is sign those invites! (In other words: just provide a config file and you&#8217;re done).</p>
<h3>More Information</h3>
<p>You can check it out at the <a href="http://poolpartyrb.com/">official PoolParty website</a>. Tutorials, walk-throughs and articles are coming soon.</p>
<p>If you are interested in active development, check out the source at <a href="http://github.com/auser/pool-party/tree/master">github</a>. Active discussion is on the <a href="http://groups.google.com/group/poolpartyrb">PoolParty Google Group</a></p>
<p>Here are the slides from the talk Ari gave at RailsConf sessions (also available <a href="http://poolpartyrb.com/misc.html">here</a>):</p>
<p><object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="doc_697824350259522" name="doc_697824350259522" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle" height="350" width="450"><param name="movie" value="http://documents.scribd.com/ScribdViewer.swf?document_id=3253193&amp;access_key=key-2ekznsio0u65pzgedip2&amp;page=1&amp;version=1"><param name="quality" value="high"><param name="play" value="true"><param name="loop" value="true"><param name="scale" value="showall"><param name="wmode" value="opaque"><param name="devicefont" value="false"><param name="bgcolor" value="#ffffff"><param name="menu" value="true"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><param name="salign" value=""><embed src="http://documents.scribd.com/ScribdViewer.swf?document_id=3253193&amp;access_key=key-2ekznsio0u65pzgedip2&amp;page=1&amp;version=1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" play="true" loop="true" scale="showall" wmode="opaque" devicefont="false" bgcolor="#ffffff" name="doc_697824350259522_object" menu="true" allowfullscreen="true" allowscriptaccess="always" salign="" type="application/x-shockwave-flash" align="middle" height="350" width="450"> </object>
<div style="font-size: 10px; text-align: center;"><a href="http://www.scribd.com/doc/3253193/pool-party-presentation">pool party presentation</a> <a href="http://www.scribd.com/upload">Upload a doc</a></div>
<div style="display: none;"> Read this doc on Scribd: <a href="http://www.scribd.com/doc/3253193/pool-party-presentation">pool party presentation</a> </div>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912240" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/06/05/announcing-poolparty-an-open-source-tool-for-managing-ec2-clusters/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/06/05/announcing-poolparty-an-open-source-tool-for-managing-ec2-clusters/</feedburner:origLink></item>
		<item>
		<title>Ruby TMTOWTDI, Episode 1</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912243/</link>
		<comments>http://blog.citrusbyte.com/2008/06/02/ruby-tmtowtdi-episode-1/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 23:06:05 +0000</pubDate>
		<dc:creator>tim</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[TMTOWTDI]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=24</guid>
		<description><![CDATA[This is the inaugural post in a series that we&#8217;ll be calling Ruby TMTOWTDI (There&#8217;s more than one way to do it).  Usually the TMTOWTDI acronym is used as a disparaging term towards flexible languages, which offer a myriad ways of solving most given problems.  This series, however, aims to use this property [...]]]></description>
			<content:encoded><![CDATA[<p>This is the inaugural post in a series that we&#8217;ll be calling Ruby <span class="caps">TMTOWTDI</span> (There&#8217;s more than one way to do it).  Usually the <span class="caps">TMTOWTDI</span> acronym is used as a disparaging term towards flexible languages, which offer a myriad ways of solving most given problems.  This series, however, aims to use this property of Ruby as an educational tool.</p>
<p>The format is as follows: we&#8217;ll present a problem, then everyone will try and write the most idiomatic, concise code that solves the problem in a readable fashion.  This is not meant to be a golfing competition, nor an optimization one.  However, we will benchmark the solutions for a rough idea on how different styles affect performance. For every installment we&#8217;ll have a performance winner for fastest time and a &#8217;style&#8217; winner for the most elegant code.</p>
<p>This gives us at CitrusByte a better understanding of each other&#8217;s coding styles, and stimulates discussions about little tricks we may not be aware of. Best of all, it&#8217;s fun as heck to be a part of.</p>
<p>You can get involved too!  Leave a comment with your solution if you think you can best the ones listed here.  We also encourage you to propose a simple problem (less than 20 lines of code to solve) for the next instalment in the series.</p>
<p>Since this is the first time we&#8217;re doing it, we&#8217;ve picked a very simple problem to solve as a quick warm-up.</p>
<p>Problem: given a string &#8217;str&#8217; of the format</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#996600;">&quot;a10 b20 c25 d40&quot;</span></pre></div></div>

<p>convert it to a hash</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">'a'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">10</span>, <span style="color:#996600;">'b'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span>, <span style="color:#996600;">'c'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">25</span>, <span style="color:#996600;">'d'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">40</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Note that the values of this hash are integers, not strings.  If the string is</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#996600;">&quot;a10 a20&quot;</span></pre></div></div>

<p>the output should be</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">'a'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>.</p>
<p>Leaving some spoiler space in case you want to try it on your own.  Below are selected solutions from our team.</p>
<hr />
<h2>Spoiler space</h2>
<hr />
<h3>Solutions</h3>
<h4>Michel</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span>str.<span style="color:#9900CC;">scan</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;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</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;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k, v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>k, v.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Time taken: 9.99s</p>
<h4>Tim</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#40;</span>str.<span style="color:#9900CC;">scan</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>\w<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<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;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k, v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>k, v.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Time taken: 9.40s</p>
<p>Tim&#8217;s comments: the splat operator has low precedence so the outermost set of<br />
parantheses are redundant, but I felt it&#8217;s better to have them there to make it<br />
very obvious what&#8217;s going on.</p>
<h4>Nicolas (winner: speed award)</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby">str.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\s<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><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<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;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><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> <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;">&#40;</span>k,v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span> = v.<span style="color:#9900CC;">to_i</span>; h <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Time taken: 7.19s</p>
<p>Nicolas&#8217;s comments: I like inject/reduce/fold, whatever you call it. It&#8217;s<br />
concise and usually a perfect fit for constructing enumerable data structures.</p>
<h4>Martin (winner: style award)</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby">str.<span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><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> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>h,i<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">/</span>^\D<span style="color:#006600; font-weight:bold;">+/</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = i<span style="color:#006600; font-weight:bold;">&#91;</span><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;">&#93;</span>.<span style="color:#9900CC;">to_i</span>; h <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Time taken: 8.75s</p>
<h4>Ari</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby">hash = str.<span style="color:#CC0066; font-weight:bold;">split</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><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> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>set, h<span style="color:#006600; font-weight:bold;">|</span>
  set.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>h<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:#9900CC;">chr</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> h<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">-1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</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>Time taken: 23.02s</p>
<p>Ari&#8217;s comments: I knew everyone else would be strutting their one-liners, so I went for as much clarity as possible.  I feel this code not only solves the problem, but also makes it very apparent what the desired solution is.</p>
<h3>Benchmark code</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'benchmark'</span>
&nbsp;
ITERATION = <span style="color:#006666;">400</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> measure<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>amp;block<span style="color:#006600; font-weight:bold;">&#41;</span>
  ITERATION.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> block.<span style="color:#9900CC;">call</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
str = <span style="color:#996600;">''</span>
<span style="color:#006666;">5000</span>.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> str <span style="color:#006600; font-weight:bold;">&amp;</span>lt;<span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'a'</span>..<span style="color:#996600;">'z'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">26</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">90</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006666;">+10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">' '</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
br = <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bmbm</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>b<span style="color:#006600; font-weight:bold;">|</span>
  b.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Running #{ITERATION} times on string of length #{str.length}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    measure <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#008000; font-style:italic;">#### Code to measure goes here</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Conclusion</h3>
<p>Lots of interesting styles here.  We have two ways of creating hashes: with the <code>Hash[ [key =>|, value]*</code> method and with <code>inject()</code>.</p>
<p>We&#8217;ve also seen various ways of breaking up a string: with <code>scan()</code>, with <code>split()</code>, and with regex arguments to a string&#8217;s <code>[]</code> method.  While we cannot come to definite conclusions about performance (since the code that is being compared doesn&#8217;t differ just in one area), we can see a couple things going on:</p>
<ul>
<li>running Hash[] on an array as opposed to creating it directly via inject hurt Michel and Tim&#8217;s performances compared to Martin/Nicolas</li>
<li>merge() seems to be significantly more costly than straight addition/replacement of hash keys. If you&#8217;re ever injecting into a hash it may be wiser (if your problem allows) to follow the <code>{ hash[key] = value; hash }</code> format instead of merging.</li>
</ul>
<p>We&#8217;ve picked Martin as the style winner for the elegance of his code which fit of the string parsing inside the inject block as opposed to the scan-based solutions which did some of it outside in the scan.  The <code>h[i[/^\D+/]] = i[/\d+$/].to_i</code> portion was the clearest signal of intent for the contents of the hash.</p>
<p>Meanwhile, Nicolas is the performance winner by a mile, coming in more than 20% faster than the nearest competitor.</p>
<p>Congratulations to Martin (style) and Nicolas (speed) for being the winners of the inaugural CitrusByte Ruby <span class="caps">TMTOWTDI</span>.  Stay tuned for the next episode!</p>
<h3><span class="caps">EDIT</span></h3>
<p>Additional comments by Nicolas:</p>
<p>Anyway, after thinking about it, even if I like inject a lot, there&#8217;s one big gain for the Hash[*...] as Michel posted. It<br />
transmits intent better, in a way.</p>
<p>You look at that and the entire code sequence is a Hash construction, plain and simple, even if you ignore the subexpression in the middle, you know the outcome. And if you look one character into the brackets, then you know it&#8217;s constructing a hash from some transformation of an existing enumerable. Bah, just something that responds to #to_a.</p>
<p>Instead, using inject, the code flows (at least in my head) nicer as you don&#8217;t have to parse subexpressions, but it&#8217;s not immediately obvious what&#8217;s it about.</p>
<p>Hm, actually, depends on what intent is better to show. I mean, the objective is something like &#8220;to transform a string in a certain format into a more traversable structure&#8221; or &#8220;to create a hash out of a string in a certain format?&#8221;</p>
<p>That (not so) subtle difference might make me decide about which way is better, but then the description of the problem is not descriptive enough. Good, that means that the problem lies in the exercise&#8217;s designer. I can sleep well :P</p>
<p>(Probably there&#8217;s a third approach that to a degree involves a recursive lambda and someone flying over a desktop yelling &#8220;stand back! I know hash construction&#8221;, but my ninjitsu is not good enough for that yet)</pre>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912243" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/06/02/ruby-tmtowtdi-episode-1/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/06/02/ruby-tmtowtdi-episode-1/</feedburner:origLink></item>
		<item>
		<title>BNet Interview: Building Successful Web Companies</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912246/</link>
		<comments>http://blog.citrusbyte.com/2008/04/18/bnet-interview-building-successful-web-companies/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 23:06:47 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[bnet]]></category>

		<category><![CDATA[interview]]></category>

		<category><![CDATA[will jessup]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=26</guid>
		<description><![CDATA[
  

Will Jessup is a founder and Managing Member of CitrusByte, a forward-thinking web application development business. Jessup talks about his company structure and how it attracts the best unique talent for his clients.

]]></description>
			<content:encoded><![CDATA[
<p>  <object type="application/x-shockwave-flash" data="http://i.zdnet.com/flash/cnb_video.swf" height="350" width="400"><param name="FlashVars" value="vidFile=dap264_will_jessup_fix.flv&amp;connectUrl=rtmp://v.zdnet.com/video/dap264_will_jessup_fix.flv&amp;br=2&amp;si=23&amp;autoplay=false&amp;still=http://i.zdnet.com/gallery/197624-400-300.jpg"><param name="movie" value="http://i.zdnet.com/flash/cnb_video.swf"></object></p>
<blockquote>
<p>Will Jessup is a founder and Managing Member of CitrusByte, a forward-thinking web application development business. Jessup talks about his company structure and how it attracts the best unique talent for his clients.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912246" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/04/18/bnet-interview-building-successful-web-companies/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/04/18/bnet-interview-building-successful-web-companies/</feedburner:origLink></item>
		<item>
		<title>April Meetup AOL Announced: April 22nd</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912248/</link>
		<comments>http://blog.citrusbyte.com/2008/04/11/april-meetup-aol-announced-april-22nd/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 23:55:17 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[meetup]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=28</guid>
		<description><![CDATA[We will have the next Los Angeles Web Application Developers Meetup at AOL this month. We are only letting in 75 guests this time. RSVP asap to reserve your spot.

AOL is the sponsor of this months meetup on April 22nd at 8pm. Weâ€™ll get between 50-75 developers together and go over some quick presentations about [...]]]></description>
			<content:encoded><![CDATA[<p>We will have the next <a href="http://web.meetup.com/34/">Los Angeles Web Application Developers Meetup</a> at <span class="caps">AOL</span> this month. <strong>We are only letting in 75 guests</strong> this time. <em><span class="caps">RSVP</span> asap to reserve your spot.</em></p>
<blockquote>
<p><span class="caps">AOL</span> is the sponsor of this months meetup on April 22nd at 8pm. Weâ€™ll get between 50-75 developers together and go over some quick presentations about new cool stuff , eat some pizza and chat. The presentation format is 5-10 minutes on something that web application developers should know, followed by 5 minutes Q&amp;A. There are four open slots right now, so please send me your proposals for a presentation!</p>
</blockquote>
<p><strong>When?</strong>: Tuesday, Apr 22, 2008, 8:00 PM</p>
<p><strong>Where?</strong>: <span class="caps">AOL</span>, 331 N Maple Dr, Beverly Hills, <span class="caps">CA 90210</span></p>
<blockquote>
<p>Join us at Los Angeles&#8217;s biggest meetup for web application developers. Find out what&#8217;s new in development for the web. Presenters will allow you to pick up a few tips and tricks and find out about new tools. After our presentations enjoy refreshments (and beer) and network with other local developers.</p>
</blockquote>
<p><a href="http://web.meetup.com/34/">http://web.meetup.com/34/</a></p>
<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912248" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/04/11/april-meetup-aol-announced-april-22nd/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/04/11/april-meetup-aol-announced-april-22nd/</feedburner:origLink></item>
		<item>
		<title>Kill your fixtures and spec happily</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912249/</link>
		<comments>http://blog.citrusbyte.com/2008/04/05/kill-your-fixtures-and-spec-happily/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 00:03:05 +0000</pubDate>
		<dc:creator>ben</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=30</guid>
		<description><![CDATA[
The trouble w/ fixtures
I don&#8217;t use fixtures in my specs. At all. The fundamental issue with fixtures is that they setup a state for your database as a whole (or at least the tables you loaded). So when you use fixtures all your specs are running against essentially the same state. As you build your [...]]]></description>
			<content:encoded><![CDATA[<br />
<h2>The trouble w/ fixtures</h2>
<p>I don&#8217;t use fixtures in my specs. At all. The fundamental issue with fixtures is that they setup a state for your database as a whole (or at least the tables you loaded). So when you use fixtures all your specs are running against essentially the same state. As you build your application and things get more complex your fixtures become more complex and your state grows and grows. The needs of your specs, however, should pretty much stay the same. So when you go back and track down an issue causing some spec to blow up you have to deal with  that spec running in a state that&#8217;s way beyond it&#8217;s needs.</p>
<h2>Brittle specs</h2>
<p>
For example, lets stay I’m writing a spec to ensure that <code>book.authors.living</code> does not return any deceased authors. The state that I need to spec this is very simple: 1 Book with 1 living Author and 1 dead Author. Then I just need to make sure that when I run <code>book.authors.living</code> I don&#8217;t get the dead Author back. It might look something like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">describe Book <span style="color:#9966CC; font-weight:bold;">do</span>
  describe <span style="color:#996600;">&quot;with authors&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    fixtures <span style="color:#ff3333; font-weight:bold;">:books</span>, <span style="color:#ff3333; font-weight:bold;">:authors</span>
&nbsp;
    it <span style="color:#996600;">&quot;should only find living authors&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      Book.<span style="color:#9900CC;">find_by_title</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Foo's Diary&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">authors</span>.<span style="color:#9900CC;">living</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#91;</span>Author.<span style="color:#9900CC;">find_by_name</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Jim'</span><span style="color:#006600; font-weight:bold;">&#41;</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Assuming I know that Jim is alive this would be a good spec. The problem is as I write specs I&#8217;m going to add more and more authors and this spec will quickly blow up. It&#8217;s not blowing up because the <code>.living</code> method broke though, it&#8217;s just blowing up because it&#8217;s no longer running in the state it expected.</p>
<h2>Kill your fixtures</h2>
<p>Without fixtures I would do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">describe Book <span style="color:#9966CC; font-weight:bold;">do</span>
  describe <span style="color:#996600;">&quot;with authors&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    before <span style="color:#ff3333; font-weight:bold;">:each</span> <span style="color:#9966CC; font-weight:bold;">do</span>
       <span style="color:#0066ff; font-weight:bold;">@book</span> = Book.<span style="color:#9900CC;">create</span>! <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Foo's Diary&quot;</span>
       <span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">authors</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@jim = Author.<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;">'Jim'</span>, <span style="color:#ff3333; font-weight:bold;">:dead</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">authors</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@jeff = Author.<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;">'Jeff'</span>, <span style="color:#ff3333; font-weight:bold;">:dead</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</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;
    it <span style="color:#996600;">&quot;should only find living authors&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">reload</span>.<span style="color:#9900CC;">authors</span>.<span style="color:#9900CC;">living</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#91;</span>@jim<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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now my spec will always run in the state I gave it. So the things I setup for my other specs won&#8217;t come back and blow this spec up. Furthermore, the state isn&#8217;t buried in multiple fixture files. It&#8217;s defined plainly right above the spec(s) which use it.</p>
<h2>Simplify your life even more</h2>
<p>Once you ditch your fixtures you&#8217;ll find a new love for writing specs. There&#8217;s a couple handy tools to simplify your life further though:</p>
<h3>Factories</h3>
<p>Dan Manges <a href="http://www.dcmanges.com/blog/38">blogged about using factories</a> to simplify creation of classes and to provide defaults. Since then there&#8217;s been a few plugins developed for quick and easy factories. We&#8217;ve been using Scott Taylor&#8217;s <a href="http://replacefixtures.rubyforge.org/">Fixture Replacement</a>.</p>
<h3><a href="http://blog.citrusbyte.com/2008/4/3/clean-up-your-code-a-little-with-add">add!</a></h3>
<p><code>add</code> is similar to the <code>&lt;&lt;</code> method of <code>has_many</code> associations except it sacrifices chainability in order to return the concatenated object. <code>add!</code> goes a step further and throws an exception if the concatenation failed. This is useful in specs because the exception (probably due to a validation error) will occur in your state setup and you will see the error message in your output when you run your specs. So the reason your spec just blew up is displayed for you.</p>
<p>With Fixture Replacement and add! the example above would look something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">describe Book <span style="color:#9966CC; font-weight:bold;">do</span>
  describe <span style="color:#996600;">&quot;with authors&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    before <span style="color:#ff3333; font-weight:bold;">:each</span> <span style="color:#9966CC; font-weight:bold;">do</span>
       <span style="color:#0066ff; font-weight:bold;">@book</span> = create_book <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Foo's Diary&quot;</span>
       <span style="color:#0066ff; font-weight:bold;">@jim</span> = <span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">authors</span>.<span style="color:#9900CC;">add</span>! new_author<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;">'Jim'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#0066ff; font-weight:bold;">@jeff</span> = <span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">authors</span>.<span style="color:#9900CC;">add</span>! new_author<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;">'Jeff'</span>, <span style="color:#ff3333; font-weight:bold;">:dead</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    it <span style="color:#996600;">&quot;should only find living authors&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">reload</span>.<span style="color:#9900CC;">authors</span>.<span style="color:#9900CC;">living</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006600; font-weight:bold;">&#91;</span>@jim<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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912249" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/04/05/kill-your-fixtures-and-spec-happily/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/04/05/kill-your-fixtures-and-spec-happily/</feedburner:origLink></item>
		<item>
		<title>Clean up your code a little with add!</title>
		<link>http://feeds.feedburner.com/~r/Citrusbyte-Home/~3/358912251/</link>
		<comments>http://blog.citrusbyte.com/2008/04/03/clean-up-your-code-a-little-with-add/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 00:08:53 +0000</pubDate>
		<dc:creator>ben</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[has_many]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=33</guid>
		<description><![CDATA[
Here&#8217;s a little chunk of code I&#8217;ve been using recently. It gives you .add
 and .add!  methods on your has_many relationships that work similar to the &#60;&#60; method. The difference is that .add sacrifices chain-ability in order to return the object being added to the collection.
Adding instead of concatenating

Using book.authors  &#8216;Geoff&#8217;) returns true [...]]]></description>
			<content:encoded><![CDATA[
<p>Here&#8217;s a little chunk of code I&#8217;ve been using recently. It gives you <code>.add</code><br />
 and <code>.add!</code>  methods on your <code>has_many</code> relationships that work similar to the <code>&lt;&lt;</code> method. The difference is that <code>.add</code> sacrifices chain-ability in order to return the object being added to the collection.</p>
<h2>Adding instead of concatenating</h2>
<p>
Using <code>book.authors << Author.new(:name => &#8216;Geoff&#8217;)</code> returns true or false forcing you to do something like <code>book.authors << (geoff = Author.new(:name => &#8216;Geoff&#8217;))</code> to maintain a reference to the new object. Using <code>.add</code> you can do <code>geoff = book.authors.add Author.new(:name => 'Geoff')</code>
</p>
<h2>No really&#8230;add!ing</h2>
<p>
Perhaps even more helpful (especially in specs) is the <code>add!</code> method. <code>add!</code> acts just like <code>add</code> except it raises a <code>RecordInvalid</code> exception on failure. So you can do <code>geoff = book.authors.add! Author.new(:name => 'Geoff')</code> and have your execution (or your transaction) halt if Geoff messed up and wasn&#8217;t valid.
</p>
<h2>Blowing up your specs</h2>
<p>This comes in handy a lot in your specs as exceptions will be displayed as the reason for failure. So if you were to add a validation to your <code>Author</code> class that caused all the <code>Author</code>&#8217;s you were adding to <code>Book</code>&#8217;s all over your specs to be invalid, you would see it (along with your validation&#8217;s helpful error message) when you ran your specs.</p>
<h2>The goods</h2>
<p>Just drop this in a file in your <code>/lib</code> folder and require it in an initializer.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">module</span> Associations
    <span style="color:#9966CC; font-weight:bold;">class</span> AssociationCollection <span style="color:#006600; font-weight:bold;">&lt;</span> AssociationProxy <span style="color:#008000; font-style:italic;">#:nodoc:</span>
      <span style="color:#008000; font-style:italic;"># works just like &lt;&lt; except returns the added records instead of self, so</span>
      <span style="color:#008000; font-style:italic;"># it's not chainable but allows you to do something like:</span>
      <span style="color:#008000; font-style:italic;">#   @jim = Book.authors.add Author.new(:name =&gt; 'jim')</span>
      <span style="color:#008000; font-style:italic;"># (often saving a whole line of code!)</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> add<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>records<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>records<span style="color:#006600; font-weight:bold;">&#41;</span>
        unarray_if_lonely<span style="color:#006600; font-weight:bold;">&#40;</span>records<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># works just like add except raises an exception on error</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> add!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>records<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> RecordInvalid.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>records<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#006600; font-weight:bold;">&lt;&lt;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>records<span style="color:#006600; font-weight:bold;">&#41;</span>
        unarray_if_lonely<span style="color:#006600; font-weight:bold;">&#40;</span>records<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># if the given object is an array of size 1 then it returns the only</span>
      <span style="color:#008000; font-style:italic;"># element itself (w/ no array), otherwise it just gives back object</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> unarray_if_lonely<span style="color:#006600; font-weight:bold;">&#40;</span>object<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#006600; font-weight:bold;">&#40;</span>object.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> object.<span style="color:#9900CC;">length</span> == <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? object.<span style="color:#9900CC;">first</span> : object
      <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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/Citrusbyte-Home/~4/358912251" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.citrusbyte.com/2008/04/03/clean-up-your-code-a-little-with-add/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.citrusbyte.com/2008/04/03/clean-up-your-code-a-little-with-add/</feedburner:origLink></item>
	</channel>
</rss>
