<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">
	<title>technical.pickles - josh on technical stuff</title>

	
	<link href="http://technicalpickles.com/posts.html" type="text/html" />

	<updated>Wed May 06 00:00:00 -0400 2009</updated>
	<author>
		<name>Josh Nichols</name>
		<email>josh@technicalpickles.com</email>
	</author>

	<id>tag:technicalpickles.com,2005:/posts</id>

	
	<link rel="self" href="http://feeds.feedburner.com/TechnicalPickles" type="application/atom+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry>
		<title>Jeweler turns 1.0.0</title>
		<link href="http://technicalpickles.com/posts/jeweler-turns-1.0.0" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/jeweler-turns-1.0.0</id>
		<updated>Wed May 06 00:00:00 -0400 2009</updated>
		<content type="html">&lt;p&gt;There&amp;#8217;s always a certain amount of stigma, fear, and uncertainty around the doing the big 1.0 release. I finally got over that for &lt;a href="http://technicalpickles.github.com/jeweler/"&gt;Jeweler&lt;/a&gt;. So, be hold! Jeweler 1.0.0.&lt;/p&gt;
&lt;p&gt;For the uninitiated, Jeweler is a tool for creating and managing RubyGems projects. It lets you quickly go from 0 to released gem. It also simplifies managing your gem after you create it.&lt;/p&gt;
&lt;p&gt;There are two main components to jeweler: the generator for creating an initial project, and the rake tasks for managing versioning and releases.&lt;/p&gt;
&lt;h3&gt;Install it&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;
sudo gem install jeweler
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;The generator&lt;/h3&gt;
&lt;p&gt;The simplest incantation of the jeweler is this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
jeweler your-awesome-gem
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates a local git repository with a basic skeleton for your project. It includes things like a Rakefile, a template to put your gem&amp;#8217;s code in, a test helper, a failing test case, and a &lt;span class="caps"&gt;LICENSE&lt;/span&gt;. It also setup to be pushed to &lt;a href="http://github.com"&gt;GitHub&lt;/a&gt; and includes a reasonable gitignore file.&lt;/p&gt;
&lt;p&gt;By default, you&amp;#8217;ll get Shoulda flavored tests, but you can opt to go with test/unit, minitest, rspec, bacon, or micronaut. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
jeweler &amp;#8212;micronaut your-awesome-gem
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you want to use cucumber, you can have that too. It&amp;#8217;ll be setup for whichever testing framework you choose:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
jeweler &amp;#8212;micronaut &amp;#8212;cucumber your-awesome-gem
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also choose to have a repository created for you on GitHub, with RubyGem creation enabled:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
jeweler &amp;#8212;create-repo your-awesome-gem
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While originally intended specifically to work with GitHub&amp;#8217;s gem support, it eventually was realized that it&amp;#8217;d be nice to release to &lt;a href="http://rubyforge.org"&gt;RubyForge&lt;/a&gt; too. You can add support to your project:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
jeweler &amp;#8212;rubyforge your-awesome-gem
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you look at your Rakefile, you&amp;#8217;ll notice that a number of tasks were generated for you. Jeweler itself provides a number of tasks, as mentioned, but it also tasks for rdoc, test or spec, and rcov. However, your Rakefile doesn&amp;#8217;t depend specifically on jeweler. This means that someone randomly cloning your project, and messing around with patching/testing it, then they don&amp;#8217;t need to have jeweler installed.&lt;/p&gt;
&lt;h3&gt;The tasks&lt;/h3&gt;
&lt;p&gt;When you are ready to do a release, you first create the version. It is stored in &lt;span class="caps"&gt;VERSION&lt;/span&gt; (formerly &lt;span class="caps"&gt;VERSION&lt;/span&gt;.yml, but both work). This only updates what jeweler thinks the version is, and doesn&amp;#8217;t actually release anything. It defaults to 0.0.0, but you can choose to specify your own:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
rake version:write
rake version:write &lt;span class="caps"&gt;MAJOR&lt;/span&gt;=1 &lt;span class="caps"&gt;MINOR&lt;/span&gt;=5 &lt;span class="caps"&gt;PATCH&lt;/span&gt;=3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You&amp;#8217;d typically release after this initial version created. Releasing involves generating a gemspec based on your version, and pushing it to GitHub, and tagging the release:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
rake release
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After your initial release, you can bump the version easily. You can choose to do the major, minor, or patch version. For 1.5.3, 1 is the major, 5 is the minor, and 3 is the patch:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
rake version:bump:patch
rake version:bump:minor
rake version:bump:major
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Version bumping only updates what version jeweler thinks you are using. You&amp;#8217;ll need to do a release to actually release:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
rake release
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you&amp;#8217;ve configured your project for releasing to RubyForge, and you&amp;#8217;ve registered your project, you can prepare RubyForge for it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
rake rubyforge:setup
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you can release the docs and gem:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
rake rubyforge:release
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;More resources&lt;/h3&gt;
&lt;p&gt;This has been a pretty high level overview and scant on details, so you might want to check out:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;The &lt;a href="http://github.com/technicalpickles/jeweler/blob/master/README.markdown"&gt;&lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;The &lt;a href="http://wiki.github.com/technicalpickles/jeweler"&gt;Wiki&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;The &lt;a href="http://rdoc.info/projects/technicalpickles/jeweler"&gt;RDoc&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;The &lt;a href="http://github.com/technicalpickles/jeweler/issues"&gt;Issues&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;The &lt;a href="http://github.com/technicalpickles/jeweler"&gt;Source&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=n_Dx4gdgXd4:icfWOsUp6Zs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=n_Dx4gdgXd4:icfWOsUp6Zs:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=n_Dx4gdgXd4:icfWOsUp6Zs:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=n_Dx4gdgXd4:icfWOsUp6Zs:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=n_Dx4gdgXd4:icfWOsUp6Zs:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=n_Dx4gdgXd4:icfWOsUp6Zs:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=n_Dx4gdgXd4:icfWOsUp6Zs:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/n_Dx4gdgXd4" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Tumbling away</title>
		<link href="http://technicalpickles.com/posts/tumbling-away" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/tumbling-away</id>
		<updated>Fri Apr 24 00:00:00 -0400 2009</updated>
		<content type="html">&lt;p&gt;Blog posts are hard. Tweets are easy. Tumblr is somewhere in between.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve began a new tumblr, &lt;a href="http://technicalpicklejar.tumblr.com/"&gt;Technical Pickle Jar&lt;/a&gt;. The general intent is that it captures my current softwarey thoughts, that haven&amp;#8217;t quite matured to full blog post status.&lt;/p&gt;
&lt;p&gt;So, if you&amp;#8217;re dying to know what&amp;#8217;s on my mind, &lt;a href="http://technicalpicklejar.tumblr.com/"&gt;check it out&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=YF9KWyMoenY:C9lmlcF2088:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=YF9KWyMoenY:C9lmlcF2088:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=YF9KWyMoenY:C9lmlcF2088:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=YF9KWyMoenY:C9lmlcF2088:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=YF9KWyMoenY:C9lmlcF2088:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=YF9KWyMoenY:C9lmlcF2088:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=YF9KWyMoenY:C9lmlcF2088:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/YF9KWyMoenY" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Ruby stubbing and mocking with rr</title>
		<link href="http://technicalpickles.com/posts/ruby-stubbing-and-mocking-with-rr" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/ruby-stubbing-and-mocking-with-rr</id>
		<updated>Tue Apr 14 00:00:00 -0400 2009</updated>
		<content type="html">&lt;p&gt;&lt;a href="http://github.com/btakita/rr/tree/master"&gt;Double Ruby, or rr for short,&lt;/a&gt; is a &amp;#8220;test double framework&amp;#8221; for Ruby. According to the &lt;a href="http://github.com/btakita/rr/blob/master/README.rdoc"&gt;rr &lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt;, &amp;#8220;A Test Double is a generalization of something that replaces a real object to make it easier to test another object. It&amp;#8217;s like a stunt double for tests. The following are test doubles: Mocks, Stubs, Fakes, Spies, Proxies&amp;#8221;. It is in the same niche as frameworks like &lt;a href="http://mocha.rubyforge.org/"&gt;mocha&lt;/a&gt;, &lt;a href="http://flexmock.rubyforge.org/"&gt;flexmock&lt;/a&gt;, and the &lt;a href="http://rspec.info/documentation/mocks/"&gt;mocking support built into RSpec&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So why bother with another framework when the others have been well established and work well? I have two main reasons:&lt;/p&gt;
