<?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>One Man's Walk in work &#187; ruby</title>
	<atom:link href="http://onemanswalk.com/work/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://onemanswalk.com/work</link>
	<description>jeremy lightsmith on agile, ruby, and consulting</description>
	<lastBuildDate>Wed, 04 May 2011 04:29:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Array.to_hash() in Ruby</title>
		<link>http://onemanswalk.com/work/2009/08/04/arrayto_hash-in-ruby/</link>
		<comments>http://onemanswalk.com/work/2009/08/04/arrayto_hash-in-ruby/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 01:40:22 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://onemanswalk.com/work/?p=171</guid>
		<description><![CDATA[	I often find myself wanting this method. This is my 3rd or 4th writing of it &#8211; it&#8217;s shorter this time. Inject is my new best friend.

	
class Array
  def to_hash
    inject&#40;&#123;&#125;&#41; &#123;&#124;hash, i&#124; hash&#91;i&#91;0&#93;&#93; = i&#91;1&#93;; hash&#125;
  end
end




	What this snippet does is take an array and turn it into a [...]]]></description>
			<content:encoded><![CDATA[	<p>I often find myself wanting this method. This is my 3rd or 4th writing of it &#8211; it&#8217;s shorter this time. Inject is my new best friend.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_hash
    inject<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>hash, i<span style="color:#006600; font-weight:bold;">|</span> hash<span style="color:#006600; font-weight:bold;">&#91;</span>i<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;">&#93;</span> = i<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>; hash<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>



	<p>What this snippet does is take an array and turn it into a hash, like so</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;apple&quot;</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;banana&quot;</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;citrus&quot;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 
    <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;apple&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#996600;">&quot;banana&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>, <span style="color:#996600;">&quot;citrus&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>
</p>



	<p>If I didn&#8217;t have to deal with the case where there may be subarrays, I&#8217;d use <a href="http://pivotallabs.com/users/nick/blog/articles/category/109-fun" title="">nick&#8217;s approach</a></p>

	<p>
<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:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>
</p>



	<p>My solution isn&#8217;t that much more code, and handles the case of subarrays.</p>

	<p><i>Update</i></p>

	<p>Ola makes the good point that this is actually the best of both worlds :</p>

	<p>
<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:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>
</p>



	<p>Thanks Ola.  &#8230;I still think inject is cool, though <img src='http://onemanswalk.com/work/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2009/08/04/arrayto_hash-in-ruby/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Stop running test:unit tests when using rspec</title>
		<link>http://onemanswalk.com/work/2009/01/16/stop-running-testunit-tests-when-using-rspec/</link>
		<comments>http://onemanswalk.com/work/2009/01/16/stop-running-testunit-tests-when-using-rspec/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 17:53:43 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://onemanswalk.com/work/?p=67</guid>
		<description><![CDATA[	You know those 3 lines that show up every time you do an rspec run?

	
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test &#34;/Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb&#34;  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test &#34;/Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb&#34;  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test &#34;/Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb&#34;




	&#8230;yeah, those lines.  I hate those lines.

	So let&#8217;s get rid of them!

	Each of those lines is rails trying to run test:unit.  RSpec replaces the default rake target by doing [...]]]></description>
			<content:encoded><![CDATA[	<p>You know those 3 lines that show up every time you do an rspec run?</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="dos">/System/Library/Frameworks/Ruby.framework/Versions/<span style="color: #cc66cc;">1.8</span>/usr/bin/ruby -Ilib:test &quot;/Library/Ruby/Gems/<span style="color: #cc66cc;">1.8</span>/gems/rake<span style="color: #cc66cc;">-0.8</span><span style="color: #cc66cc;">.3</span>/lib/rake/rake_test_loader.rb&quot;  
/System/Library/Frameworks/Ruby.framework/Versions/<span style="color: #cc66cc;">1.8</span>/usr/bin/ruby -Ilib:test &quot;/Library/Ruby/Gems/<span style="color: #cc66cc;">1.8</span>/gems/rake<span style="color: #cc66cc;">-0.8</span><span style="color: #cc66cc;">.3</span>/lib/rake/rake_test_loader.rb&quot;  
/System/Library/Frameworks/Ruby.framework/Versions/<span style="color: #cc66cc;">1.8</span>/usr/bin/ruby -Ilib:test &quot;/Library/Ruby/Gems/<span style="color: #cc66cc;">1.8</span>/gems/rake<span style="color: #cc66cc;">-0.8</span><span style="color: #cc66cc;">.3</span>/lib/rake/rake_test_loader.rb&quot;</pre></div></div>
</p>



	<p>&#8230;yeah, those lines.  I hate those lines.</p>

	<p>So let&#8217;s get rid of them!</p>

	<p>Each of those lines is rails trying to run test:unit.  RSpec replaces the default rake target by doing this:</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">task <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:spec</span></pre></div></div>
</p>



	<p><span class="caps">BUT</span><img src="!" alt="" border="0" />  If you&#8217;ve played with rake before, you know that this doesn&#8217;t actually replace the default target, it only adds to it.  We need to remove the default target <em>then</em> point it at :spec</p>

	<p>Turns out that&#8217;s not too hard.  Put this code (got it from <a href="http://rubyizednrailified.blogspot.com/2008/07/remove-rake-tasks.html" title="">here</a>) at the end of your Rakefile:</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#6666ff; font-weight:bold;">Rake::TaskManager</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> remove_task<span style="color:#006600; font-weight:bold;">&#40;</span>task_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@tasks</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>task_name.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Rake.<span style="color:#9900CC;">application</span>.<span style="color:#9900CC;">remove_task</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;default&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
task <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:spec</span></pre></div></div>
</p>



	<p>You&#8217;re all set!</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2009/01/16/stop-running-testunit-tests-when-using-rspec/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Eventual Consistency, or things will all work out&#8230;eventually</title>
		<link>http://onemanswalk.com/work/2008/08/05/eventual-consistency-or-things-will-all-work-out-eventually/</link>
		<comments>http://onemanswalk.com/work/2008/08/05/eventual-consistency-or-things-will-all-work-out-eventually/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 00:12:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dsls]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	So we&#8217;re working with Amazon&#8217;s SimpleDB.  It&#8217;s pretty sweet, though the ruby libraries for it are still a bit primitive.  One of the problems you run up against when you&#8217;re writing integration tests against it is eventual consistency.

	Take this test :

	
it &#34;should save&#34; do
  customer = Customer.create!&#40;:name =&#62; 'bob', :email =&#62; 'bob@example.com'&#41;
 [...]]]></description>
			<content:encoded><![CDATA[	<p>So we&#8217;re working with <a href="http://www.amazon.com/SimpleDB-AWS-Service-Pricing/b?ie=UTF8&#038;node=342335011" title="">Amazon&#8217;s SimpleDB</a>.  It&#8217;s pretty sweet, though the ruby libraries for it are still a bit primitive.  One of the problems you run up against when you&#8217;re writing integration tests against it is eventual consistency.</p>

	<p>Take this test :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">it <span style="color:#996600;">&quot;should save&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  customer = Customer.<span style="color:#9900CC;">create</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;">'bob'</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob@example.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  customer = Customer.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>customer.<span style="color:#9900CC;">key</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  customer.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'bob'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>The way SimpleDB works, you&#8217;re assured that Customer.find will work&#8230;eventually, but not right away.</p>

	<p>For a couple days we contented ourselves to just run the integration tests a couple times until they didn&#8217;t error out.  But that got old.</p>

	<p>Enter &#8220;eventually&#8221; :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">it <span style="color:#996600;">&quot;should save&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  customer = Customer.<span style="color:#9900CC;">create</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;">'bob'</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob@example.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  customer = eventually <span style="color:#006600; font-weight:bold;">&#123;</span> Customer.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>customer.<span style="color:#9900CC;">key</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  customer.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'bob'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>It&#8217;s a very simple method (below) that just retries the passed in block until it succeeds, timing out after 10 tries.  Super simple, works like a charm.  Thank you ruby.</p>

	<p>Here&#8217;s the source :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> eventually<span style="color:#006600; font-weight:bold;">&#40;</span>tries = <span style="color:#006666;">0</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">yield</span>
<span style="color:#9966CC; font-weight:bold;">rescue</span>
  <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#9966CC; font-weight:bold;">if</span> tries <span style="color:#006600; font-weight:bold;">&gt;</span>= <span style="color:#006666;">10</span>
  <span style="color:#CC0066; font-weight:bold;">sleep</span> <span style="color:#006666;">0.5</span>
  eventually<span style="color:#006600; font-weight:bold;">&#40;</span>tries <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>I will say, I can&#8217;t help but smile every time I write &#8220;eventually&#8221; in a test&#8230; <img src='http://onemanswalk.com/work/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2008/08/05/eventual-consistency-or-things-will-all-work-out-eventually/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clickable Stack Traces on your Rails Error Page</title>
		<link>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/</link>
		<comments>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 18:29:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Wouldn&#8217;t it be nice if when an error happened in your application, you could not only see the stack trace, but click on a line and jump to the offending code?  This is not groundbreaking stuff, I know, I had this like 10 years ago in C++ and later Java fat clients, and I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[	<p>Wouldn&#8217;t it be nice if when an error happened in your application, you could not only see the stack trace, but click on a line and jump to the offending code?  This is not groundbreaking stuff, I know, I had this like 10 years ago in C++ and later Java fat clients, and I&#8217;m sure other languages &#038; IDEs had it too &#8211; but somehow in moving to writing web apps in Ruby, I lost it.</p>

	<p>I want it back damnit!</p>

	<p>Turns out it&#8217;s pretty easy to get back (at least it is if you use textmate) &#8211; check it out.</p>

	<h2>Custom Error Page</h2>

	<p>First, to get a custom error page for your project, add something like this to your application.rb :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> rescue_action_locally<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  render <span style="color:#ff3333; font-weight:bold;">:template</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;application/public_error&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">alias</span> rescue_action_in_public render_action_locally</pre></div></div>
</p>



	<p>Note, if you&#8217;re using exception notifiable, you probably want to change the last line to something like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">alias</span> render_404 rescue_action_locally
<span style="color:#9966CC; font-weight:bold;">alias</span> render_500 rescue_action_locally</pre></div></div>
</p>



	<p>We use markaby, so our public_error template looks something like this; it&#8217;s probably a good idea to keep this simple and not use a layout, just in case the error came from the layout :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">html <span style="color:#9966CC; font-weight:bold;">do</span>
  head <span style="color:#9966CC; font-weight:bold;">do</span>
    title action_name
    stylesheet_link_tag <span style="color:#996600;">'error'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  body <span style="color:#9966CC; font-weight:bold;">do</span>
    div.<span style="color:#9900CC;">error</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      div.<span style="color:#9900CC;">message</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        h1 <span style="color:#996600;">&quot;Whoops&quot;</span>
&nbsp;
        p <span style="color:#996600;">&quot;We detected an error.  Don't worry, though, 
we've been notified and we're on it.&quot;</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>




	<h2>Adding a Stack Trace w/ Links to the Error Page</h2>

	<p>So, it would be helpful to us for our error page to tell us more in our development and staging environments.  We do use exception notifiable, so we don&#8217;t actually need or want it to say anything else to a real user in production.  Adding this to our template, it now looks like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">html <span style="color:#9966CC; font-weight:bold;">do</span>
  head <span style="color:#9966CC; font-weight:bold;">do</span>
    title action_name
    stylesheet_link_tag <span style="color:#996600;">'error'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  body <span style="color:#9966CC; font-weight:bold;">do</span>
    div.<span style="color:#9900CC;">error</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      div.<span style="color:#9900CC;">message</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        h1 <span style="color:#996600;">&quot;Whoops&quot;</span>
&nbsp;
        p <span style="color:#996600;">&quot;We detected an error.  Don't worry, though, 
we've been notified and we're on it.&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> RAILS_ENV != <span style="color:#996600;">'production'</span>
        div.<span style="color:#9900CC;">stack_trace</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          h2 <span style="color:#996600;">&quot;Stack Trace&quot;</span>
          div <span style="color:#006600; font-weight:bold;">&#123;</span> link_to_code $!.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>, <span style="color:#996600;">&quot;
&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
          hr
          div <span style="color:#006600; font-weight:bold;">&#123;</span> link_to_code $!.<span style="color:#9900CC;">backtrace</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;
&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</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>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>What&#8217;s that &#8220;link_to_code&#8221; method in there?</p>

	<p>It&#8217;s a method in application_helper that replaces any path with a textmate url to open up that file on your local system and jump to the offending line.  Check it out :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> link_to_code<span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>
  text.<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><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>\w\.<span style="color:#006600; font-weight:bold;">-</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;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>\w\<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:#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:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>match<span style="color:#006600; font-weight:bold;">|</span>
    file = $<span style="color:#006666;">1</span>.<span style="color:#9900CC;">starts_with</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? $<span style="color:#006666;">1</span> : <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, $<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    link_to match, <span style="color:#996600;">&quot;txmt://open?url=file://#{file}&amp;line=#{$2}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>That&#8217;s it.  Suddenly, stack traces are friendly again!</p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pretty Printing Seconds in Ruby</title>
		<link>http://onemanswalk.com/work/2007/10/16/pretty-printing-seconds-in-ruby/</link>
		<comments>http://onemanswalk.com/work/2007/10/16/pretty-printing-seconds-in-ruby/#comments</comments>
		<pubDate>Tue, 16 Oct 2007 05:28:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	I must have written various versions of this code 20 times so far in my career, but never so quickly and cleanly.  I love ruby.

	
    min, sec = sec / 60, sec % 60
    hour, min = min / 60, min % 60
    day, hour [...]]]></description>
			<content:encoded><![CDATA[	<p>I must have written various versions of this code 20 times so far in my career, but never so quickly and cleanly.  I love ruby.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">    min, sec = sec <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">60</span>, sec <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">60</span>
    hour, min = min <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">60</span>, min <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">60</span>
    day, hour = hour <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">24</span>, hour <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">24</span>
    week, day = day <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">7</span>, day <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">7</span>
&nbsp;
    <span style="color:#006600; font-weight:bold;">&#91;</span>
      week <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> ? <span style="color:#996600;">&quot;#{week} weeks&quot;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>,
      day <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> ? <span style="color:#996600;">&quot;#{day} days&quot;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>,
      hour <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> ? <span style="color:#996600;">&quot;#{hour} hours&quot;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>,
      min <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> ? <span style="color:#996600;">&quot;#{min} minutes&quot;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>,
      sec <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> ? <span style="color:#996600;">&quot;#{sec} seconds&quot;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">compact</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;, &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>
</p>



	<p>The really cool thing is the multiple assignment that ruby lets you do.  I haven&#8217;t used it for something exactly like this before, but I do use it often and it&#8217;s really nice.</p>

	<p>I feel like there should be a better way to do the bottom half of this, but this is pretty damn readable, I think.  You might have to have a little rubyFU to remember what compact does &#8211; which is get rid of the nils.</p>

	<p>Anyway, this was about 15 minutes of work (and I did it test first).</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/10/16/pretty-printing-seconds-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing Markaby &#8211; Language Level Refactorings</title>
		<link>http://onemanswalk.com/work/2007/09/26/customizing-markaby-language-level-refactorings/</link>
		<comments>http://onemanswalk.com/work/2007/09/26/customizing-markaby-language-level-refactorings/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 23:14:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dsls]]></category>
		<category><![CDATA[markaby]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	It&#8217;s very easy to call out to methods in markaby, but it&#8217;d be nice if you could actually customize the dsl as well.

	For example, on many of our pages we have a bottom row that has buttons that look a certain way.  So on every page, we have :

	
  table&#40;:width =&#62; &#34;100%&#34;&#41; do
 [...]]]></description>
			<content:encoded><![CDATA[	<p>It&#8217;s very easy to call out to methods in markaby, but it&#8217;d be nice if you could actually customize the dsl as well.</p>

	<p>For example, on many of our pages we have a bottom row that has buttons that look a certain way.  So on every page, we have :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:width</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100%&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    tr <span style="color:#9966CC; font-weight:bold;">do</span>
      td.<span style="color:#9900CC;">left</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        previous_button
        first_button
      <span style="color:#9966CC; font-weight:bold;">end</span>
      td.<span style="color:#9900CC;">center</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#CC0066; font-weight:bold;">print</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      td.<span style="color:#9900CC;">right</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        next_button
        last_button
      <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>



	<p>The code for the actual buttons changes, and after a few tries to extract the whole thing into a single method, we gave up.  Our efforts had made it harder to read, not easier.  There was always just a little too much variance, and it didn&#8217;t feel right.</p>

	<p>What we really wanted to write was :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  last_row <span style="color:#9966CC; font-weight:bold;">do</span>
    column <span style="color:#9966CC; font-weight:bold;">do</span>
      previous_button
      first_button
    <span style="color:#9966CC; font-weight:bold;">end</span>
    column <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#CC0066; font-weight:bold;">print</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    column <span style="color:#9966CC; font-weight:bold;">do</span>
      next_button
      last_button
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>This lets the buttons that change all stay in the view, and gets rid of the skeleton and positional stuff that doesn&#8217;t change.  Furthermore, it&#8217;s <span class="caps">DRY</span> and puts all that positional logic in one place instead of scattered across 20 views.</p>

	<p>How to do this?</p>

	<p>I wrote a test (in RSpec) that looks something like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">describe ApplicationHelper <span style="color:#9966CC; font-weight:bold;">do</span>
  it <span style="color:#996600;">&quot;should generate a table from a buttons method&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    last_row<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:columns</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      column <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#996600;">&quot;foo&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      column <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#996600;">&quot;bar&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'&lt;table width=&quot;100%&quot;&gt;&lt;tr&gt;'</span> <span style="color:#006600; font-weight:bold;">+</span>
                    <span style="color:#996600;">'&lt;td class=&quot;left&quot;&gt;foo&lt;/td&gt;'</span> <span style="color:#006600; font-weight:bold;">+</span> 
                    <span style="color:#996600;">'&lt;td class=&quot;right&quot;&gt;bar&lt;/td&gt;'</span> <span style="color:#006600; font-weight:bold;">+</span>
                  <span style="color:#996600;">'&lt;/tr&gt;&lt;/table&gt;'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>After a bunch of fiddling and poking around, I finally made the test (and a couple others) pass with this code in my ApplicationHelper :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> last_row<span style="color:#006600; font-weight:bold;">&#40;</span>options, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  markaby <span style="color:#9966CC; font-weight:bold;">do</span>
    table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:width</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100%&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      tr <span style="color:#9966CC; font-weight:bold;">do</span>
        LastRowContext.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:columns</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.
                                  <span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">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>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> LastRowContext
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>markaby, columns<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@markaby</span>, <span style="color:#0066ff; font-weight:bold;">@column_count</span>, <span style="color:#0066ff; font-weight:bold;">@column_index</span> = 
                            markaby, columns, <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> column<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    alignment = <span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0066ff; font-weight:bold;">@column_index</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1</span> : <span style="color:#ff3333; font-weight:bold;">:left</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#0066ff; font-weight:bold;">@column_count</span> : <span style="color:#ff3333; font-weight:bold;">:right</span>
    <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#ff3333; font-weight:bold;">:center</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@markaby</span>.<span style="color:#9900CC;">instance_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      td<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> alignment, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>I&#8217;m sure this could get cleaned up more; this was the work of less than an hour.  In particular, if you did this often, you could extract a common MarkabyContext superclass that had some convenience methods.  The point is, this is really easy to do, and we shouldn&#8217;t be scared to try &#8220;Language level refactorings&#8221; like this.</p>



 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/09/26/customizing-markaby-language-level-refactorings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Daemonizing a Ruby Script in Rails</title>
		<link>http://onemanswalk.com/work/2007/09/20/daemonizing-a-ruby-script-in-rails/</link>
		<comments>http://onemanswalk.com/work/2007/09/20/daemonizing-a-ruby-script-in-rails/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 18:28:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	This took way longer than it should have, so I thought I&#8217;d jot down what I did so it might take less time next time.

	1. Install the daemons gem

	
sudo gem install daemons




	2. Presumably you have a script that looks something like this already

	
#!/usr/bin/env ruby
require File.dirname&#40;__FILE__&#41; + &#34;/. ./config/environment&#34;
&#160;
SchedulerDaemon.new.run




	This is a scheduler script that lives in [...]]]></description>
			<content:encoded><![CDATA[	<p>This took <em>way</em> longer than it should have, so I thought I&#8217;d jot down what I did so it might take less time next time.</p>

	<p>1. Install the <a href="http://daemons.rubyforge.org" title="">daemons</a> gem</p>

	<p>
<div class="wp_syntax"><div class="code"><pre>sudo gem install daemons</pre></div></div>
</p>



	<p>2. Presumably you have a script that looks something like this already</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
<span style="color:#CC0066; font-weight:bold;">require</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;">+</span> <span style="color:#996600;">&quot;/. ./config/environment&quot;</span>
&nbsp;
SchedulerDaemon.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">run</span></pre></div></div>
</p>



	<p>This is a scheduler script that lives in the scripts directory of a rails project.</p>

	<p>3. You&#8217;re going to take this code and wrap it in daemon stuff, like so</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
RAILS_ROOT = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</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;">+</span> <span style="color:#996600;">'/. .'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
gem <span style="color:#996600;">'daemons'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'daemons'</span>
&nbsp;
Daemons.<span style="color:#9900CC;">run_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;scheduler&quot;</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:log_output</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir_mode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:normal</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{RAILS_ROOT}/log&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, <span style="color:#996600;">&quot;config/environment&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  SchedulerDaemon.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">run</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>The really hard part for me was debugging it.  Which meant figuring out how to get logging going.  With this code, you can just tail &#8220;log/scheduler.output&#8221; and see the contents of any puts in the code.  Once I started doing that, everything else was easy.</p>

	<p><span class="caps">RAILS</span>_ROOT has to be set first, because daemons changes the current working directory.  Also, I use the log dir, because it&#8217;s shared by capistrano, so I can deploy, then stop / start my scheduler and not worry about losing my first pid file &#8211; plus that&#8217;s where logs are supposed to go.</p>

	<p>4. To make my life just a little easier, I also added some debugging statements</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">Daemons.<span style="color:#9900CC;">run_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;scheduler&quot;</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:log_output</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir_mode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:normal</span>, 
                 <span style="color:#ff3333; font-weight:bold;">:dir</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{RAILS_ROOT}/log&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, <span style="color:#996600;">&quot;config/environment&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;starting scheduler at #{Time.now} for #{RAILS_ENV}&quot;</span>
    SchedulerDaemon.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">run</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">ensure</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ending scheduler at #{Time.now}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>This was actually pretty easy, and next time it will take 5 minutes to create a daemon.  Nice gem.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/09/20/daemonizing-a-ruby-script-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Fun with Times, Mocks, and Closures</title>
		<link>http://onemanswalk.com/work/2007/05/24/more-fun-with-times-mocks-and-closures/</link>
		<comments>http://onemanswalk.com/work/2007/05/24/more-fun-with-times-mocks-and-closures/#comments</comments>
		<pubDate>Thu, 24 May 2007 05:48:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mocha]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	I solved a complex problem in cruise today with some non-trivial mocking.  Check this one out :


	I needed to test that every x amount of time, we do a clean checkout.  It can be every 6 hours, 2 days, whatever.  How do you test this?

	Well, maybe you could mock the time.

	
Time.stubs&#40;:now&#41;.returns&#40;Time.now + [...]]]></description>
			<content:encoded><![CDATA[	<p>I solved a complex problem in cruise today with some non-trivial mocking.  Check this one out :</p>


	<p>I needed to test that every x amount of time, we do a clean checkout.  It can be every 6 hours, 2 days, whatever.  How do you test this?</p>

	<p>Well, maybe you could mock the time.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:now</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">returns</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">2</span>.<span style="color:#9900CC;">hours</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>
</p>



	<p>That almost works, except that the code works by touching a file.  And touch doesn&#8217;t use <code>Time.now</code>.  Now at this point, we could go crazy, and mock <code>FileUtils.touch</code>, of course that means we&#8217;ll also have to mock <code>File.exists?</code> and then what are we really testing?</p>

	<p>Instead, I used Mocha to temporarily replace <code>FileUtils.touch</code> with my own implementation that acts like the original but uses my own value of time.  It looks like this so far.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  marker = sandbox.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/last_clean_checkout_timestamp'</span>
&nbsp;
  now = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:touch</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">with</span><span style="color:#006600; font-weight:bold;">&#40;</span>marker<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">returns</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>marker, <span style="color:#996600;">'w'</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>f<span style="color:#006600; font-weight:bold;">|</span> f</pre></div></div>
</p>



	<p>you&#8217;ll notice that both of these stubs are returning procs that reference <code>now</code> a local variable&#8230;.</p>

	<p>That&#8217;s ruby magic.</p>

	<p>It means that I can now forget about mocks and write the rest of my test like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span> <span style="color:#ff3333; font-weight:bold;">:every</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>.<span style="color:#9900CC;">hour</span>
&nbsp;
  assert <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span>?
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">59</span>.<span style="color:#9900CC;">minutes</span>
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span>.<span style="color:#9900CC;">minutes</span>
  assert <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span>?
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span> <span style="color:#ff3333; font-weight:bold;">:every</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>.<span style="color:#9900CC;">days</span>
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>.<span style="color:#9900CC;">day</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">23</span>.<span style="color:#9900CC;">hours</span>
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span>.<span style="color:#9900CC;">hours</span>
  assert <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span>?</pre></div></div>
</p>



	<p>Instead of changing the mocks several times in between each test, I can just change my local variable <code>now</code>.  Because of closures, the mocked out methods return the new value.</p>

	<p>Ruby is awesome (and Mocha is pretty sweet too)</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/05/24/more-fun-with-times-mocks-and-closures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object Mother&#8230;in rails?</title>
		<link>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/</link>
		<comments>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/#comments</comments>
		<pubDate>Tue, 06 Mar 2007 16:58:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	That&#8217;s right, the circa 2000 pattern still makes sense today.  Use an &#8220;object mother&#8221; as a test factory to conveniently create objects for your unit tests to bang against.  It will often default values, or have different states in which to create objects.  For example, you might have a new_user, as well [...]]]></description>
			<content:encoded><![CDATA[	<p>That&#8217;s right, the circa 2000 pattern still makes sense today.  Use an &#8220;object mother&#8221; as a test factory to conveniently create objects for your unit tests to bang against.  It will often default values, or have different states in which to create objects.  For example, you might have a new_user, as well as a new_superuser and new_guest method all of which return users.</p>

	<p>Read about the <a href="http://www.martinfowler.com/bliki/ObjectMother.html" title="">original pattern</a></p>

	<p>But why, you ask, not just use rails&#8217; fixtures?  Glad you asked.</p>

	<ol>
		<li>it&#8217;s more intuitive and maintainable to setup the data you need right next to the test</li>
			<li>it&#8217;s easier to create just exactly the objects you need when you have test factory methods</li>
	</ol>

	<p>But don&#8217;t take my word for it.  Let&#8217;s look at some code.</p>

	<p>First off, what does this look like in ruby?  I am creating an &#8220;ObjectMother&#8221; module, which I then just mixin to my tests.  A test might then look like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">include</span> ObjectMother
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> test_delete_project
    project = new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'foo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post <span style="color:#ff3333; font-weight:bold;">:delete</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> project.<span style="color:#9900CC;">id</span>
    assert_raise<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> Tag.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>project.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>The magic is in &#8220;new_project&#8221;.  It&#8217;s an entirely pragmatic construct.  If you pass it a string, it will set everything else to acceptable defaults, and use that string as a name.  It looks something like this.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">module</span> ObjectMother
  <span style="color:#9966CC; font-weight:bold;">def</span> new_project<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
    options = <span style="color:#006600; font-weight:bold;">&#123;</span>:name <span style="color:#006600; font-weight:bold;">=&gt;</span> options<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">String</span>
    options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:url_name</span><span style="color:#006600; font-weight:bold;">&#93;</span> = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\W<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> !options.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url_name</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    Project.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>Here in project, it defaults the url_name (which must be unique) from the name you&#8217;ve given it.  However, you could also create a more custom project by running this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  new_project<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;">'garage'</span>, <span style="color:#ff3333; font-weight:bold;">:url_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'the_garage'</span>, <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>
</p>



	<p>This is how it works for projects, but it&#8217;s only purpose in life is to make my life easier and reduce the amount of code one has to type or read.  So each type of thing it creates works a little bit differently according to our needs<br />
&#8212;&#8212;<br />
h2. A more complicated example</p>

	<p>I was playing with ferret last week, and wrote a test that looked like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> test_across_types
    project = new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'rabbit holes'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post = new_post<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:subject</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'a rabbit has a big head'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    user = new_user<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:display_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'rabbit head'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@search</span>.<span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'rabbit'</span>
    assert_find <span style="color:#006600; font-weight:bold;">&#91;</span>project, post, user<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@search</span>.<span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'rabbit head'</span>
    assert_find <span style="color:#006600; font-weight:bold;">&#91;</span>post, user<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>My thought process was something along the lines of :</p>

	<ol>
		<li>I want to test that my searcher works across types</li>
			<li>I need to create a project, post, and user that all have a term in them (in different places)</li>
			<li>I want to search for the term, and make sure i get all of them</li>
			<li>I want to search for a term that maybe 2 of them have and make sure I only get those 2</li>
	</ol>

	<p>Writing the test for this part literally took 30 seconds, I didn&#8217;t have to go lookup the fixtures or add a new fixture for my new case.  I also didn&#8217;t have to remember all the things that it takes to make a valid project or post or user.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helpful Additions To Test::Unit</title>
		<link>http://onemanswalk.com/work/2007/03/06/helpful-additions-to-test-unit/</link>
		<comments>http://onemanswalk.com/work/2007/03/06/helpful-additions-to-test-unit/#comments</comments>
		<pubDate>Tue, 06 Mar 2007 16:54:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Doing a lot of rails work, I&#8217;m getting a good feel for testing in ruby and rails.  Here are some tricks / snippets I use :

	assert_raises takes a string and/or a class

	I want to be able to write

	
one = Project.new&#40;'one'&#41;
&#160;
projects




	I&#8217;ve done this a few times, but I think cruisecontrol.rb&#8217;s implementation is the most robust [...]]]></description>
			<content:encoded><![CDATA[	<p>Doing a lot of rails work, I&#8217;m getting a good feel for testing in ruby and rails.  Here are some tricks / snippets I use :</p>

	<h2>assert_raises takes a string and/or a class</h2>

	<p>I want to be able to write</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">one = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'one'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
projects</pre></div></div>
</p>



	<p>I&#8217;ve done this a few times, but I think <a href="http://cruisecontrolrb.thoughtworks.com" title="">cruisecontrol.rb&#8217;s</a> implementation is the most robust :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> assert_raises<span style="color:#006600; font-weight:bold;">&#40;</span>arg1 = <span style="color:#0000FF; font-weight:bold;">nil</span>, arg2 = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    expected_error = arg1.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Exception</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? arg1 : <span style="color:#0000FF; font-weight:bold;">nil</span>
    expected_class = arg1.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">Class</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? arg1 : <span style="color:#0000FF; font-weight:bold;">nil</span>
    expected_message = arg1.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? arg1 : arg2
    <span style="color:#9966CC; font-weight:bold;">begin</span> 
      <span style="color:#9966CC; font-weight:bold;">yield</span>
      <span style="color:#CC0066; font-weight:bold;">fail</span> <span style="color:#996600;">&quot;expected error was not raised&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">Test::Unit::AssertionFailedError</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#9966CC; font-weight:bold;">if</span> e.<span style="color:#9900CC;">message</span> == <span style="color:#996600;">&quot;expected error was not raised&quot;</span>
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_error, e<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_error
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_class, e.<span style="color:#9966CC; font-weight:bold;">class</span>, <span style="color:#996600;">&quot;Unexpected error type raised&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_class
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_message, e.<span style="color:#9900CC;">message</span>, <span style="color:#996600;">&quot;Unexpected error message&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_message.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">String</span>
      assert_matched<span style="color:#006600; font-weight:bold;">&#40;</span>expected_message, e.<span style="color:#9900CC;">message</span>, <span style="color:#996600;">&quot;Unexpected error message&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_message.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC00FF; font-weight:bold;">Regexp</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>


	<p>&#8212;&#8212;<br />
h2. assert_equal_sets</p>

	<p>In Java, I used to push things into sets and compare them when I didn&#8217;t care about order.  In ruby, I sometimes use assert_equal_sets.  It does a compare of two arrays independent of order.  So</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  assert_equal_sets <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>   <span style="color:#008000; font-style:italic;"># passes</span>
  assert_equal_sets <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>, <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>    <span style="color:#008000; font-style:italic;"># fails</span></pre></div></div>
</p>



	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> reorder_like!<span style="color:#006600; font-weight:bold;">&#40;</span>other<span style="color:#006600; font-weight:bold;">&#41;</span>
    tmp = dup
    clear
    other.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#0000FF; font-weight:bold;">self</span></pre></div></div>
</p>


	<p>&#8212;&#8212;<br />
h2. file_sandbox for testing against the file system</p>

	<p>After dragging this code around me for the last 6 or 7 projects I&#8217;ve been on, I finally packaged it up as a <a href="http://rubyforge.org/projects/file_sandbox" title="">gem</a> .  It lets you write code like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">in_sandbox <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sandbox<span style="color:#006600; font-weight:bold;">|</span>
  sandbox.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'b/a.txt'</span>, <span style="color:#ff3333; font-weight:bold;">:with_contents</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'some stuff'</span>
&nbsp;
  assert_equal <span style="color:#996600;">'some_stuff'</span>, <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>sandbox.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/b/a.txt'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>Basically it creates a temporary directory for you to muck about in.  After the block is ended (or teardown is called on your test) that directory and everything in it is guaranteed to be cleaned up.  It also has a bunch of methods to make file based things easier like creating a file, etc.</p>

	<p>Install it with &#8220;gem install file_sandbox&#8221; </p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/03/06/helpful-additions-to-test-unit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

