<?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>Cretaceous Labs &#187; Coding</title>
	<atom:link href="http://cretaceouslabs.com/blog/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://cretaceouslabs.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 10 Oct 2010 20:02:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Deploy a specific revision with Capistrano</title>
		<link>http://cretaceouslabs.com/blog/2010/08/deploy-specific-revision-capistrano/</link>
		<comments>http://cretaceouslabs.com/blog/2010/08/deploy-specific-revision-capistrano/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 19:50:49 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Capistrano]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/?p=195</guid>
		<description><![CDATA[Just about every website that I release, be it at work or at home, is stored in a Git repository, and deployed using Capistrano.
I&#8217;ve been debugging this PHP application at work that was written by a 3rd-party. After cleaning up the XSS and SQL injection vulnerabilities in it, we found a new bug. It&#8217;s minor, [...]]]></description>
			<content:encoded><![CDATA[<p>Just about every website that I release, be it at work or at home, is stored in a <a title="Git" href="http://git-scm.com/">Git</a> repository, and deployed using <a title="Capistrano" href="http://capify.org/">Capistrano</a>.</p>
<p>I&#8217;ve been debugging this PHP application at work that was written by a 3rd-party. After cleaning up the XSS and SQL injection vulnerabilities in it, we found a new bug. It&#8217;s minor, yet important.</p>
<p>While tracking down the offending line, I needed to deploy various revisions of the website. It turns out that it&#8217;s really easy to do this with <a title="Capistrano" href="http://capify.org/">Capistrano</a>:</p>
<pre>$ cap -S revision=3f30b6de3c55a8347e5f3de3b43193591e6c7322 beta deploy</pre>
<p>One thing I noticed was the need to provide the full SHA-1 object name (AKA revision ID, commit ID). Normally with Git, you can provide the first few characters of a commit ID. For example:</p>
<pre>$ git checkout 3f30b6de</pre>
<p>instead of</p>
<pre>$ git checkout 3f30b6de3c55a8347e5f3de3b43193591e6c7322</pre>
<p>I guess Capistrano&#8217;s a bit more picky!</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2010/08/deploy-specific-revision-capistrano/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Matching Printable Characters</title>
		<link>http://cretaceouslabs.com/blog/2009/11/matching-printable-characters/</link>
		<comments>http://cretaceouslabs.com/blog/2009/11/matching-printable-characters/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 21:10:17 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/?p=138</guid>
		<description><![CDATA[Who doesn&#8217;t love regular expressions? They&#8217;re fucking awesome. I use&#8217;em at least 10 times per day.
Sometimes, there&#8217;re restrictions on what patterns you can use. I wanted to change a regex for validating passwords. Originally, it allowed letters, numbers, and a seemingly random collection of special characters. How lame is this?:
^[A-Za-z0-9]{1}[A-Za-z0-9_\\.\\!\@\#\-]{0,255}$
Not only are the allowed special [...]]]></description>
			<content:encoded><![CDATA[<p>Who doesn&#8217;t love regular expressions? <a href="/blog/wp-content/uploads/2009/11/dino-riders.jpg" alt="Dino-Riders: They're fucking awesome">They&#8217;re fucking awesome.</a> I use&#8217;em at least 10 times per day.</p>
<p>Sometimes, there&#8217;re restrictions on what patterns you can use. I wanted to change a regex for validating passwords. Originally, it allowed letters, numbers, and a seemingly random collection of special characters. How lame is this?:</p>
<pre>^[A-Za-z0-9]{1}[A-Za-z0-9_\\.\\!\@\#\-]{0,255}$</pre>
<p>Not only are the allowed special characters arbitrary, but they&#8217;re escaping characters in a character class, and using &#8220;{1}&#8221;.</p>
<p>I tried changing the regex to this:</p>
<pre>^[[:print:]]{0,255}$</pre>
<p>Unfortunately, that wasn&#8217;t considered &#8220;valid&#8221; by the system in question. Luckily, there&#8217;s a fairly concise alternative:</p>
<pre>^[\x20-\x7E]{0,255}$</pre>
<p>If you find a system that lacks support for POSIX character classes, check out <a href="http://en.wikipedia.org/wiki/Regular_expression#POSIX_character_classes" alt="POSIX character classes on Wikipedia">this Wikipedia article</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2009/11/matching-printable-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hashes and Frozen Keys</title>
		<link>http://cretaceouslabs.com/blog/2009/08/hashes-and-frozen-keys/</link>
		<comments>http://cretaceouslabs.com/blog/2009/08/hashes-and-frozen-keys/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 20:07:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2009/08/hashes-and-frozen-keys/</guid>
		<description><![CDATA[The hash class freezes keys that are strings:

This can be a problem if you want to use those keys later on&#8230;more specifically, if you want to extract and edit those strings:

Unfortunately, those strings will have to be duped or cloned:

]]></description>
			<content:encoded><![CDATA[<p>The hash class <a href="http://www.ruby-doc.org/core/classes/Hash.html#M002851">freezes keys</a> that are strings:<br />
<script src="http://gist.github.com/172416.js"></script><br />
This can be a problem if you want to use those keys later on&#8230;more specifically, if you want to extract and edit those strings:<br />
<script src="http://gist.github.com/172417.js"></script><br />
Unfortunately, those strings will have to be duped or cloned:<br />
<script src="http://gist.github.com/173453.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2009/08/hashes-and-frozen-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sinatra and Passenger/mod_rails</title>
		<link>http://cretaceouslabs.com/blog/2009/08/sinatra-and-passengermod_rails/</link>
		<comments>http://cretaceouslabs.com/blog/2009/08/sinatra-and-passengermod_rails/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 20:24:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2009/08/sinatra-and-passengermod_rails/</guid>
		<description><![CDATA[If you want Apache to serve up a Sinatra app, your best bet is to use Passenger (AKA mod_rails).
All your app needs is the usual Rackup config file (config.ru) in the app&#8217;s root directory, and the directory public/ .
There&#8217;s one caveat, though: if you have a file named environment.rb , do not put it in [...]]]></description>
			<content:encoded><![CDATA[<p>If you want Apache to serve up a Sinatra app, your best bet is to use <a href="http://www.modrails.com/">Passenger</a> (AKA mod_rails).</p>
<p>All your app needs is the usual Rackup config file (config.ru) in the app&#8217;s root directory, and the directory public/ .</p>
<p>There&#8217;s one caveat, though: if you have a file named environment.rb , do <span style="font-weight: bold;">not</span> put it in config/ .</p>
<p>If mod_rails finds config/environment.rb , it&#8217;ll think your app runs on Rails:<br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.flickr.com/photos/nphoffman/3818060253/sizes/o/"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 500px; height: 400px;" src="http://farm3.static.flickr.com/2604/3818060253_56f6e9c173.jpg" alt="" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2009/08/sinatra-and-passengermod_rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby in Etch</title>
		<link>http://cretaceouslabs.com/blog/2009/02/ruby-in-etch/</link>
		<comments>http://cretaceouslabs.com/blog/2009/02/ruby-in-etch/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 03:24:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2009/02/ruby-in-etch/</guid>
		<description><![CDATA[As some of you may know, Ruby and Debian&#8217;s ways of packaging software go together like Darwin and religious zealots&#8230;as in, they don&#8217;t. So how should you install Ruby when using Debian Stable? How do you stay up-to-date with new versions of Ruby and gems?
By using Backports, and not worrying so much.
First, setup your box [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you may know, Ruby and Debian&#8217;s ways of packaging software go together like Darwin and religious zealots&#8230;as in, <a href="http://pkg-ruby-extras.alioth.debian.org/rubygems.html">they don&#8217;t</a>. So how should you install Ruby when using Debian Stable? How do you stay up-to-date with new versions of Ruby and gems?</p>
<p>By using Backports, and not worrying so much.</p>
<p>First, setup your box to be able to install <a href="http://backports.org/dokuwiki/doku.php?id=instructions">backports of packages</a>. Next, uninstall any Ruby-related packages. Yeah, that&#8217;s right. Do you know why? Because, if you&#8217;re running Etch, you&#8217;re using Ruby 1.8.5! Egad! That&#8217;s almost medieval. Make sure to uninstall irb and rdoc, too.</p>
<p>Now it&#8217;s time to install shinier versions&#8230;1.8.7, to be exact:</p>
<pre>$ sudo aptitude install -t etch-backports libruby1.8 ruby1.8 rdoc1.8 irb1.8</pre>
<p>All of that should be on one line.</p>
<p>There you have it. Now you&#8217;ve got Ruby 1.8.7 on Debian Stable (AKA &#8220;Etch&#8221;). Ruby&#8217;s not complete without RubyGems, though. Download the <a href="http://rubyforge.org/frs/?group_id=126">latest version</a> to /usr/src/ , and extract it:</p>
<pre>$ cd /usr/src/$ wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz$ tar zxf rubygems-1.3.1.tgz</pre>
<p>Then run the setup utility:</p>
<pre>$ cd rubygems-1.3.1/$ sudo ruby setup.rb</pre>
<p>Let&#8217;s do one last thing to make life easier. The RubyGems setup script installed the &#8220;gem&#8221; command as /usr/bin/gem1.8 . I don&#8217;t want to have to type that &#8220;1.8&#8243; every time.</p>
<pre>$ cd /usr/local/bin/$ sudo ln -v -s /usr/bin/gem1.8 gem</pre>
<p>There. Now we can run &#8220;gem&#8221; like the rest of the world.</p>
<p>Easy? Yar.</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2009/02/ruby-in-etch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Default stegosaurses</title>
		<link>http://cretaceouslabs.com/blog/2009/01/default-stegosaurses/</link>
		<comments>http://cretaceouslabs.com/blog/2009/01/default-stegosaurses/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 00:10:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2009/01/default-stegosaurses/</guid>
		<description><![CDATA[Ever heard of Jonathan Viney&#8217;s active_record_defaults plugin? It&#8217;s fantastic. In a nutshell, it enables you to specify default values for model attributes. If an attribute isn&#8217;t set when initialising a new instance, the attribute&#8217;s set to whatever the default value is.
For example, let&#8217;s rebuild that Stegosaurus class from a previous post:
class Stegosaurus &#60; ActiveRecord::Base  [...]]]></description>
			<content:encoded><![CDATA[<p>Ever heard of Jonathan Viney&#8217;s <a href="http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/">active_record_defaults</a> plugin? It&#8217;s fantastic. In a nutshell, it enables you to specify default values for model attributes. If an attribute isn&#8217;t set when initialising a new instance, the attribute&#8217;s set to whatever the default value is.</p>
<p>For example, let&#8217;s rebuild that Stegosaurus class from a <a href="http://nick-hoffman.blogspot.com/2009/01/friends-dont-let-friends-code-without.html">previous post</a>:
<pre><span class="keyword">class </span><span class="class">Stegosaurus</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>  <span class="ident">attr_accessible</span> <span class="symbol">:tail_spikes</span><span class="punct">,</span> <span class="symbol">:back_plates</span>

  <span class="ident">validates_numericality_of</span> <span class="symbol">:tail_spikes</span><span class="punct">,</span>    <span class="symbol"> <img src='http://cretaceouslabs.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_integer</span>             <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>    <span class="symbol">:greater_than_or_equal_to</span> <span class="punct">=&gt;</span> <span class="number">0</span>

  <span class="ident">validates_numericality_of</span> <span class="symbol">:back_plates</span><span class="punct">,</span>    <span class="symbol"> <img src='http://cretaceouslabs.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_integer</span>             <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>    <span class="symbol">:greater_than_or_equal_to</span> <span class="punct">=&gt;</span> <span class="number">0</span><span class="keyword">end</span></pre>
<p>With that, if we created a new Stegosaurus and didn&#8217;t specify any attributes, not only would it have no tail spikes or back plates, but the model instance wouldn&#8217;t be valid.</p>
<p>To fix that, all we need to do is add two lines:
<pre><span class="keyword">class </span><span class="class">Stegosaurus</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>  <span class="ident">attr_accessible</span> <span class="symbol">:tail_spikes</span><span class="punct">,</span> <span class="symbol">:back_plates</span>

  <span class="ident">default</span> <span class="symbol">:tail_spikes</span> <span class="punct">=&gt;</span> <span class="number">4</span>  <span class="ident">default</span> <span class="symbol">:back_plates</span> <span class="punct">=&gt;</span> <span class="number">6</span>

  <span class="ident">validates_numericality_of</span> <span class="symbol">:tail_spikes</span><span class="punct">,</span>    <span class="symbol"> <img src='http://cretaceouslabs.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_integer</span>             <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>    <span class="symbol">:greater_than_or_equal_to</span> <span class="punct">=&gt;</span> <span class="number">0</span>

  <span class="ident">validates_numericality_of</span> <span class="symbol">:back_plates</span><span class="punct">,</span>    <span class="symbol"> <img src='http://cretaceouslabs.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_integer</span>             <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>    <span class="symbol">:greater_than_or_equal_to</span> <span class="punct">=&gt;</span> <span class="number">0</span><span class="keyword">end</span></pre>
<p>And voila, new Stegosaurus objects will automatically have 4 tail spikes and 6 back plates:
<pre>&gt;&gt; s = Stegosaurus.new=&gt; #&lt;Stegosaurus tail_spikes: 4, back_plates: 6&gt;&gt;&gt; ?&gt; s.tail_spikes=&gt; 4&gt;&gt; s.back_plates=&gt; 6</pre>
<p>However, if used improperly, this can lead to several hours of pounding your head on your desk. Note that that the default values we used above, 4 and 6, are immutable objects. You can&#8217;t change them. You can perform operations on them, such as addition (4 + 1), but that never <b>changes</b> the object.</p>
<p>Say we want our Stegosaurus class to have an array specifying its diet that defaults to &#8220;fern&#8221; and &#8220;cycad&#8221;:
<pre><span class="keyword">class </span><span class="class">Stegosaurus</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>  <span class="ident">attr_accessible</span> <span class="symbol">:tail_spikes</span><span class="punct">,</span> <span class="symbol">:back_plates</span><span class="punct">,</span> <span class="symbol">:diet</span>

  <span class="ident">default</span> <span class="symbol">:tail_spikes</span> <span class="punct">=&gt;</span> <span class="number">4</span>  <span class="ident">default</span> <span class="symbol">:back_plates</span> <span class="punct">=&gt;</span> <span class="number">6</span>  <span class="ident">default</span> <span class="symbol">:diet</span> <span class="punct">=&gt;</span> <span class="punct">%w(</span><span class="string">fern cycad</span><span class="punct">)</span>

  <span class="ident">validates_numericality_of</span> <span class="symbol">:tail_spikes</span><span class="punct">,</span>    <span class="symbol"> <img src='http://cretaceouslabs.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_integer</span>             <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>    <span class="symbol">:greater_than_or_equal_to</span> <span class="punct">=&gt;</span> <span class="number">0</span>

  <span class="ident">validates_numericality_of</span> <span class="symbol">:back_plates</span><span class="punct">,</span>    <span class="symbol"> <img src='http://cretaceouslabs.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly_integer</span>             <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>    <span class="symbol">:greater_than_or_equal_to</span> <span class="punct">=&gt;</span> <span class="number">0</span><span class="keyword">end</span></pre>
<p>That looks right. But watch this:
<pre>=&gt; charles = Stegosaurus.new=&gt; #&lt;Stegosaurus tail_spikes: 4, back_plates: 6, diet: ["fern", "cycad"]&gt;&gt;&gt; ?&gt; arnold = Stegosaurus.new=&gt; #&lt;Stegosaurus tail_spikes: 4, back_plates: 6, diet: ["fern", "cycad"]&gt;&gt;&gt; ?&gt; charles.diet < < 'moss'=&gt; ["fern", "cycad", "moss"]&gt;&gt; ?&gt; arnold.diet=&gt; ["fern", "cycad", "moss"]</pre>
<p>We didn&#8217;t mean to do that&#8230;Arnold doesn&#8217;t want to eat moss!</p>
<p>At the moment, when the Stegosaurus class is initialised, it creates one Array for the default &#8220;diet&#8221; attribute of all future Stegosaurus objects. In other words, Charles&#8217; and Arnolds&#8217; &#8220;diet&#8221; attributes were references to the same object:
<pre>&gt;&gt; charles.diet.object_id=&gt; 19103090&gt;&gt; ?&gt; arnold.diet.object_id=&gt; 19103090</pre>
<p>How do we fix that? We tell the Stegosaurus class to create a new Array for each model instance. Simply change this:
<pre><span class="ident">default</span> <span class="symbol">:diet</span> <span class="punct">=&gt;</span> <span class="punct">%w(</span><span class="string">fern cycad</span><span class="punct">)</span></pre>
<p>to this:
<pre><span class="ident">default</span> <span class="symbol">:diet</span> <span class="punct">=&gt;</span> <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span> <span class="punct">%w(</span><span class="string">fern cycad</span><span class="punct">)</span> <span class="punct">}</span></pre>
<p>Now each time we create a new Stegosaurus, that Proc will fire, and create a new &#8220;diet&#8221; Array:
<pre>=&gt; charles = Stegosaurus.new=&gt; #&lt;Stegosaurus tail_spikes: 4, back_plates: 6, diet: ["fern", "cycad"]&gt;&gt;&gt; ?&gt; arnold = Stegosaurus.new=&gt; #&lt;Stegosaurus tail_spikes: 4, back_plates: 6, diet: ["fern", "cycad"]&gt;&gt;&gt; ?&gt; charles.diet < < 'moss'=&gt; ["fern", "cycad", "moss"]&gt;&gt; ?&gt; arnold.diet=&gt; ["fern", "cycad"]</pre>
<p>I highly recommend giving <a href="http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/">active_record_defaults</a> a try. It&#8217;s very handy, and very easy to use.</p>
<p>And by the way, yes, I was bitten by this problem. Why else would I be writing about it! Can you guess how I discovered it, though? &#8230; My <a href="http://nick-hoffman.blogspot.com/2009/01/testingits-what-lifes-all-about.html">tests</a> picked it up!</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2009/01/default-stegosaurses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Friends don’t let friends code without tests</title>
		<link>http://cretaceouslabs.com/blog/2009/01/friends-don%e2%80%99t-let-friends-code-without-tests/</link>
		<comments>http://cretaceouslabs.com/blog/2009/01/friends-don%e2%80%99t-let-friends-code-without-tests/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 17:54:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2009/01/friends-don%e2%80%99t-let-friends-code-without-tests/</guid>
		<description><![CDATA[But what if your friends don&#8217;t write tests? Or what if you&#8217;re just lazy? Well, if the latter applies, that&#8217;s your own damn fault. But if nobody around you tests their code, you&#8217;re probably not going to be motivated to test your own code. But, fear not! RSpec is here!
RSpec is a really awesome testing [...]]]></description>
			<content:encoded><![CDATA[<p>But what if your friends don&#8217;t write tests? Or what if you&#8217;re just lazy? Well, if the latter applies, that&#8217;s your own damn fault. But if nobody around you tests their code, you&#8217;re probably not going to be motivated to test your own code. But, fear not! RSpec is here!</p>
<p>RSpec is a really awesome testing framework. By &#8220;awesome&#8221;, I mean that it:
<ul>
<li>makes it easy to write tests;</li>
<li>makes your tests easy to read;</li>
<li>gives you an easy development path to follow.</li>
</ul>
<p>It accomplishes this by following <a href="http://209.85.229.132/search?q=cache:r-hQvNP_81YJ:dannorth.net/introducing-bdd+behaviour+driven+development&amp;hl=en&amp;ct=clnk&amp;cd=6" title="Dan North's site seems to be having problems at the moment, so we have to resort to Google's cache.">behaviour-driven development (BDD)</a>. If you&#8217;re not a keener, don&#8217;t bother following that link. Just read on for the stegosaurus.</p>
<p>With BDD, you write specs that describe your application&#8217;s behaviour in terms of what it <span style="font-weight: bold;">should</span> and <span style="font-weight: bold;">shouldn&#8217;t</span> do.</p>
<p>Let&#8217;s write an app to mimic one of my favourite dinosaurs. And while we&#8217;re at it, we&#8217;ll do some testing. But first, install RSpec:
<pre><span class="ident">sudo</span> <span class="ident">gem</span> <span class="ident">install</span> <span class="ident">rspec</span></pre>
<p>Stegosauruses are known for 2 things. Let&#8217;s start with those awesome tail spikes:
<pre><span class="keyword">class </span><span class="class">Stegosaurus</span><span class="ident">  attr_accessor</span> <span class="symbol">:tail_spikes</span><span class="keyword">end</span></pre>
<p>That&#8217;s pretty simple. So how do we spec this? We start off by describing what we want our application to do. In this case, we should be able to grab the &#8220;tail_spikes&#8221; attribute:
<pre><span class="ident">require</span> <span class="punct">'</span><span class="string">stegosaurus</span><span class="punct">'</span>

<span class="ident">describe</span> <span class="constant">Stegosaurus</span> <span class="keyword">do</span><span class="ident">it</span> <span class="punct">'</span><span class="string">should have tail spikes</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">steggie</span> <span class="punct">=</span> <span class="constant">Stegosaurus</span><span class="punct">.</span><span class="ident">new</span>

  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes</span>  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes=</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>Run the spec, and we&#8217;re off to the races!
<pre>$ spec stegosaurus_spec.rb.

Finished in 0.015328 seconds

1 example, 0 failures</pre>
<p>We should also spec setting the number of tail spikes, so let&#8217;s update the spec:
<pre><span class="ident">require</span> <span class="punct">'</span><span class="string">stegosaurus</span><span class="punct">'</span>

<span class="ident">describe</span> <span class="constant">Stegosaurus</span> <span class="keyword">do</span><span class="ident">it</span> <span class="punct">'</span><span class="string">should have tail spikes</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">steggie</span> <span class="punct">=</span> <span class="constant">Stegosaurus</span><span class="punct">.</span><span class="ident">new</span>

  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes</span>  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes=</span>

  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span> <span class="punct">=</span> <span class="number">4</span>  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">4</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>That&#8217;s one of the most readable and easy-to-understand tests you&#8217;ve ever seen, eh?</p>
<p>Stegosauruses are also known for the huge plates on their backs:
<pre><span class="keyword">class </span><span class="class">Stegosaurus</span><span class="ident">attr_accessor</span> <span class="symbol">:tail_spikes</span><span class="ident">attr_accessor</span> <span class="symbol">:plates</span><span class="keyword">end</span></pre>
<p>The spec should be pretty obvious:
<pre><span class="ident">require</span> <span class="punct">'</span><span class="string">stegosaurus</span><span class="punct">'</span>

<span class="ident">describe</span> <span class="constant">Stegosaurus</span> <span class="keyword">do</span><span class="ident">it</span> <span class="punct">'</span><span class="string">should have tail spikes</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">steggie</span> <span class="punct">=</span> <span class="constant">Stegosaurus</span><span class="punct">.</span><span class="ident">new</span>

  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes</span>  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes=</span>

  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span> <span class="punct">=</span> <span class="number">4</span>  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">4</span><span class="keyword">end</span>

<span class="ident">it</span> <span class="punct">'</span><span class="string">should have plates</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">steggie</span> <span class="punct">=</span> <span class="constant">Stegosaurus</span><span class="punct">.</span><span class="ident">new</span>

  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:plates</span>  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:plates=</span>

  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">plates</span> <span class="punct">=</span> <span class="number">12</span>  <span class="ident">steggie</span><span class="punct">.</span><span class="ident">plates</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">12</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>Notice, though, that we&#8217;re beginning to repeat ourselves. In both of those examples (#it blocks), we create a new Stegosaurus. Let&#8217;s tidy that up a bit:
<pre><span class="ident">require</span> <span class="punct">'</span><span class="string">stegosaurus</span><span class="punct">'</span>

<span class="ident">describe</span> <span class="constant">Stegosaurus</span> <span class="keyword">do</span><span class="ident">before</span> <span class="symbol">:each</span> <span class="keyword">do</span>  <span class="attribute">@steggie</span> <span class="punct">=</span> <span class="constant">Stegosaurus</span><span class="punct">.</span><span class="ident">new</span><span class="keyword">end</span>

<span class="ident">it</span> <span class="punct">'</span><span class="string">should have tail spikes</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes</span>  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:tail_spikes=</span>

  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span> <span class="punct">=</span> <span class="number">4</span>  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">4</span><span class="keyword">end</span>

<span class="ident">it</span> <span class="punct">'</span><span class="string">should have plates</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:plates</span>  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">respond_to</span> <span class="symbol">:plates=</span>

  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span> <span class="punct">=</span> <span class="number">12</span>  <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">12</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>What we did there is tell RSpec to create a new Stegosaurus before each example is run. To make the stegosaurus available to the examples, it simply needs to be an instance variable.</p>
<p>When running your specs, there are a few different ways to format the output. I tend to use the default and &#8220;specdoc&#8221; formats. Currently, our spec&#8217;s output looks like this:
<pre>$ spec stegosaurus_spec.rb..

Finished in 0.01591 seconds

2 examples, 0 failures</pre>
<p>But we can also tell RSpec to spit out a summary of our specs:
<pre>$ spec stegosaurus_spec.rb --format specdoc

Stegosaurus- should have tail spikes- should have plates

Finished in 0.057443 seconds

2 examples, 0 failures</pre>
<p>Our Stegosaurus class is feeling a bit weird, though..technically, we can set the &#8220;tail_spikes&#8221; and &#8220;plates&#8221; attributes to any object:
<pre>$ irb&gt;&gt; require 'stegosaurus'=&gt; true&gt;&gt;?&gt; steggie = Stegosaurus.new=&gt; #&lt;Stegosaurus:0x602afc&gt;&gt;&gt;?&gt; steggie.tail_spikes = "we don't want to be able to do this!"=&gt; "we don't want to be able to do this!"</pre>
<p>So we need to restrict those two attributes to integers:
<pre><span class="keyword">class </span><span class="class">Stegosaurus</span><span class="ident">attr_reader</span> <span class="symbol">:tail_spikes</span><span class="ident">attr_reader</span> <span class="symbol">:plates</span>

<span class="keyword">def </span><span class="method">tail_spikes=</span><span class="punct">(</span><span class="ident">number_of_tail_spikes</span><span class="punct">)</span>  <span class="keyword">raise</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_tail_spikes) must be a Fixnum.</span><span class="punct">'</span> <span class="keyword">unless</span> <span class="ident">number_of_tail_spikes</span><span class="punct">.</span><span class="ident">is_a?</span> <span class="constant">Fixnum</span>

  <span class="attribute">@tail_spikes</span> <span class="punct">=</span> <span class="ident">number_of_tail_spikes</span><span class="keyword">end</span>

<span class="keyword">def </span><span class="method">plates=</span><span class="punct">(</span><span class="ident">number_of_plates</span><span class="punct">)</span>  <span class="keyword">raise</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_plates) must be a Fixnum.</span><span class="punct">'</span> <span class="keyword">unless</span> <span class="ident">number_of_plates</span><span class="punct">.</span><span class="ident">is_a?</span> <span class="constant">Fixnum</span>

  <span class="attribute">@plates</span> <span class="punct">=</span> <span class="ident">number_of_plates</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>With that done, what do we need to change in our specs? We need to spec the behaviour of the 2 new setter methods:
<pre><span class="ident">require</span> <span class="punct">'</span><span class="string">stegosaurus</span><span class="punct">'</span>

<span class="ident">describe</span> <span class="constant">Stegosaurus</span> <span class="keyword">do</span><span class="ident">before</span> <span class="symbol">:each</span> <span class="keyword">do</span>  <span class="attribute">@steggie</span> <span class="punct">=</span> <span class="constant">Stegosaurus</span><span class="punct">.</span><span class="ident">new</span><span class="keyword">end</span>

<span class="ident">describe</span> <span class="punct">'</span><span class="string">"tail_spikes" attribute</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">it</span> <span class="punct">'</span><span class="string">can only be set to an integer</span><span class="punct">'</span> <span class="keyword">do</span>    <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span>      <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">asdf</span><span class="punct">'</span>      <span class="punct">}.</span><span class="ident">should</span> <span class="ident">raise_error</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_tail_spikes) must be a Fixnum.</span><span class="punct">'</span>

    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span> <span class="punct">=</span> <span class="number">4</span>    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">4</span>  <span class="keyword">end</span><span class="keyword">end</span>

<span class="ident">describe</span> <span class="punct">'</span><span class="string">"plates" attribute</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">it</span> <span class="punct">'</span><span class="string">can only be set to an integer</span><span class="punct">'</span> <span class="keyword">do</span>    <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span>      <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">asdf</span><span class="punct">'</span>      <span class="punct">}.</span><span class="ident">should</span> <span class="ident">raise_error</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_plates) must be a Fixnum.</span><span class="punct">'</span>

    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span> <span class="punct">=</span> <span class="number">12</span>    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">12</span>  <span class="keyword">end</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>Most of that is pretty self-explanatory. However, notice that we&#8217;ve just nested #describe inside of the original #describe&#8230;twice! We do this because it makes are specs read more fluidly, and it groups examples (#it blocks) together. And inside these inner-#describes, we can do anything we talked about earlier, like setup a before-block.</p>
<p>Let&#8217;s add one more attribute&#8230;a name:
<pre><span class="keyword">class </span><span class="class">Stegosaurus</span><span class="ident">attr_reader</span> <span class="symbol">:tail_spikes</span><span class="punct">,</span> <span class="symbol">:plates</span><span class="punct">,</span> <span class="symbol">:name</span>

<span class="keyword">def </span><span class="method">tail_spikes=</span><span class="punct">(</span><span class="ident">number_of_tail_spikes</span><span class="punct">)</span>  <span class="keyword">raise</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_tail_spikes) must be a Fixnum.</span><span class="punct">'</span> <span class="keyword">unless</span> <span class="ident">number_of_tail_spikes</span><span class="punct">.</span><span class="ident">is_a?</span> <span class="constant">Fixnum</span>

  <span class="attribute">@tail_spikes</span> <span class="punct">=</span> <span class="ident">number_of_tail_spikes</span><span class="keyword">end</span>

<span class="keyword">def </span><span class="method">plates=</span><span class="punct">(</span><span class="ident">number_of_plates</span><span class="punct">)</span>  <span class="keyword">raise</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_plates) must be a Fixnum.</span><span class="punct">'</span> <span class="keyword">unless</span> <span class="ident">number_of_plates</span><span class="punct">.</span><span class="ident">is_a?</span> <span class="constant">Fixnum</span>

  <span class="attribute">@plates</span> <span class="punct">=</span> <span class="ident">number_of_plates</span><span class="keyword">end</span>

<span class="keyword">def </span><span class="method">name=</span><span class="punct">(</span><span class="ident">name</span><span class="punct">)</span>  <span class="keyword">raise</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (name) must be a String.</span><span class="punct">'</span> <span class="keyword">unless</span> <span class="ident">name</span><span class="punct">.</span><span class="ident">is_a?</span> <span class="constant">String</span>

  <span class="attribute">@name</span> <span class="punct">=</span> <span class="ident">name</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>And now the specs look like:
<pre><span class="ident">require</span> <span class="punct">'</span><span class="string">stegosaurus</span><span class="punct">'</span>

<span class="ident">describe</span> <span class="constant">Stegosaurus</span> <span class="keyword">do</span><span class="ident">before</span> <span class="symbol">:each</span> <span class="keyword">do</span>  <span class="attribute">@steggie</span> <span class="punct">=</span> <span class="constant">Stegosaurus</span><span class="punct">.</span><span class="ident">new</span><span class="keyword">end</span>

<span class="ident">describe</span> <span class="punct">'</span><span class="string">"tail_spikes" attribute</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">it</span> <span class="punct">'</span><span class="string">can only be set to an integer</span><span class="punct">'</span> <span class="keyword">do</span>    <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span>      <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">asdf</span><span class="punct">'</span>    <span class="punct">}.</span><span class="ident">should</span> <span class="ident">raise_error</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_tail_spikes) must be a Fixnum.</span><span class="punct">'</span>

    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span> <span class="punct">=</span> <span class="number">4</span>    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">tail_spikes</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">4</span>  <span class="keyword">end</span><span class="keyword">end</span>

<span class="ident">describe</span> <span class="punct">'</span><span class="string">"plates" attribute</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">it</span> <span class="punct">'</span><span class="string">can only be set to an integer</span><span class="punct">'</span> <span class="keyword">do</span>    <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span>      <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">asdf</span><span class="punct">'</span>    <span class="punct">}.</span><span class="ident">should</span> <span class="ident">raise_error</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (number_of_plates) must be a Fixnum.</span><span class="punct">'</span>

    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span> <span class="punct">=</span> <span class="number">12</span>    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">plates</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="number">12</span>  <span class="keyword">end</span><span class="keyword">end</span>

<span class="ident">describe</span> <span class="punct">'</span><span class="string">"name" attribute</span><span class="punct">'</span> <span class="keyword">do</span>  <span class="ident">it</span> <span class="punct">'</span><span class="string">can only be set to a string</span><span class="punct">'</span> <span class="keyword">do</span>    <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span>      <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">name</span> <span class="punct">=</span> <span class="punct">['</span><span class="string">not valid</span><span class="punct">']</span>    <span class="punct">}.</span><span class="ident">should</span> <span class="ident">raise_error</span> <span class="constant">ArgumentError</span><span class="punct">,</span> <span class="punct">'</span><span class="string">The first argument (name) must be a String.</span><span class="punct">'</span>

    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">Rupert</span><span class="punct">'</span>    <span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">name</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="punct">'</span><span class="string">Rupert</span><span class="punct">'</span>  <span class="keyword">end</span><span class="keyword">end</span><span class="keyword">end</span></pre>
<p>Let&#8217;s run that spec now:
<pre>$ spec stegosaurus_spec.rb..F

1)'Stegosaurus "name" attribute can only be set to a string' FAILEDexpected "Rupert", got "Rupert" (using .equal?)./stegosaurus_spec.rb:37:

Finished in 0.02225 seconds

3 examples, 1 failure</pre>
<p>Hrmmm, that&#8217;s no good. But that&#8217;s a fairly useful error message, eh?&#8230;well, sort of. It&#8217;s telling us what it expected, and what it received. The thing is, it expected &#8220;Rupert&#8221; and got &#8220;Rupert&#8221;, so what&#8217;s going on? The problem lies in the fact that you can&#8217;t use #equal? to compare strings, and that&#8217;s exactly what we&#8217;re doing. If you don&#8217;t believe me, try it for yourself:
<pre>$ irb&gt;&gt; 'Rupert'.equal? 'Rupert'=&gt; false</pre>
<p>BTW, did you notice the &#8220;(using .equal?)&#8221; hint that RSpec gave us?</p>
<p>Enough banter. All we need to do is change this:
<pre><span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">name</span><span class="punct">.</span><span class="ident">should</span> <span class="ident">equal</span> <span class="punct">'</span><span class="string">Rupert</span><span class="punct">'</span></pre>
<p>to this:
<pre><span class="attribute">@steggie</span><span class="punct">.</span><span class="ident">name</span><span class="punct">.</span><span class="ident">should</span> <span class="punct">==</span> <span class="punct">'</span><span class="string">Rupert</span><span class="punct">'</span></pre>
<p>Shall we view our specs in all their glory now?
<pre>$  spec stegosaurus_spec.rb --format specdoc

Stegosaurus "tail_spikes" attribute- can only be set to an integer

Stegosaurus "plates" attribute- can only be set to an integer

Stegosaurus "name" attribute- can only be set to a string

Finished in 0.018755 seconds

3 examples, 0 failures</pre>
<p>Those&#8217;re the absolute basics of RSpec. If you&#8217;re thirsty for more, check out the <a href="http://www.rspec.info/">RSpec website</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2009/01/friends-don%e2%80%99t-let-friends-code-without-tests/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Testing&#8230;it&#8217;s what life&#8217;s all about</title>
		<link>http://cretaceouslabs.com/blog/2009/01/testing-its-what-lifes-all-about/</link>
		<comments>http://cretaceouslabs.com/blog/2009/01/testing-its-what-lifes-all-about/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 03:05:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2009/01/testing-its-what-lifes-all-about/</guid>
		<description><![CDATA[I just read a great call to action by John Nunemaker. He wants to see everyone writing tests for their code, and has decided to &#8220;post like a superhuman fiend until everyone is testing&#8221;. He&#8217;s on to something!
With that said, I&#8217;m joining in. I primarily use RSpec, so I&#8217;ll be focussing on that, but we&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>I just read a great <a href="http://railstips.org/2009/1/6/test-or-die">call to action</a> by John Nunemaker. He wants to see everyone writing tests for their code, and has decided to &#8220;post like a superhuman fiend until everyone is testing&#8221;. He&#8217;s on to something!</p>
<p>With that said, I&#8217;m joining in. I primarily use <a href="http://www.rspec.info/">RSpec</a>, so I&#8217;ll be focussing on that, but we&#8217;ll give <a href="http://wiki.github.com/aslakhellesoy/cucumber/">Cucumber</a> and other tools a go, and we&#8217;ll be starting with the basics.</p>
<p>So subscribe to our feeds, and stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2009/01/testing-its-what-lifes-all-about/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Resources:   :as   vs   :controller</title>
		<link>http://cretaceouslabs.com/blog/2008/11/rails-resources-%c2%a0%c2%a0as-%c2%a0%c2%a0vs-%c2%a0%c2%a0controller/</link>
		<comments>http://cretaceouslabs.com/blog/2008/11/rails-resources-%c2%a0%c2%a0as-%c2%a0%c2%a0vs-%c2%a0%c2%a0controller/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 18:10:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2008/11/rails-resources-%c2%a0%c2%a0as-%c2%a0%c2%a0vs-%c2%a0%c2%a0controller/</guid>
		<description><![CDATA[As you may or may not know, when configuring routes and resources in config/routes.rb , you can pass the :as or :controller options to map.resources . When I first read about these two options in the Rails Routing From The Outside In Rails Guide, they seemed to do the exact same thing.
However, that&#8217;s not the [...]]]></description>
			<content:encoded><![CDATA[<p>As you may or may not know, when configuring routes and resources in config/routes.rb , you can pass the :as or :controller options to map.resources . When I first read about these two options in the <a href="http://guides.rubyonrails.org/routing_outside_in.html">Rails Routing From The Outside In</a> Rails Guide, they seemed to do the exact same thing.</p>
<p>However, that&#8217;s not the case at all.
<pre><span class="ident">map</span><span class="punct">.</span><span class="ident">resources</span> <span class="symbol">:photos</span><span class="punct">,</span> <span class="symbol">:as</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">images</span><span class="punct">'</span></pre>
<p>This allows you to request the &#8220;photos&#8221; resource using the word &#8220;images&#8221;. IE: http://localhost:3000/images/ would route to the &#8220;photos&#8221; controller.</p>
<p>On the other hand,
<pre><span class="ident">map</span><span class="punct">.</span><span class="ident">resources</span> <span class="symbol">:photos</span><span class="punct">,</span> <span class="symbol">:controller</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">images</span><span class="punct">'</span></pre>
<p>This allows you to request the &#8220;images&#8221; resource using the word &#8220;photos&#8221;. IE: http://localhost:3000/photos/ would route to the &#8220;images&#8221; controller.</p>
<p>Simple? Yes. Subtle? Yes.</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2008/11/rails-resources-%c2%a0%c2%a0as-%c2%a0%c2%a0vs-%c2%a0%c2%a0controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convention and reasoning</title>
		<link>http://cretaceouslabs.com/blog/2008/11/convention-and-reasoning/</link>
		<comments>http://cretaceouslabs.com/blog/2008/11/convention-and-reasoning/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 18:28:00 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Conventions]]></category>

		<guid isPermaLink="false">http://cretaceouslabs.com/blog/2008/11/convention-and-reasoning/</guid>
		<description><![CDATA[When coding in the past, I rarely used the switch/select/case-conditional. However, since I started using Ruby, I&#8217;ve found it to be quite useful in some situations.
Vim formats them like this:
case foobarwhen 'something' # Do something.when 'another thing' # Do another thing.else # Do this if the above "when"s don't match.end
That indentation scheme doesn&#8217;t feel right [...]]]></description>
			<content:encoded><![CDATA[<p>When coding in the past, I rarely used the switch/select/case-conditional. However, since I started using Ruby, I&#8217;ve found it to be quite useful in some situations.</p>
<p>Vim formats them like this:
<pre><span class="keyword">case</span> <span class="ident">foobar</span><span class="keyword">when</span> <span class="punct">'</span><span class="string">something</span><span class="punct">'</span> <span class="comment"># Do something.</span><span class="keyword">when</span> <span class="punct">'</span><span class="string">another thing</span><span class="punct">'</span> <span class="comment"># Do another thing.</span><span class="keyword">else</span> <span class="comment"># Do this if the above "when"s don't match.</span><span class="keyword">end</span></pre>
<p>That indentation scheme doesn&#8217;t feel right to me. The &#8220;when&#8221; and &#8220;else&#8221; clauses are pretty much children of the &#8220;case&#8221; clause. In my opinion, &#8220;when&#8221; and &#8220;else&#8221; should be indented +1, like this:
<pre><span class="keyword">case</span> <span class="ident">foobar</span> <span class="keyword">when</span> <span class="punct">'</span><span class="string">something</span><span class="punct">'</span>   <span class="comment"># Do something.</span> <span class="keyword">when</span> <span class="punct">'</span><span class="string">another thing</span><span class="punct">'</span>   <span class="comment"># Do another thing.</span> <span class="keyword">else</span>   <span class="comment"># Do this if the above "when"s don't match.</span><span class="keyword">end</span></pre>
<p>But now the code within each &#8220;when&#8221; and &#8220;else&#8221; is indented +2! That&#8217;s a lot of indentation.</p>
<p>I brought this up in #ruby-lang on Freenode. A couple of people agreed with me, a couple didn&#8217;t care, and the rest disagreed. There was one poignant comment though:
<pre class="code-block"><code>&lt;quix&gt; litage: why doesn't your reasoning for if/elsif apply to case/when?</code></pre>
<p>I haven&#8217;t been able to answer that yet. quix&#8217;s reasoning is sound, but I can&#8217;t shake the dirtiness that I feel when indenting &#8220;when&#8221; and &#8220;else&#8221; to the same level as &#8220;case&#8221;. It just feels wrong&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://cretaceouslabs.com/blog/2008/11/convention-and-reasoning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