* Terser syntax that&amp;#8217;s more rubyish (you call the methods you want to mock, with the parameters you want)
* More advanced functionality (test spies are my particular favorite)
&lt;p&gt;I&amp;#8217;m going go over the functionality I&amp;#8217;ve been using, with a few asides when it&amp;#8217;s much different than mocha and RSpec.&lt;/p&gt;
&lt;h3&gt;Installation&lt;/h3&gt;
&lt;p&gt;The rr gem is, well, rr:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
sudo gem install rr
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you must update your helper to use rr. In test/unit, you&amp;#8217;d update &lt;code&gt;test/test_helper.rb&lt;/code&gt; to include:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rr&amp;#39;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TestCase&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;RR&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Adapters&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TestUnit&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;For RSpec, you&amp;#8217;d want to update &lt;code&gt;spec/spec_helper.rb&lt;/code&gt; to include:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rr&amp;#39;&lt;/span&gt;

&lt;span class="no"&gt;Spec&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Runner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mock_with&lt;/span&gt; &lt;span class="no"&gt;RR&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Adapters&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rspec&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h4&gt;Gotchas&lt;/h4&gt;
&lt;p&gt;If after installing RR, you get errors like &amp;#8220;stack level too deep (SystemStackError)&amp;#8221; coming from inside RR, there&amp;#8217;s a good chance your helper is being loaded multiple times from different paths.&lt;/p&gt;
&lt;p&gt;For example, &lt;code&gt;test/test_foo.rb&lt;/code&gt; does &lt;code&gt;require File.join(File.dirname(__FILE__), 'test_helper')&lt;/code&gt;, and &lt;code&gt;test/foo/test_bar.rb&lt;/code&gt; does &lt;code&gt;require File.join(File.dirname(__FILE__), '..', 'test_helper')&lt;/code&gt;. This means that &lt;code&gt;test/test_helper&lt;/code&gt; and &lt;code&gt;test/bar/../test_helper&lt;/code&gt; are both being required. They have have different paths even though they refer to same file because the ruby interpreter isn&amp;#8217;t smart enough to normalize it.&lt;/p&gt;
&lt;p&gt;Doing a proper fix for this multiple-require problem in your project is beyond the scope of this article, but here&amp;#8217;s a quick fix:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TestCase&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;RR&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Adapters&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TestUnit&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="kp"&gt;include&lt;/span&gt;&lt;span class="p"&gt;?(&lt;/span&gt;&lt;span class="no"&gt;RR&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Adapters&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TestUnit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h3&gt;Stubbing&lt;/h3&gt;
&lt;p&gt;Stubbing lets you replace the implementation of an object, no strings attached.&lt;/p&gt;
&lt;p&gt;This will stub away any all calls to &lt;code&gt;trogdor.burninate&lt;/code&gt;, regardless of the values and number of arguments.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;You&amp;#8217;ll often want to make a stub return a specific value. You can specify this with a block or &lt;code&gt;returns&lt;/code&gt;. I&amp;#8217;ve generally found the former to be more readable.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninated?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peasants&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninated?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;If you need to stub a method to raise an exception, you can use the same mechanic for returning a value. Just use a block to define a return value, &lt;code&gt;raise&lt;/code&gt; something instead of returning.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;come_in_the_day&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;the Trogdor comes in the NIIIIIIIIIIIIGHT!!!!!&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;As I mentioned, all these examples will match any invocation of stubbed method. You can be pickier though, and give the arguments you expected it to be called with. You can just pass the values you want to the stub. You can also fallback to using &lt;code&gt;with&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peasants&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h3&gt;Mocking&lt;/h3&gt;
&lt;p&gt;Mocking is similar to stubbing, except it adds an expectation. If you mock something, and it isn&amp;#8217;t called, then your test will fail.&lt;/p&gt;
&lt;p&gt;You can mostly take the examples above, and replace &lt;code&gt;stub&lt;/code&gt; with &lt;code&gt;mock&lt;/code&gt;, and away you go.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;
&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninated?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;There is one difference that I&amp;#8217;ve seen though. If you mock a method without any arguments&amp;#8230; it will only match being called without any arguments. This is different than how mocha/rspec work.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_a_female?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;inbox&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_a_female?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# ERROR! expected no args, but got one arg!&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;One way around this is to use rr&amp;#8217;s wildcard matchers. I&amp;#8217;m not going to detail all the possibilities, but I&amp;#8217;ll just say we can use &lt;code&gt;anything&lt;/code&gt; to match any argument passed to it. Check the &lt;a href="http://github.com/btakita/rr/blob/master/README.rdoc"&gt;&lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt; for other matchers.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_a_female?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;inbox&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from_a_female?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# No error, but still not from a female&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h3&gt;Don&amp;#8217;t allow method calls&lt;/h3&gt;
&lt;p&gt;rr lets you indicate a method should never be called, and will raise an error if it does. You can use the same style above for specifying argument.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;dont_allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strong_sad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;improve_on_methods_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strong_bad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h3&gt;Mock and stub objects&lt;/h3&gt;
&lt;p&gt;Sometimes you might want to make a pure mock object, without using a specific implementation. rr doesn&amp;#8217;t support this as well as mocha or rspec. You can create bare @Object@s, and stub or mock as necessary.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;cottage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cottage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninated?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;(There is a &lt;code&gt;mock!&lt;/code&gt; helper for making pure mock objects, but I haven&amp;#8217;t had consistently good experiences with it.)&lt;/p&gt;
&lt;h3&gt;Instances of&lt;/h3&gt;
&lt;p&gt;Want all your peasants to be burninated in one fell swoop?&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instance_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Peasant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninated?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;You can &lt;code&gt;mock&lt;/code&gt; it as well, and specify arguments.&lt;/p&gt;
&lt;h3&gt;Test spies&lt;/h3&gt;
&lt;p&gt;Test spies let you stub a method, and then latter assert the values it was called with.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peasant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;come_in_the_night&lt;/span&gt;

&lt;span class="n"&gt;assert_received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;# test/unit&lt;/span&gt;
&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;have_received&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# rspec&lt;/span&gt;
&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;have_received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:burninate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# rspec, same thing&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h4&gt;A more detailed example&lt;/h4&gt;
&lt;p&gt;I have found this extremely useful with rspec and shoulda, where you might stub in a before/setup block, and then have tests that add expectations on those being called. Consider this example using shoulda and mocha:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;coming in the night&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;setup&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stubs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:burninate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stubs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:burninate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peasants&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    
    &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;come_in_the_night&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;before_should&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;burninate the countryside&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:burninate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;before_should&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;burninate the peasants&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:burninate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;&lt;code&gt;before_should&lt;/code&gt; is used to set an expectation before the setup block actually gets called. This is kind of annoying because you have specify the return values each time. We&amp;#8217;re able to express this with RR much more concisely, and without having to rely on &lt;code&gt;before_should&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;coming in the night&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;setup&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;come_in_the_night&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;burninate the countryside&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;assert_received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;countryside&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;burninate the peasants&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;assert_received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;burninate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peasants&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h3&gt;Rails&lt;/h3&gt;
