<?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"
	>

<channel>
	<title>Erik on Rails</title>
	<atom:link href="http://erikonrails.snowedin.net/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://erikonrails.snowedin.net</link>
	<description>One developer’s experiences learning Ruby on Rails</description>
	<pubDate>Thu, 18 Apr 2013 00:38:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Script for opening the latest migration</title>
		<link>http://erikonrails.snowedin.net/?p=492</link>
		<comments>http://erikonrails.snowedin.net/?p=492#comments</comments>
		<pubDate>Thu, 18 Apr 2013 00:38:25 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[migrations]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=492</guid>
		<description><![CDATA[Often after generating a Rails migration I want to edit it, but it&#8217;s annoying to have to type in those dang timestamps. So I wrote a little script that just opens the newest one:

#!/bin/sh
s db/migrate/`ls db/migrate &#124; tail -n 1`

Put that in ~/bin/latest-migration, chmod u+x it, and add ~/bin to your PATH and you can [...]]]></description>
			<content:encoded><![CDATA[<p>Often after generating a Rails migration I want to edit it, but it&#8217;s annoying to have to type in those dang timestamps. So I wrote a little script that just opens the newest one:</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">#!/bin/sh
s db/migrate/`ls db/migrate | tail -n 1`</pre></div></div>

<p>Put that in ~/bin/latest-migration, chmod u+x it, and add ~/bin to your PATH and you can just run latest-migration from within any Rails folder. Of course you have to replace &#8217;s&#8217; with the command for invoking your text editor of choice.</p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=492</wfw:commentRss>
		</item>
		<item>
		<title>env: ruby_bundler_wrapper: No such file or directory</title>
		<link>http://erikonrails.snowedin.net/?p=478</link>
		<comments>http://erikonrails.snowedin.net/?p=478#comments</comments>
		<pubDate>Thu, 20 Sep 2012 19:48:12 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[bundler]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[rvm]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=478</guid>
		<description><![CDATA[I spent bunch of time scratching my head, trying to figure out why my ruby executables (rails, heroku, etc) weren&#8217;t working.  I thought it was because I had an old custom shebang in my .gemrc, but that didn&#8217;t seem to change anything.  I ran gem regenerate binstubs and that didn&#8217;t help either. I [...]]]></description>
			<content:encoded><![CDATA[<p>I spent bunch of time scratching my head, trying to figure out why my ruby executables (rails, heroku, etc) weren&#8217;t working.  I thought it was because I had an old custom shebang in my .gemrc, but that didn&#8217;t seem to change anything.  I ran <code>gem regenerate binstubs</code> and that didn&#8217;t help either. I still to this day don&#8217;t even know what package provides ruby_bundler_wrapper, or even where it lives.</p>
<p>BUT, I figured out the problem for me was that I had my gems stored in a global rvm gemset, so I had to regenerate the binstubs in there:</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">rvm @global do rvm regenerate binstubs</pre></div></div>

<p>And that seemed to do the trick. Long morning of system administration today.</p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=478</wfw:commentRss>
		</item>
		<item>
		<title>&#8220;libxml2 is missing&#8221; error when installing Nokogiri gem on Mac OS X Lion</title>
		<link>http://erikonrails.snowedin.net/?p=476</link>
		<comments>http://erikonrails.snowedin.net/?p=476#comments</comments>
		<pubDate>Tue, 04 Sep 2012 21:04:14 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=476</guid>
		<description><![CDATA[I&#8217;m not 100% sure what did the trick for me, but the last thing I did was symlink my gcc binary to gcc-4.2, which for some reason is hardcoded in Nokogiri:

sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

As suggested here. The Nokogiri gem is totally unhelpful with the error messages.  It just tells you that libxml2 can&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not 100% sure what did the trick for me, but the last thing I did was symlink my gcc binary to gcc-4.2, which for some reason is hardcoded in Nokogiri:</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2</pre></div></div>

<p>As suggested <a href="http://stackoverflow.com/a/8454423">here</a>. The Nokogiri gem is totally unhelpful with the error messages.  It just tells you that libxml2 can&#8217;t be found, no matter what the build problem is. If you are having trouble getting Nokogiri to install, your friend is the mkmf.log. On an RVM sytem, that&#8217;ll be in ~/.rvm/gems/[your gemset]/gems/nokogiri-1.5.5/ext/nokogiri/mkmf.log.  Without RVM it&#8217;ll be wherever gem sticks unbuilt gems by default.</p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=476</wfw:commentRss>
		</item>
		<item>
		<title>Quoting quotes in Ruby</title>
		<link>http://erikonrails.snowedin.net/?p=465</link>
		<comments>http://erikonrails.snowedin.net/?p=465#comments</comments>
		<pubDate>Thu, 21 Jun 2012 19:23:06 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=465</guid>
		<description><![CDATA[Cucumber makes heavy use of quotes to wrap data:

When I purchase &#34;Oryx and Crake&#34;
Then I should receive an email with the subject &#34;Thank you for your purchase!&#34;

I sometimes need to to programmatically generate these strings, when invoking steps-within-steps:

When /^I purchase &#34;([^&#34;&#34;]*)&#34;$/ do &#124;title&#124;
  step &#34;Search for \&#34;#{title}\&#34;&#34;
  ...
end

Thankfully, Ruby has a metric shit [...]]]></description>
			<content:encoded><![CDATA[<p>Cucumber makes heavy use of quotes to wrap data:</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">When I purchase &quot;Oryx and Crake&quot;
Then I should receive an email with the subject &quot;Thank you for your purchase!&quot;</pre></div></div>

<p>I sometimes need to to programmatically generate these strings, when invoking steps-within-steps:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">When</span> <span style="color:#006600; font-weight:bold;">/</span>^I purchase <span style="color:#996600;">&quot;([^&quot;</span><span style="color:#996600;">&quot;]*)&quot;</span>$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> |title|
  step <span style="color:#996600;">&quot;Search for <span style="color:#000099;">\&quot;</span>#{title}<span style="color:#000099;">\&quot;</span>&quot;</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Thankfully, Ruby has a <a href="http://pivotallabs.com/users/nick/blog/articles/409-stupid-ruby-quoting-tricks">metric shit ton</a> of ways to quote strings. But what I haven&#8217;t seen mention of is the fact that you can just use the % operator with an arbitrary character to get a fully <a href="http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation">interpolated</a> string:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># These are all equivalent:</span>
step <span style="color:#006600; font-weight:bold;">%</span>~Search <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#996600;">&quot;#{title}&quot;</span>~
step <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#40;</span>Search <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#996600;">&quot;#{title}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
step <span style="color:#006600; font-weight:bold;">%&lt;</span>Search <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#996600;">&quot;#{title}&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>
<span style="color:#008000; font-style:italic;"># I doesn't matter which symbol you use.  Just use whatever </span>
<span style="color:#008000; font-style:italic;"># symbol is not present in your string.</span></pre></div></div>

<p>All the references I&#8217;ve seen tell you to use <code>%Q[whatever]</code> for interpolated strings. The Q seems unnecessary.</p>
<p>But if you want an uninterpolated string you do still need to do %q?whatever?.</p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=465</wfw:commentRss>
		</item>
		<item>
		<title>Stupid Ruby Tricks 3: Injecting an operator</title>
		<link>http://erikonrails.snowedin.net/?p=457</link>
		<comments>http://erikonrails.snowedin.net/?p=457#comments</comments>
		<pubDate>Fri, 01 Jun 2012 21:46:31 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=457</guid>
		<description><![CDATA[Just found out that you can inject an operator into a collection. For example:

&#91;&#91;1,2&#93;,&#91;3,4&#93;&#93;.inject&#40;&#91;&#93;, :+&#41;
#=&#62; [1, 2, 3, 4]

You don&#8217;t even need the starting value. Rad.
Edit: If you leave out the starting value, injecting an empty array will return nil. Include an empty array as the first parameter if you want an empty array back [...]]]></description>
			<content:encoded><![CDATA[<p>Just found out that you can inject an operator into a collection. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</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:#006666;">3</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</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:#008000; font-style:italic;">#=&gt; [1, 2, 3, 4]</span></pre></div></div>

<p>You don&#8217;t even need the starting value. Rad.</p>
<p><b>Edit:</b> If you leave out the starting value, injecting an empty array will return nil. Include an empty array as the first parameter if you want an empty array back in that case.</p>
<p>Previous trick: <a href="http://erikonrails.snowedin.net/?p=402">Using find and find_all on ActiveRecord collections</a></p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=457</wfw:commentRss>
		</item>
		<item>
		<title>Custom rspec matcher for Delayed::Job performable methods</title>
		<link>http://erikonrails.snowedin.net/?p=440</link>
		<comments>http://erikonrails.snowedin.net/?p=440#comments</comments>
		<pubDate>Tue, 22 May 2012 19:50:59 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[delayed-job]]></category>

		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=440</guid>
		<description><![CDATA[I&#8217;ve been working on code that delays a bunch of work using Delayed Job&#8217;s :delay method. Stuff like this:

class Album
  after_create :send_emails
&#160;
  def send_emails
    recipients.each do &#124;recipient&#124;
      recipient.delay.send_email
    end
  end
end

I tried a number of testing strategies: 

checking that the Delayed::Job.count changed
testing [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on code that delays a bunch of work using Delayed Job&#8217;s :delay method. Stuff like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Album
  after_create <span style="color:#ff3333; font-weight:bold;">:send_emails</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> send_emails
    recipients.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |recipient|
      recipient.<span style="color:#9900CC;">delay</span>.<span style="color:#9900CC;">send_email</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>I tried a number of testing strategies: </p>
<ul>
<li>checking that the Delayed::Job.count changed</li>
<li>testing for properties on Delayed::Job.last</li>
<li>working off the jobs and testing that the work got done.</li>
</ul>
<p>But they all seemed too indirect.  In the end I decided to just write a custom matcher that lets me do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">expect <span style="color:#006600; font-weight:bold;">&#123;</span> 
  Album.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:recipients</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>janelle_monáe<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">to</span> delay_method<span style="color:#006600; font-weight:bold;">&#40;</span>janelle_monáe, <span style="color:#ff3333; font-weight:bold;">:send_email</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Another reasonable route would be to mock :delay on the appropriate object, and then do a :should_receive on the mock.  That only works if the object that&#8217;s getting the method delayed exists in your test scope. It won&#8217;t work if the object is created or looked up inside the method you&#8217;re testing&#8230;. unless you stub out the relevant :create and :find methods.</p>
<p>I don&#8217;t know.  I&#8217;m still (3+ years in) learning how to decide how intensively to mock my unit tests.  Some part of me thinks I&#8217;m just not using mocks enough.  But the part that likes unit tests to be a little more flexible in the face of refactoring resists.  We&#8217;ll see how it goes.</p>
<p>Use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># On a specific object:</span>
apple = Apple.<span style="color:#9900CC;">new</span>
expect <span style="color:#006600; font-weight:bold;">&#123;</span>...<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">to</span> delay_method<span style="color:#006600; font-weight:bold;">&#40;</span>apple, <span style="color:#ff3333; font-weight:bold;">:core</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># On any instance of a class:</span>
expect <span style="color:#006600; font-weight:bold;">&#123;</span>...<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">to</span> delay_method<span style="color:#006600; font-weight:bold;">&#40;</span>Apple.<span style="color:#9900CC;">any_instance</span>, <span style="color:#ff3333; font-weight:bold;">:core</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># On whatever object the code returns:</span>
expect <span style="color:#006600; font-weight:bold;">&#123;</span> Apple.<span style="color:#9900CC;">create</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">to</span> delay_method<span style="color:#006600; font-weight:bold;">&#40;</span>returned_object, <span style="color:#ff3333; font-weight:bold;">:core</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>And here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># spec/support/delayed_job.rb</span>
<span style="color:#9966CC; font-weight:bold;">module</span> RspecDelayedJobMatcher
  <span style="color:#9966CC; font-weight:bold;">class</span> ReturnedObject
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> returned_object
  <span style="color:#6666ff; font-weight:bold;">RspecDelayedJobMatcher::ReturnedObject</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">RSpec::Matchers</span>.<span style="color:#9900CC;">define</span> <span style="color:#ff3333; font-weight:bold;">:delay_method</span> <span style="color:#9966CC; font-weight:bold;">do</span> |object, method_name|
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> jobs
    <span style="color:#6666ff; font-weight:bold;">Delayed::Job</span>.<span style="color:#9900CC;">all</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#9966CC; font-weight:bold;">do</span> |job|
      <span style="color:#006600; font-weight:bold;">&#40;</span>payload = job.<span style="color:#9900CC;">payload_object</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> equivalent_object?<span style="color:#006600; font-weight:bold;">&#40;</span>payload.<span style="color:#9900CC;">object</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> payload.<span style="color:#9900CC;">method_name</span> == <span style="color:#0066ff; font-weight:bold;">@method_name</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">count</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> equivalent_object?<span style="color:#006600; font-weight:bold;">&#40;</span>payload<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@object</span> == returned_object
      payload == <span style="color:#0066ff; font-weight:bold;">@response</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#0066ff; font-weight:bold;">@object</span>.<span style="color:#9900CC;">is_a</span>? <span style="color:#6666ff; font-weight:bold;">RSpec::Mocks::AnyInstance::Recorder</span>
      payload.<span style="color:#9900CC;">is_a</span>? <span style="color:#0066ff; font-weight:bold;">@object</span>.<span style="color:#9900CC;">instance_variable_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@klass&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      payload == <span style="color:#0066ff; font-weight:bold;">@object</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> description
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@object</span> == returned_object
      <span style="color:#996600;">&quot;the returned object (#{@response.inspect})&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#0066ff; font-weight:bold;">@object</span>.<span style="color:#9900CC;">is_a</span>? <span style="color:#6666ff; font-weight:bold;">RSpec::Mocks::AnyInstance::Recorder</span>
      <span style="color:#996600;">&quot;some instance of #{@object.instance_variable_get(&quot;</span><span style="color:#0066ff; font-weight:bold;">@klass</span><span style="color:#996600;">&quot;).to_s}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0066ff; font-weight:bold;">@object</span>.<span style="color:#9900CC;">inspect</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  match <span style="color:#9966CC; font-weight:bold;">do</span> |proc|
    <span style="color:#0066ff; font-weight:bold;">@method_name</span> = method_name
    <span style="color:#0066ff; font-weight:bold;">@object</span> = object
    count_before = jobs
    <span style="color:#0066ff; font-weight:bold;">@response</span> = <span style="color:#CC0066; font-weight:bold;">proc</span>.<span style="color:#9900CC;">call</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>@count = jobs <span style="color:#006600; font-weight:bold;">-</span> count_before<span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  failure_message_for_should <span style="color:#9966CC; font-weight:bold;">do</span> |actual|
    <span style="color:#996600;">&quot;Expected block to create a job to invoke #{@method_name} on #{description}, but it created #{@count}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  failure_message_for_should_not <span style="color:#9966CC; font-weight:bold;">do</span> |actual|
    <span style="color:#996600;">&quot;Block created #{@count} job(s) to invoke #{@method_name} on #{description}, but expected none&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  description <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#996600;">&quot;expect block to delay #{@method_name} on a #{@object.class.to_s}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=440</wfw:commentRss>
		</item>
		<item>
		<title>Invoking methods on optional objects</title>
		<link>http://erikonrails.snowedin.net/?p=434</link>
		<comments>http://erikonrails.snowedin.net/?p=434#comments</comments>
		<pubDate>Fri, 18 May 2012 20:20:01 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=434</guid>
		<description><![CDATA[Lately I&#8217;ve been using pretty heavily an extension I wrote to Ruby&#8217;s Object class:

# lib/core_extensions/object.rb
class Object
  def defined_and&#40;key=nil&#41;
    return nil if nil?
    block_given? ? yield&#40;self&#41; : self&#91;key&#93;
  end
end

This lets me invoke methods on objects that may or may not exist.  It&#8217;s really useful for the frequent [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been using pretty heavily an extension I wrote to Ruby&#8217;s Object class:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># lib/core_extensions/object.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> defined_and<span style="color:#006600; font-weight:bold;">&#40;</span>key=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">nil</span>?
    block_given? ? <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span> : <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This lets me invoke methods on objects that may or may not exist.  It&#8217;s really useful for the frequent situations where an optional child object has a method that also applies to it&#8217;s parent:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ZipCode
  <span style="color:#9966CC; font-weight:bold;">def</span> start_for<span style="color:#006600; font-weight:bold;">&#40;</span>vegetable<span style="color:#006600; font-weight:bold;">&#41;</span>
    planting_specs.<span style="color:#9966CC; font-weight:bold;">for</span><span style="color:#006600; font-weight:bold;">&#40;</span>vegetable<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">defined_and</span> <span style="color:#006600; font-weight:bold;">&amp;</span>:start
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As opposed to the ugly alternative:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> start_for<span style="color:#006600; font-weight:bold;">&#40;</span>vegetable<span style="color:#006600; font-weight:bold;">&#41;</span>
  spec = planting_specs.<span style="color:#9966CC; font-weight:bold;">for</span><span style="color:#006600; font-weight:bold;">&#40;</span>vegetable<span style="color:#006600; font-weight:bold;">&#41;</span>
  spec.<span style="color:#9900CC;">start</span> <span style="color:#9966CC; font-weight:bold;">if</span> spec
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That will either return a start, or it&#8217;ll return nil if no planting_spec was found. It works with a block (&#038;:start is a shortcut for {|x| x.start}) or an enumerable key:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:a</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">defined_and</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 1</span>
<span style="color:#0000FF; font-weight:bold;">nil</span>.<span style="color:#9900CC;">defined_and</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; nil</span></pre></div></div>

<p>To use stuff in the lib/core_extensions folder in your Rails app, as defined above, you need an initializer:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># config/initializers/core_extensions.rb</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span>Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;lib/core_extensions/*&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|f| <span style="color:#CC0066; font-weight:bold;">require</span> f<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=434</wfw:commentRss>
		</item>
		<item>
		<title>Explain ActiveRecord queries in Rails console</title>
		<link>http://erikonrails.snowedin.net/?p=432</link>
		<comments>http://erikonrails.snowedin.net/?p=432#comments</comments>
		<pubDate>Wed, 16 May 2012 02:28:32 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[activerecord]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=432</guid>
		<description><![CDATA[I keep missing this in Google somehow, but if you want ActiveRecord to explain your queries in the console, do:

ActiveRecord::Base.logger = Logger.new&#40;STDOUT&#41;

via shime on StackOverflow
]]></description>
			<content:encoded><![CDATA[<p>I keep missing this in Google somehow, but if you want ActiveRecord to explain your queries in the console, do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">logger</span> = <span style="color:#CC00FF; font-weight:bold;">Logger</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>STDOUT<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>via <a href="http://stackoverflow.com/questions/9173448/retrieving-sql-queries-from-active-record-queries-in-rails-3">shime on StackOverflow</a></p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=432</wfw:commentRss>
		</item>
		<item>
		<title>Destructive (low memory) Ruby each</title>
		<link>http://erikonrails.snowedin.net/?p=429</link>
		<comments>http://erikonrails.snowedin.net/?p=429#comments</comments>
		<pubDate>Fri, 11 May 2012 17:08:11 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=429</guid>
		<description><![CDATA[As mentioned, if you have a loop that allocates a bunch of memory tied to an object, destroying the current object after each iteration can help save on memory.  I found myself reusing this pattern, so I just extended Array:

class Array
  def destructive_each
    while item = shift
    [...]]]></description>
			<content:encoded><![CDATA[<p>As <a href="http://erikonrails.snowedin.net/?p=410">mentioned</a>, if you have a loop that allocates a bunch of memory tied to an object, destroying the current object after each iteration can help save on memory.  I found myself reusing this pattern, so I just extended Array:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><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> destructive_each
    <span style="color:#9966CC; font-weight:bold;">while</span> item = shift
      <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>item<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>It&#8217;s called &#8220;destructive&#8221; because after using it your array will be empty.</p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=429</wfw:commentRss>
		</item>
		<item>
		<title>Simplify your conditional step definitions with should_unless</title>
		<link>http://erikonrails.snowedin.net/?p=420</link>
		<comments>http://erikonrails.snowedin.net/?p=420#comments</comments>
		<pubDate>Sat, 24 Mar 2012 01:02:44 +0000</pubDate>
		<dc:creator>erik</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[cucumber]]></category>

		<guid isPermaLink="false">http://erikonrails.snowedin.net/?p=420</guid>
		<description><![CDATA[In cucumber, you often want to be able to use a step in both it&#8217;s positive and negative form.  For example, in different universes I might want to ensure that &#8220;pigs can fly&#8221;, or that &#8220;pigs can not fly&#8221;. Typically I&#8217;ve either written two step matchers for this, or added an awkward conditional:

Then /^pigs [...]]]></description>
			<content:encoded><![CDATA[<p>In cucumber, you often want to be able to use a step in both it&#8217;s positive and negative form.  For example, in different universes I might want to ensure that &#8220;pigs can fly&#8221;, or that &#8220;pigs can not fly&#8221;. Typically I&#8217;ve either written two step matchers for this, or added an awkward conditional:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">Then</span> <span style="color:#006600; font-weight:bold;">/</span>^pigs can <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">not</span> |<span style="color:#006600; font-weight:bold;">&#41;</span>fly$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> |negated|
  <span style="color:#9966CC; font-weight:bold;">if</span> negated.<span style="color:#9900CC;">blank</span>?
    Pig.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">should</span> have_the_power_of_flight
  <span style="color:#9966CC; font-weight:bold;">else</span>
    Pig.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">should_not</span> have_the_power_of_flight
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>But today I decided to factor that out into a little helper method in my cucumber config:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># features/support/env.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> should_unless<span style="color:#006600; font-weight:bold;">&#40;</span>missing, matcher<span style="color:#006600; font-weight:bold;">&#41;</span>
    missing.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#9966CC; font-weight:bold;">not</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? should_not<span style="color:#006600; font-weight:bold;">&#40;</span>matcher<span style="color:#006600; font-weight:bold;">&#41;</span> : should<span style="color:#006600; font-weight:bold;">&#40;</span>matcher<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></pre></div></div>

<p>This allows me to write my matcher much more simply:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">Then</span> <span style="color:#006600; font-weight:bold;">/</span>^pigs can <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">not</span> |<span style="color:#006600; font-weight:bold;">&#41;</span>fly$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> |negated|
  Pig.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">should_unless</span> negated, have_the_power_of_flight
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So fresh and so <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://erikonrails.snowedin.net/?feed=rss2&amp;p=420</wfw:commentRss>
		</item>
	</channel>
</rss>