&lt;p&gt;There&amp;#8217;s nothing special about using rr for Rails. If you&amp;#8217;re using rspec, though, you may want to checkout &lt;a href="http://github.com/josephwilk/rspec-rr/tree/master"&gt;rspec-rr&lt;/a&gt; which provides an rr-ified &lt;code&gt;mock_model&lt;/code&gt; and &lt;code&gt;stub_model&lt;/code&gt;. It does not seem to support &lt;code&gt;:null_object =&amp;gt; true&lt;/code&gt; though.&lt;/p&gt;
&lt;h3&gt;Gotchas&lt;/h3&gt;
&lt;h4&gt;Method missing&lt;/h4&gt;
&lt;p&gt;rr doesn&amp;#8217;t always play nice with stubbing/mocking methods that are provided  by &lt;code&gt;method_missing&lt;/code&gt;. I&amp;#8217;ve had particularly bad times with &lt;code&gt;ActionMailer::Base&lt;/code&gt; subclasses.&lt;/p&gt;
&lt;p&gt;The issue is that rr works by doing alias_method down in its inner bowls, and you can&amp;#8217;t alias a method that doesn&amp;#8217;t exist. rr works around this by trying to &lt;code&gt;send&lt;/code&gt; to the non-existent method, hoping that the act of sending will define it before trying to send again. It does so without any arguments, so if you are stubbing a method that has any arguments, you are going to see some funky @ArgumentError@s.&lt;/p&gt;
&lt;p&gt;One work around for this is to stub out method missing itself, and pass it the name of the method you care about. I found the slightly alternative syntax works best:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PeasantMailer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:deliver_burnination&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peasant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;You should be able to use test spies on this too:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;PeasantMailer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;have_received&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;method_missing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:deliver_burnination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;peasant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h4&gt;Test spies for assignment&lt;/h4&gt;
&lt;p&gt;There&amp;#8217;s a possibility you would want to use test spies for while testing assignments:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;species&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;dragon&amp;#39;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;If you were using test/unit, you&amp;#8217;d verify it like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;assert_received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;species&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;dragon&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;There&amp;#8217;s a good chance you&amp;#8217;ll get a message to the effect &lt;code&gt;call&lt;/code&gt; isn&amp;#8217;t defined on the String &amp;#8216;dragon&amp;#8217;. This is caused because inside the block, rr is doing some trickery with &lt;code&gt;method_missing&lt;/code&gt; to record what methods you call on &lt;code&gt;trogdor&lt;/code&gt;. I&amp;#8217;m told that when you do assignment, the ruby interpreter will always return the assigned value, so rr gets confused when it gets a string, rather than it&amp;#8217;s own objects to track method calls.&lt;/p&gt;
&lt;p&gt;The ugly fix for this is to use it&amp;#8217;s method_missing more directly.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;assert_received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;trogdor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;method_missing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:species&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;dragon&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;h3&gt;Anything else?&lt;/h3&gt;
&lt;p&gt;I came In reality, I&amp;#8217;m only beginning to scratch the surface of rr. The terse syntax and test spies are the things that got me hook, but there are other features that look promising, like proxying and stubbing method chains.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=ttGqgGYm5t8:XSb5UhuWJyA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=ttGqgGYm5t8:XSb5UhuWJyA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=ttGqgGYm5t8:XSb5UhuWJyA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=ttGqgGYm5t8:XSb5UhuWJyA:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=ttGqgGYm5t8:XSb5UhuWJyA:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=ttGqgGYm5t8:XSb5UhuWJyA:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=ttGqgGYm5t8:XSb5UhuWJyA:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/ttGqgGYm5t8" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Debugging Cucumber</title>
		<link href="http://technicalpickles.com/posts/debugging-cucumber" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/debugging-cucumber</id>
		<updated>Mon Mar 30 00:00:00 -0400 2009</updated>
		<content type="html">&lt;p&gt;Cucumber has been the new hotness when it comes to acceptance and integration testing. But, like any other tool testing tool, your test cases can fail in unexpected ways.&lt;/p&gt;
&lt;p&gt;This article covers a couple I tricks I found to uncover the cause of cucumber failures.&lt;/p&gt;
&lt;h3&gt;ruby-debug&lt;/h3&gt;
&lt;h4&gt;breakpoint&lt;/h4&gt;
&lt;p&gt;I&amp;#8217;m a big fan a &lt;a href="http://www.datanoise.com/ruby-debug"&gt;ruby-debug&lt;/a&gt;. To get started, you have to add a little setup to &lt;code&gt;features/support/env.rb&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;ruby-debug&amp;#39;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;Then, you can place &lt;code&gt;breakpoint&lt;/code&gt; anywhere in your code, such as in your application code, or somewhere in a step definition&lt;/p&gt;
&lt;notextile&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Then&lt;/span&gt;&lt;span class="sr"&gt; /^the process should exit cleanly$/&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;breakpoint&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="vi"&gt;@exited_cleanly&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Process did not exit cleanly: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@stdout&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;&lt;/notextile&gt;&lt;/p&gt;
&lt;p&gt;Note, that if you put the breakpoint at the end of a block, you need to add another statement after &lt;code&gt;breakpoint&lt;/code&gt;. If you don&amp;#8217;t, then ruby-debug will drop into the calling block, which can be very confusing.&lt;/p&gt;
&lt;notextile&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Then&lt;/span&gt;&lt;span class="sr"&gt; /^the process should exit cleanly$/&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="vi"&gt;@exited_cleanly&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Process did not exit cleanly: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@stdout&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="n"&gt;breakpoint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;&lt;/notextile&gt;&lt;/p&gt;
&lt;h4&gt;Then I debug&lt;/h4&gt;
&lt;p&gt;I found it convenient to have a debug step like &amp;#8220;Then I debug&amp;#8221;, which I put in &lt;code&gt;features/step_definitions/debug_steps.rb&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;Then&lt;/span&gt;&lt;span class="sr"&gt; /^I debug$/&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;breakpoint&lt;/span&gt;
  &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;The main benefit I found to having this step is that you can place it exactly where you want in a problematic feature. If you were to just put it in the step definition you are having problems with, and you use the step in multiple place, then it take more effort to debug to the exact place you want to.&lt;/p&gt;
&lt;h3&gt;Webrat&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://github.com/brynary/webrat/tree/master"&gt;Webrat&lt;/a&gt; is the default tool for doing cucumber testing with Rails. It lets you do things like click links, type and submit forms, and so on.&lt;/p&gt;
&lt;p&gt;There will be times where you try to click on something, for example, and it&amp;#8217;s not there. You&amp;#8217;ll get a big blob of &lt;span class="caps"&gt;HTML&lt;/span&gt; thrown back, and figuring out what that exactly means can be a real drag.&lt;/p&gt;
&lt;h4&gt;save_and_open_page&lt;/h4&gt;
&lt;p&gt;Webrat provides a method &lt;code&gt;save_and_open_page&lt;/code&gt; which captures the current &lt;span class="caps"&gt;HTML&lt;/span&gt;, saves it, and then will open it in a browser for you. Extremely useful.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;When&lt;/span&gt;&lt;span class="sr"&gt; /^I follow &amp;quot;(.*)&amp;quot;$/&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;save_and_open_page&lt;/span&gt;
  &lt;span class="n"&gt;click_link&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;This can get a little out of hand if you use this in a lot of places. Dozens of pages getting opened, even if things aren&amp;#8217;t failing.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="no"&gt;When&lt;/span&gt;&lt;span class="sr"&gt; /^I follow &amp;quot;(.*)&amp;quot;$/&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="k"&gt;begin&lt;/span&gt;
    &lt;span class="n"&gt;click_link&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;rescue&lt;/span&gt;
    &lt;span class="n"&gt;save_and_open_page&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;  
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;&lt;p&gt;This will only open the page if something actually went wrong.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Cucumber is still a very young tool. Better ways of debugging will be found, but until then, ruby-debug and &lt;code&gt;save_and_open_page&lt;/code&gt; will treat you well.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=OAmHCLpIzag:liZl7oigN3o:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=OAmHCLpIzag:liZl7oigN3o:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=OAmHCLpIzag:liZl7oigN3o:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=OAmHCLpIzag:liZl7oigN3o:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=OAmHCLpIzag:liZl7oigN3o:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=OAmHCLpIzag:liZl7oigN3o:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=OAmHCLpIzag:liZl7oigN3o:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/OAmHCLpIzag" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Escape Ruby Regular Expressions</title>
		<link href="http://technicalpickles.com/posts/escape-ruby-regular-expressions" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/escape-ruby-regular-expressions</id>
		<updated>Fri Feb 27 00:00:00 -0500 2009</updated>
		<content type="html">&lt;p&gt;If you&amp;#8217;re doing any work with &lt;a href='http://cukes.info/'&gt;cucumber&lt;/a&gt;, it would do you well to become more familiar with Ruby regular expressions.&lt;/p&gt;

&lt;p&gt;One useful piece of regular expression functionality is that you can interpolate variables inside an regular expression just like you would in a string. In cucumber, we could write a &lt;a href='http://wiki.github.com/aslakhellesoy/cucumber/step-definitions'&gt;step&lt;/a&gt; like this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='no'&gt;Then&lt;/span&gt;&lt;span class='sr'&gt; /^I should see &amp;quot;(.*)&amp;quot;$/&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;text&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
  &lt;span class='n'&gt;assert_match&lt;/span&gt;&lt;span class='sr'&gt; /#{text}/&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='vi'&gt;@response&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;body&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;There is a problem though. If &lt;code&gt;text = &amp;#39;Push Push (Lady Lightning)&lt;/code&gt; and &lt;code&gt;@response_body = &amp;#39;Push Push (Lady Lightning)&lt;/code&gt;, the assertion would fail!&lt;/p&gt;

&lt;p&gt;This is because there are some characters that have special meaning to regular expressions. Here, parentheses indicate &lt;a href='http://www.regular-expressions.info/brackets.html'&gt;capture groups&lt;/a&gt; which lets you extracted matched values. The net effect is the expression would match &amp;#8216;Push Push Lady Lightning&amp;#8217; and capture &amp;#8220;Lady Lighning&amp;#8221;, but would not match &amp;#8216;Push Push (Lady Lightning)&amp;#8217;.&lt;/p&gt;

&lt;p&gt;You can get around this by escaping characters with &lt;code&gt;\&lt;/code&gt; easily enough if we were doing it by hand. That won&amp;#8217;t do for since we have a variable though. Fortunately, there is method &lt;a href='http://www.ruby-doc.org/core/classes/Regexp.html#M001216'&gt;&lt;code&gt;Regexp.escape&lt;/code&gt;&lt;/a&gt; which takes a string and escapes any special characters.&lt;/p&gt;

&lt;p&gt;We can update our step to look like:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='no'&gt;Then&lt;/span&gt;&lt;span class='sr'&gt; /^I should see &amp;quot;(.*)&amp;quot;$/&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;text&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
  &lt;span class='n'&gt;assert_match&lt;/span&gt;&lt;span class='sr'&gt; /#{Regexp.escape(text)}/&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='vi'&gt;@response&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;body&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This assertion will now be successful if &lt;code&gt;text = &amp;#39;Push Push (Lady Lightning)&lt;/code&gt; and &lt;code&gt;@response_body = &amp;#39;Push Push (Lady Lightning)&lt;/code&gt; as you&amp;#8217;d expected.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=nSxFyVmrtus:ccPbdU644cA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=nSxFyVmrtus:ccPbdU644cA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=nSxFyVmrtus:ccPbdU644cA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=nSxFyVmrtus:ccPbdU644cA:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=nSxFyVmrtus:ccPbdU644cA:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=nSxFyVmrtus:ccPbdU644cA:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=nSxFyVmrtus:ccPbdU644cA:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/nSxFyVmrtus" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Using method_missing and respond_to? to create dynamic methods</title>
		<link href="http://technicalpickles.com/posts/using-method_missing-and-respond_to-to-create-dynamic-methods" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/using-method_missing-and-respond_to-to-create-dynamic-methods</id>
		<updated>Tue Feb 17 00:00:00 -0500 2009</updated>
		<content type="html">&lt;p&gt;&lt;code&gt;method_missing&lt;/code&gt; is a well-known tool in the Ruby metaprogramming toolbox. It&amp;#8217;s callback method you can implement that gets called when a object tries to call a method that&amp;#8217;s, well, missing. A well known example of this is ActiveRecord dynamic finders. For example, if your &lt;code&gt;User&lt;/code&gt; has an &lt;code&gt;email&lt;/code&gt; attribute, you can &lt;code&gt;User.find_by_email(&amp;#39;joe@example.com&amp;#39;)&lt;/code&gt; even though &lt;code&gt;User&lt;/code&gt; nor &lt;code&gt;ActiveRecord::Base&lt;/code&gt; have defined it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;method_missing&lt;/code&gt;&amp;#8217;s cousin, &lt;code&gt;respond_to?&lt;/code&gt;, is often forgotten though. &lt;code&gt;respond_to?&lt;/code&gt; is used to determine if an object responds to a method. It is often used to check that an object knows about a method before actually calling it, in order to avoid an error at runtime about the method existing.&lt;/p&gt;

&lt;p&gt;To have a consistent API when using &lt;code&gt;method_missing&lt;/code&gt;, it&amp;#8217;s important to implement a corresponding &lt;code&gt;respond_to?&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id='example'&gt;Example&lt;/h3&gt;

&lt;p&gt;Imagine we have a &lt;code&gt;Legislator&lt;/code&gt; class. We want a dynamic finder that will translate &lt;code&gt;find_by_first_name(&amp;#39;John&amp;#39;)&lt;/code&gt; to &lt;code&gt;find(:first_name =&amp;gt; &amp;#39;John&amp;#39;)&lt;/code&gt;. Here&amp;#8217;s a first pass:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Legislator&lt;/span&gt;
  &lt;span class='c1'&gt;# Pretend this is a real implementation&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;find&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;conditions&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='p'&gt;{})&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
  
  &lt;span class='c1'&gt;# Define on self, since it&amp;#39;s  a class method&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;method_missing&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;arguments&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='o'&gt;&amp;amp;&lt;/span&gt;&lt;span class='n'&gt;block&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='c1'&gt;# the first argument is a Symbol, so you need to_s it if you want to pattern match&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_s&lt;/span&gt; &lt;span class='o'&gt;=~&lt;/span&gt; &lt;span class='sr'&gt;/^find_by_(.*)$/&lt;/span&gt;
      &lt;span class='n'&gt;find&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='vg'&gt;$1&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_sym&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='n'&gt;arguments&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;else&lt;/span&gt;
      &lt;span class='k'&gt;super&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That seems to do the trick. But &lt;code&gt;Legislator.respond_to?(:find_by_first_name)&lt;/code&gt; would return &lt;code&gt;false&lt;/code&gt;. Let&amp;#8217;s add &lt;code&gt;respond_to?&lt;/code&gt; to fix it.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Legislator&lt;/span&gt;
  &lt;span class='c1'&gt;# ommitted&lt;/span&gt;
  
  &lt;span class='c1'&gt;# It&amp;#39;s important to know Object defines respond_to to take two parameters: the method to check, and whether to include private methods&lt;/span&gt;
  &lt;span class='c1'&gt;# http://www.ruby-doc.org/core/classes/Object.html#M000333&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;respond_to?&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;include_private&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='kp'&gt;false&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_s&lt;/span&gt; &lt;span class='o'&gt;=~&lt;/span&gt; &lt;span class='sr'&gt;/^find_by_(.*)$/&lt;/span&gt;
      &lt;span class='kp'&gt;true&lt;/span&gt;
    &lt;span class='k'&gt;else&lt;/span&gt;
      &lt;span class='k'&gt;super&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As commented, &lt;code&gt;respond_to?&lt;/code&gt; takes two parameters. If you don&amp;#8217;t do this, there&amp;#8217;s a chance some library may expect to be able to invoke &lt;code&gt;respond_to?&lt;/code&gt; with 2 parameters, resulting in an &lt;code&gt;ArgumentError&lt;/code&gt;. In particular, the RSpec mocking library does this exactly this, and caused me a bit of befuddlement until I turned on full backtraces.&lt;/p&gt;

&lt;h3 id='possible_refactorings'&gt;Possible refactorings&lt;/h3&gt;

&lt;h4 id='dry'&gt;DRY&lt;/h4&gt;

&lt;p&gt;You might notice it&amp;#8217;s not very DRY. We use the same pattern matching twice. This particular logic is simple, but it could be grow to be more complicated.&lt;/p&gt;

&lt;p&gt;We can again look to ActiveRecord for inspiration. It encapsulates the logic in &lt;a href='http://api.rubyonrails.org/classes/ActiveRecord/DynamicFinderMatch.html'&gt;ActiveRecord::DynamicFinderMatch&lt;/a&gt;, to avoid repetition between &lt;code&gt;method_missing&lt;/code&gt; and &lt;code&gt;respond_to?&lt;/code&gt;, in addition to simplifying the methods.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;LegislatorDynamicFinderMatch&lt;/span&gt;
  &lt;span class='kp'&gt;attr_accessor&lt;/span&gt; &lt;span class='ss'&gt;:attribute&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;initialize&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_s&lt;/span&gt; &lt;span class='o'&gt;=~&lt;/span&gt; &lt;span class='sr'&gt;/^find_by_(.*)$/&lt;/span&gt;
      &lt;span class='vi'&gt;@attribute&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='vg'&gt;$1&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_sym&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
  
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;match?&lt;/span&gt;
    &lt;span class='vi'&gt;@attribute&lt;/span&gt; &lt;span class='o'&gt;!=&lt;/span&gt; &lt;span class='kp'&gt;nil&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Legislator&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;method_missing&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;arguments&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='o'&gt;&amp;amp;&lt;/span&gt;&lt;span class='n'&gt;block&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;match&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='no'&gt;LegislatorDynamicFinderMatch&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;match?&lt;/span&gt;
      &lt;span class='n'&gt;find&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;attribute&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='n'&gt;arguments&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;else&lt;/span&gt;
      &lt;span class='k'&gt;super&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;respond_to?&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;include_private&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='kp'&gt;false&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='no'&gt;LegislatorDynamicFinderMatch&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;match?&lt;/span&gt;
      &lt;span class='kp'&gt;true&lt;/span&gt;
    &lt;span class='k'&gt;else&lt;/span&gt;
      &lt;span class='k'&gt;super&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id='caching_'&gt;Caching &lt;code&gt;method_missing&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Always passing through &lt;code&gt;method_missing&lt;/code&gt; &lt;a href='http://www.jroller.com/dscataglini/entry/speeding_up_method_missing'&gt;can be slow&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another lesson we can take from is ActiveRecord is defining the method during &lt;code&gt;method_missing&lt;/code&gt;, and then &lt;code&gt;send&lt;/code&gt; to the now-defined method.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Legislator&lt;/span&gt;    
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;method_missing&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='n'&gt;arguments&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='o'&gt;&amp;amp;&lt;/span&gt;&lt;span class='n'&gt;block&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;match&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='no'&gt;LegislatorDynamicFinderMatch&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;match?&lt;/span&gt;
      &lt;span class='n'&gt;define_dynamic_finder&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;attribute&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
      &lt;span class='nb'&gt;send&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;method_sym&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;arguments&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;else&lt;/span&gt;
      &lt;span class='k'&gt;super&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
  
  &lt;span class='kp'&gt;protected&lt;/span&gt;
  
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;define_dynamic_finder&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;finder&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;attribute&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='nb'&gt;class_eval&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class='no'&gt;RUBY&lt;/span&gt;
&lt;span class='sh'&gt;      def self.#{finder}(#{attribute})        # def self.find_by_first_name(first_name)&lt;/span&gt;
&lt;span class='sh'&gt;        find(:#{attribute} =&amp;gt; #{attribute})   #   find(:first_name =&amp;gt; first_name)&lt;/span&gt;
&lt;span class='sh'&gt;      end                                     # end&lt;/span&gt;
&lt;span class='no'&gt;    RUBY&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;h3 id='testing'&gt;Testing&lt;/h3&gt;

&lt;p&gt;Being the test-minded individual that I am, no code would be complete without some testing.&lt;/p&gt;

&lt;p&gt;Creating the &lt;code&gt;LegislatorDynamicFinderMatch&lt;/code&gt; makes it straight forward to test your matching logic. An example using RSpec could look like:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='n'&gt;describe&lt;/span&gt; &lt;span class='no'&gt;LegislatorDynamicFinderMatch&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
  &lt;span class='n'&gt;describe&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;find_by_first_name&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
    &lt;span class='n'&gt;before&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
      &lt;span class='vi'&gt;@match&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='no'&gt;LegislatorDynamicFinderMatch&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;:find_by_first_name&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
      
    &lt;span class='n'&gt;it&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;should have attribute :first_name&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
      &lt;span class='vi'&gt;@match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;attribute&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;should&lt;/span&gt; &lt;span class='o'&gt;==&lt;/span&gt; &lt;span class='ss'&gt;:first_name&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
    
    &lt;span class='n'&gt;it&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;should be a match&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
      &lt;span class='vi'&gt;@match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;should&lt;/span&gt; &lt;span class='n'&gt;be_a_match&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
  
  &lt;span class='n'&gt;describe&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;zomg&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
    &lt;span class='n'&gt;before&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
      &lt;span class='vi'&gt;@match&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='no'&gt;LegislatorDynamicFinderMatch&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;:zomg&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
    
    &lt;span class='n'&gt;it&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;should have nil attribute&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
      &lt;span class='vi'&gt;@match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;attribute&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;should&lt;/span&gt; &lt;span class='n'&gt;be_nil&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
    
    &lt;span class='n'&gt;it&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;should not be a match&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
      &lt;span class='vi'&gt;@match&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;should_not&lt;/span&gt; &lt;span class='n'&gt;be_a_match&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you are creating dynamic methods which are just syntactic sugar around another method, like our finder, I think it is sufficient to use mocking to ensure the main method gets called with the correct arguments. Here&amp;#8217;s an RSpec example:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='n'&gt;describe&lt;/span&gt; &lt;span class='no'&gt;Legislator&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;dynamic find_by_first_name&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
  &lt;span class='n'&gt;it&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;should call find(:first_name =&amp;gt; first_name)&amp;#39;&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
    &lt;span class='no'&gt;Legislator&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;should_receive&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;:find&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;with&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;:first_name&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    
    &lt;span class='no'&gt;Legislator&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;find_by_first_name&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;h3 id='summary'&gt;Summary&lt;/h3&gt;

&lt;p&gt;If you&amp;#8217;re going to use &lt;code&gt;method_missing&lt;/code&gt;, be sure to implement &lt;code&gt;respond_to?&lt;/code&gt; in kind.&lt;/p&gt;

&lt;p&gt;You can improve the DRYness of these methods by encapsulating the matching logic in a separate class, and you can improve performance by defining methods when they are missing.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=vZQq9X9nFnk:G4z-YDO1y7E:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=vZQq9X9nFnk:G4z-YDO1y7E:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=vZQq9X9nFnk:G4z-YDO1y7E:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=vZQq9X9nFnk:G4z-YDO1y7E:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=vZQq9X9nFnk:G4z-YDO1y7E:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=vZQq9X9nFnk:G4z-YDO1y7E:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=vZQq9X9nFnk:G4z-YDO1y7E:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/vZQq9X9nFnk" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Autoloading factory_girl definitions</title>
		<link href="http://technicalpickles.com/posts/autoloading-factory_girl-definitions" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/autoloading-factory_girl-definitions</id>
		<updated>Sun Feb 15 00:00:00 -0500 2009</updated>
		<content type="html">&lt;p&gt;I&amp;#8217;d like to point out a feature that silently made it into &lt;a href='http://thoughtbot.com/projects/factory_girl'&gt;factory_girl&lt;/a&gt; as of 1.1.4: autoloading of factory definitions.&lt;/p&gt;

&lt;p&gt;If you create &lt;code&gt;test/factories.rb&lt;/code&gt;, &lt;code&gt;spec/factories.rb&lt;/code&gt;, they will automatically be required, making your factories available to your tests or specs.&lt;/p&gt;

&lt;p&gt;Additionally, you can create directories named &lt;code&gt;test/factories&lt;/code&gt; and &lt;code&gt;spec/factories&lt;/code&gt;, and any &lt;code&gt;.rb&lt;/code&gt; files will be required as well. I generally prefer this way, and name factories like &lt;code&gt;urmodel_factory.rb&lt;/code&gt;, such that fuzzy finding files makes it easy to open up a specific factory.&lt;/p&gt;

&lt;p&gt;This functionality was part the &lt;a href='http://github.com/technicalpickles/factory_girl_on_rails/tree/master'&gt;factory_girl_on_rails&lt;/a&gt; plugin I wrote several months ago before there was any autoloading. Once I got annoyed by having to install a plugin in addition to a gem, I found a way to do it inside of factory_girl herself, and you can now benefit from that work.&lt;/p&gt;

&lt;p&gt;Relatedly, you can consider factory_girl_on_rails deprecated in favor of the built-in factory_girl functionality.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qKkZwvwR3b0:X9zVScAFZmY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qKkZwvwR3b0:X9zVScAFZmY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=qKkZwvwR3b0:X9zVScAFZmY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qKkZwvwR3b0:X9zVScAFZmY:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=qKkZwvwR3b0:X9zVScAFZmY:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qKkZwvwR3b0:X9zVScAFZmY:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=qKkZwvwR3b0:X9zVScAFZmY:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/qKkZwvwR3b0" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Adding Cucumber to a Ruby project</title>
		<link href="http://technicalpickles.com/posts/adding-cucumber-to-a-ruby-project" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/adding-cucumber-to-a-ruby-project</id>
		<updated>Fri Feb 13 00:00:00 -0500 2009</updated>
		<content type="html">&lt;p&gt;I had &lt;a href='http://technicalpickles.com/posts/jeweler-now-with-support-for-test-unit-minitest-rspec-and-cucumber'&gt;announced recently&lt;/a&gt; that &lt;a href='github.com/technicalpickles/jeweler/'&gt;jeweler&lt;/a&gt; now supports generating the files needed to use cucumber in your project. Personally, I&amp;#8217;ve always been annoyed when looking for documentation about, for example, adding cucumber to your project, and then come across stuff that is like &amp;#8220;Oh hai, just generate a project with jeweler, lol.&amp;#8221;&lt;/p&gt;

&lt;p&gt;I don&amp;#8217;t want to be that guy, so here&amp;#8217;s the process I used for adding Cucumber to the testing mix of jeweler.&lt;/p&gt;

&lt;h3 id='background'&gt;Background&lt;/h3&gt;

&lt;p&gt;I&amp;#8217;m not going to go any details about actually using Cucumber, so you&amp;#8217;ll want to do a little reading ahead of time. I&amp;#8217;d recommend the &lt;a href='http://cukes.info/'&gt;Cucumber website&lt;/a&gt;, the &lt;a href='http://wiki.github.com/aslakhellesoy/cucumber'&gt;Cucumber Wiki on GitHub&lt;/a&gt;, and &lt;a href='http://www.pragprog.com/titles/achbd/the-rspec-book'&gt;The RSpec Book (currently in beta)&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id='file_layout'&gt;File layout&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Everything cucumber should live in &lt;code&gt;features&lt;/code&gt; directory.&lt;/li&gt;

&lt;li&gt;&lt;a href='http://wiki.github.com/aslakhellesoy/cucumber/feature-introduction'&gt;Features&lt;/a&gt; describing your project live in this &lt;code&gt;features&lt;/code&gt; directory&lt;/li&gt;

&lt;li&gt;&lt;code&gt;features/support/env.rb&lt;/code&gt; sets up the &lt;a href='http://wiki.github.com/aslakhellesoy/cucumber/a-whole-new-world'&gt;world&lt;/a&gt; that the features will be run under&lt;/li&gt;

&lt;li&gt;&lt;a href='http://wiki.github.com/aslakhellesoy/cucumber/step-definitions'&gt;Steps&lt;/a&gt; live in the &lt;code&gt;features/step_definitions&lt;/code&gt; directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here&amp;#8217;s some convient bash one-liner for making the directories:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir -p features/{support,step_definitions}&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id='envrb'&gt;env.rb&lt;/h3&gt;

&lt;p&gt;You do have one choice to make here&amp;#8230; what framework do you want to use implement your steps? The wiki has instructions for using &lt;a href='http://wiki.github.com/aslakhellesoy/cucumber/using-testunit'&gt;Test::Unit&lt;/a&gt;, &lt;a href='http://wiki.github.com/aslakhellesoy/cucumber/using-minitest'&gt;MiniTest&lt;/a&gt;, and &lt;a href='http://wiki.github.com/aslakhellesoy/cucumber/rspec-expectations'&gt;RSpec&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s also worth noting that &lt;code&gt;env.rb&lt;/code&gt; is the cucumber-equivalent of &lt;code&gt;test_helper.rb&lt;/code&gt; or &lt;code&gt;spec_helper.rb&lt;/code&gt;, so do any configuration or &lt;code&gt;require&lt;/code&gt;ing here. For example you probably want to require your main ruby file from the &lt;code&gt;lib&lt;/code&gt; directory. For jeweler, I did:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='vg'&gt;$LOAD_PATH&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;unshift&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='no'&gt;File&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;dirname&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='bp'&gt;__FILE__&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='o'&gt;+&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;/../../lib&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
&lt;span class='nb'&gt;require&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;jeweler&amp;#39;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;h3 id='rake_configuration'&gt;Rake configuration&lt;/h3&gt;

&lt;p&gt;You do have a &lt;code&gt;Rakefile&lt;/code&gt;, right? Given the file layout above, you can add this snippet:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;begin&lt;/span&gt;
  &lt;span class='nb'&gt;require&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;cucumber/rake/task&amp;#39;&lt;/span&gt;
  &lt;span class='no'&gt;Cucumber&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Rake&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Task&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;:features&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
&lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='no'&gt;LoadError&lt;/span&gt;
  &lt;span class='nb'&gt;puts&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;Cucumber is not available. In order to run features, you must: sudo gem install cucumber&amp;quot;&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;h3 id='summary'&gt;Summary&lt;/h3&gt;

&lt;p&gt;Adding cucumber to a project is pretty straightforward, but all the info was never in one place. Hopefully, this article addresses that. Now you can have fun writing your features and step definitions.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qGzF7jSWclk:QjN9zEtpCSk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qGzF7jSWclk:QjN9zEtpCSk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=qGzF7jSWclk:QjN9zEtpCSk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qGzF7jSWclk:QjN9zEtpCSk:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=qGzF7jSWclk:QjN9zEtpCSk:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=qGzF7jSWclk:QjN9zEtpCSk:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=qGzF7jSWclk:QjN9zEtpCSk:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/qGzF7jSWclk" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Jeweler, now with support for test/unit, minitest, rspec, and cucumber</title>
		<link href="http://technicalpickles.com/posts/jeweler-now-with-support-for-test-unit-minitest-rspec-and-cucumber" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/jeweler-now-with-support-for-test-unit-minitest-rspec-and-cucumber</id>
		<updated>Tue Feb 03 00:00:00 -0500 2009</updated>
		<content type="html">&lt;p&gt;I had announced &lt;a href='http://github.com/technicalpickles/jeweler'&gt;jeweler&lt;/a&gt; &lt;a href='http://technicalpickles.com/posts/craft-the-perfect-gem-with-jeweler'&gt;a few weeks ago&lt;/a&gt;, and it&amp;#8217;s been very well received. It&amp;#8217;s even the &lt;a href='http://github.com'&gt;GitHub&lt;/a&gt; recommended way of managing your gems, which is pretty awesome.&lt;/p&gt;

&lt;p&gt;Things haven&amp;#8217;t been sitting still though. Version &lt;a href='http://github.com/technicalpickles/jeweler/tree/v0.8.1'&gt;0.8.1&lt;/a&gt; is now released. Highlights include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Support for generating projects with &lt;a href='http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html'&gt;test/unit&lt;/a&gt;, &lt;a href='http://blog.zenspider.com/minitest/'&gt;minitest&lt;/a&gt;, and &lt;a href='http://rspec.info/'&gt;rspec&lt;/a&gt; in addition to &lt;a href='http://github.com/thoughtbot/shoulda'&gt;shoulda&lt;/a&gt; and &lt;a href='http://github.com/chneukirchen/bacon/'&gt;bacon&lt;/a&gt;.&lt;/li&gt;

&lt;li&gt;Support for &lt;a href='http://cukes.info/'&gt;cucumber&lt;/a&gt; stories&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can get it using the usual gem install command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Run the following if you haven&amp;#39;t already:
gem sources -a http://gems.github.com
# Install the gem:
sudo gem install technicalpickles-jeweler&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;See the &lt;code&gt;jeweler&lt;/code&gt; command for possible arguments:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Usage: jeweler [options] reponame
e.g. jeweler the-perfect-gem
        --bacon                      generate bacon specs
        --shoulda                    generate shoulda tests
        --testunit                   generate test/unit tests
        --miniunit                   generate mini/unit tests
        --rspec                      generate rspec tests
        --create-repo                create the repository on GitHub
        --summary [SUMMARY]          specify the summary of the project
    -h, --help                       display this help and exit&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That is all for now, so get crafting!&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=i4q9xKG4t50:2_qhpZTV_hc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=i4q9xKG4t50:2_qhpZTV_hc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=i4q9xKG4t50:2_qhpZTV_hc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=i4q9xKG4t50:2_qhpZTV_hc:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=i4q9xKG4t50:2_qhpZTV_hc:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=i4q9xKG4t50:2_qhpZTV_hc:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=i4q9xKG4t50:2_qhpZTV_hc:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/i4q9xKG4t50" height="1" width="1"/&gt;</content>
	</entry>
	
	<entry>
		<title>Craft the perfect gem with Jeweler</title>
		<link href="http://technicalpickles.com/posts/craft-the-perfect-gem-with-jeweler" rel="alternate" type="text/html" />
		<id>http://technicalpickles.com/posts/craft-the-perfect-gem-with-jeweler</id>
		<updated>Mon Jan 19 00:00:00 -0500 2009</updated>
		<content type="html">&lt;p&gt;I&amp;#8217;ve been working on &lt;a href='http://github.com/technicalpickles/jeweler'&gt;Jeweler&lt;/a&gt; for some time now, but never announced it officially. That changes now. This is kind of a long winded intro, so if you want to get to just jump into the action without the explanation, check out the latest &lt;a href='http://github.com/technicalpickles/jeweler'&gt;README&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id='intro'&gt;Intro&lt;/h3&gt;

&lt;p&gt;&lt;a href='http://www.rubygems.org/'&gt;Rubygems&lt;/a&gt; are the awesome way of distributing your code to others. &lt;a href='http://github.com/'&gt;GitHub&lt;/a&gt; and &lt;a href='http://git-scm.com/'&gt;git&lt;/a&gt; are the best tools for version control of your project. GitHub can even &lt;a href='http://gems.github.com/'&gt;generate a Rubygem if you include a gemspec&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The trouble is when developing your Rubygems on GitHub, you generally do one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Manage the gemspec by hand * &amp;#8230; why bother doing something by hand when you can automate it?&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Write your own Rake stuff to create the Gem::Specification and output it to a gemspec file, and deal with keeping the Rakefile and gemspec in sync * &amp;#8230; why keep reinventing the wheel for each project?&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Use hoe or echoe for generating the gemspec&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&amp;#8230; why use tools made for the days before GitHub existed?&lt;/li&gt;

&lt;li&gt;&amp;#8230; why have extra stuff you aren&amp;#8217;t going to use or want?&lt;/li&gt;

&lt;li&gt;&amp;#8230; what&amp;#8217;s with the weird configuration that&amp;#8217;s not particularly well documented except by example?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Jeweler was created with a few goals in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only use a Gem::Specification as configuration&lt;/li&gt;

&lt;li&gt;Be one command away from version bumping and releasing&lt;/li&gt;

&lt;li&gt;Store version information in one place&lt;/li&gt;

&lt;li&gt;Only concern itself with git, gems, and versioning&lt;/li&gt;

&lt;li&gt;Not be a requirement for using your Rakefile (you just wouldn&amp;#8217;t be able to use its tasks)&lt;/li&gt;

&lt;li&gt;Use Jeweler to manage Jeweler. Oh the meta!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id='installation'&gt;Installation&lt;/h3&gt;

&lt;p&gt;Run the following if you haven&amp;#8217;t already:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem sources -a http://gems.github.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Install the gem:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install technicalpickles-jeweler&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id='configuration_for_an_existing_project'&gt;Configuration for an existing project&lt;/h3&gt;

&lt;p&gt;Armed with the gem, we can begin diving into an example. &lt;a href='http://github.com/technicalpickles/the-perfect-gem/tree'&gt;the-perfect-gem&lt;/a&gt; was setup as a simple example and showcase:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;begin&lt;/span&gt;
  &lt;span class='nb'&gt;require&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;jeweler&amp;#39;&lt;/span&gt;
  &lt;span class='no'&gt;Jeweler&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Tasks&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;name&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;jeweler&amp;quot;&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;executables&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;jeweler&amp;quot;&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;summary&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;Simple and opinionated helper for creating Rubygem projects on GitHub&amp;quot;&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;email&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;josh@technicalpickles.com&amp;quot;&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;homepage&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;http://github.com/technicalpickles/jeweler&amp;quot;&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;description&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;Simple and opinionated helper for creating Rubygem projects on GitHub&amp;quot;&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;authors&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s2'&gt;&amp;quot;Josh Nichols&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;files&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt;  &lt;span class='no'&gt;FileList&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s2'&gt;&amp;quot;[A-Z]*&amp;quot;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;{bin,generators,lib,test}/**/*&amp;quot;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;lib/jeweler/templates/.gitignore&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt;
    &lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;add_dependency&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;schacon-git&amp;#39;&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='no'&gt;LoadError&lt;/span&gt;
  &lt;span class='nb'&gt;puts&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com&amp;quot;&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here&amp;#8217;s a rundown of what&amp;#8217;s happening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Wrap everything in a begin block, and rescue from LoadError * This lets us degrade gracefully if jeweler isn&amp;#8217;t installed&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Create an instance of &lt;code&gt;Jeweler::Tasks&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;It gets yielded a new &lt;code&gt;Gem::Specification&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;This is where all the configuration happens&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Things you definitely need to specify: * &lt;code&gt;name&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Things you probably want to specify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;summary&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;&lt;code&gt;email&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;&lt;code&gt;homepage&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;&lt;code&gt;description&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;&lt;code&gt;authors&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Things you can specify, but have defaults * &lt;code&gt;files&lt;/code&gt;, defaults to &lt;code&gt;FileList[&amp;quot;[A-Z]*.*&amp;quot;, &amp;quot;{bin,generators,lib,test,spec}/**/*&amp;quot;]&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Things you shouldn&amp;#8217;t specify: * &lt;code&gt;version&lt;/code&gt;, because Jeweler takes care of this for you&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Other things of interest&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;executables&lt;/code&gt;, if you have any scripts&lt;/li&gt;

&lt;li&gt;&lt;code&gt;add_dependency&lt;/code&gt;, if you have any dependencies&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Keep in mind that this is a &lt;code&gt;Gem::Specification&lt;/code&gt;, so you can do whatever you would need to do to get your gem in shape. See the &lt;a href='http://www.rubygems.org/read/chapter/20#page85'&gt;reference&lt;/a&gt; for more details.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id='bootstrap_a_new_project'&gt;Bootstrap a new project&lt;/h3&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before proceeding, take a minute to setup your git environment, specifically &lt;a href='http://github.com/guides/tell-git-your-user-name-and-email-address'&gt;setup your name and email for git&lt;/a&gt; and &lt;a href='http://github.com/guides/local-github-config'&gt;your username and token for GitHub&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git config --global user.email johndoe@example.com
$ git config --global user.name &amp;#39;John Doe&amp;#39;
$ git config --global github.user johndoe
$ git config --global github.token 55555555555555&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Jeweler provides a generator, &lt;code&gt;jeweler&lt;/code&gt;. It requires only one argument, the name of a repo you want to create. It also takes a few options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&amp;#8211;&lt;a href='http://github.com/thoughtbot/shoulda'&gt;shoulda&lt;/a&gt;: generate shoulda-based tests (this is the default)&lt;/li&gt;

&lt;li&gt;&amp;#8211;&lt;a href='http://github.com/chneukirchen/bacon/tree/master'&gt;bacon&lt;/a&gt;: generate bacon-based tests&lt;/li&gt;

&lt;li&gt;&amp;#8211;summary SUMMARY: specify the summary to use&lt;/li&gt;

&lt;li&gt;&amp;#8211;create-repo: create the repo on GitHub and enable rubygems for it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting it all together looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ jeweler --create-repo --summary &amp;quot;Oh so perfect&amp;quot; the-perfect-gem&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The effect is this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Creates the the-perfect-gem directory&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Seeds it with some basic files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.gitignore&lt;/code&gt;, with the usual suspects predefined&lt;/li&gt;

&lt;li&gt;&lt;code&gt;Rakefile&lt;/code&gt;, setup with tasks for jeweler, test, rdoc, and rcov&lt;/li&gt;

&lt;li&gt;&lt;code&gt;README&lt;/code&gt;, with your project name&lt;/li&gt;

&lt;li&gt;&lt;code&gt;LICENSE&lt;/code&gt;, MIT, with your name prefilled&lt;/li&gt;

&lt;li&gt;&lt;code&gt;test/test_helper&lt;/code&gt;, setup with shoulda, mocha, and a re-opened &lt;code&gt;Test::Unit::TestCase&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;&lt;code&gt;test/the_perfect_gem.rb&lt;/code&gt;, placeholder failing test&lt;/li&gt;

&lt;li&gt;&lt;code&gt;lib/the_perfect_gem.rb&lt;/code&gt;, placeholder library file&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Initializes a git repository&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Sets up &lt;code&gt;git@github.com:johndoe/jeweler.git&lt;/code&gt; as the &lt;code&gt;origin&lt;/code&gt; git remote&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Makes an initial commit&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Creates up a new repository on GitHub and pushes to it (omit &amp;#8211;create-repo to skip this)&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Enables RubyGems for the repository&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id='overview_of_jeweler_workflow'&gt;Overview of Jeweler workflow&lt;/h3&gt;

&lt;p&gt;Here&amp;#8217;s the general idea:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hack, commit, hack, commit, etc, etc&lt;/li&gt;

&lt;li&gt;Version bump&lt;/li&gt;

&lt;li&gt;Release&lt;/li&gt;

&lt;li&gt;Have a delicious scotch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The hacking and the scotch are up to you, but Jeweler provides rake tasks for the rest.&lt;/p&gt;

&lt;h3 id='versioning'&gt;Versioning&lt;/h3&gt;

&lt;p&gt;Versioning information is stored in &lt;code&gt;VERSION.yml&lt;/code&gt;. It&amp;#8217;s a plain YAML file which contains three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;major&lt;/li&gt;

&lt;li&gt;minor&lt;/li&gt;

&lt;li&gt;patch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider &lt;code&gt;1.5.3&lt;/code&gt;. This maps to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;major == 1&lt;/li&gt;

&lt;li&gt;minor == 5&lt;/li&gt;

&lt;li&gt;patch == 3&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id='your_first_time'&gt;Your first time&lt;/h5&gt;

&lt;p&gt;When you first start using Jeweler, there won&amp;#8217;t be a &lt;code&gt;VERSION.yml&lt;/code&gt;, so it&amp;#8217;ll assume 0.0.0.&lt;/p&gt;

&lt;p&gt;If you need some arbitrary version, you can do one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rake version:write MAJOR=6 MINOR=0 PATCH=3&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Write &lt;code&gt;VERSION.yml&lt;/code&gt; by hand (lame)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id='after_that'&gt;After that&lt;/h4&gt;

&lt;p&gt;You have a few rake tasks for doing the version bump:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rake version:bump:patch # 1.5.1 -&amp;gt; 1.5.2
$ rake version:bump:minor # 1.5.1 -&amp;gt; 1.6.0
$ rake version:bump:major # 1.5.1 -&amp;gt; 2.0.0&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you need to do an arbitrary bump, use the same task you used to create &lt;code&gt;VERSION.yml&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rake version:write MAJOR=6 MINOR=0 PATCH=3&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The process of version bumping does a commit to your repo, so make sure your repo is in a clean state (ie nothing uncommitted).&lt;/p&gt;

&lt;h4 id='release_it'&gt;Release it&lt;/h4&gt;

&lt;p&gt;It&amp;#8217;s pretty straight forward:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rake release&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This takes care of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generating a &lt;code&gt;.gemspec&lt;/code&gt; for you project, with the version you just bumped to&lt;/li&gt;

&lt;li&gt;Commit and push the updated &lt;code&gt;.gemspec&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Create a versioned tag&lt;/li&gt;

&lt;li&gt;Push the tag&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id='play_the_waiting_game'&gt;Play the waiting game&lt;/h4&gt;

&lt;p&gt;How do you know when your gem is built? &lt;a href='http://hasmygembuiltyet.org/'&gt;Has My Gem Built Yet&lt;/a&gt; was specifically designed to answer that question.&lt;/p&gt;

&lt;p&gt;If it happens to be down, you can also check out the GitHub Gem repo&amp;#8217;s &lt;a href='http://gems.github.com/list.html'&gt;list&lt;/a&gt;. Just search for yourname-yourrepo.&lt;/p&gt;

&lt;h4 id='putting_it_all_together'&gt;Putting it all together&lt;/h4&gt;

&lt;pre&gt;&lt;code&gt;# hack, hack, hack, commit, etc
$ rake version:bump:patch release&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now browse to http://hasmygembuiltyet.org/yourname/yourproject, and wait for it to be built.&lt;/p&gt;

&lt;h3 id='finale'&gt;Finale&lt;/h3&gt;

&lt;p&gt;You should have a good idea of what Jeweler is all about by now. It makes gem creation an impulse action. It makes managing versions of gems nice and automated.&lt;/p&gt;

&lt;p&gt;Oh, and did I mention it is the &lt;a href='http://img.skitch.com/20090119-h644bj6enrn2t8i5tej37uxif.jpg'&gt;GitHub recommended way for maintaining your gem&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;Now go forth, and craft the perfect gem.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=2ma3CRuGZkc:ohzY4LftkWg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=2ma3CRuGZkc:ohzY4LftkWg:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=2ma3CRuGZkc:ohzY4LftkWg:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=2ma3CRuGZkc:ohzY4LftkWg:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=2ma3CRuGZkc:ohzY4LftkWg:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/TechnicalPickles?a=2ma3CRuGZkc:ohzY4LftkWg:GLiUmhXMQSk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/TechnicalPickles?i=2ma3CRuGZkc:ohzY4LftkWg:GLiUmhXMQSk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/TechnicalPickles/~4/2ma3CRuGZkc" height="1" width="1"/&gt;</content>
	</entry>
	
</feed>
