<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>technicalpickles - Josh Nichols on the Internet</title>

	<link href="http://technicalpickles.com/posts.atom" rel="self" type="application/atom+xml"/>
	<link href="http://technicalpickles.com/posts.html" type="text/html"/>

	<updated>2013-09-01T00:00:00+00:00</updated>
	<author>
		<name>Josh Nichols</name>
		<email>josh@technicalpickles.com</email>
	</author>

	<id>tag:technicalpickles.com,2005:/posts</id>

	
	<entry>
		<title>Parsing CSV with Ruby</title>
		<link href="http://technicalpickles.com/posts/parsing-csv-with-ruby" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/parsing-csv-with-ruby</id>
		<updated>2013-09-01T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;I’m filing this one under “blog posts I wish existed when I was googling.” If you are dealing with data on the web, you are probably most familiar with JSON and XML. Less common nowadays is CSV, but if it’s all you have, and the alternative is screen scraping, then you are thankful.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://mlkshk.com/r/5NX8.gif&quot; alt=&quot;death by snoo snoo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Imagine we have some data in CSV:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Year,Make,Model,Description,Price
1997,Ford,E350,&quot;ac, abs, moon&quot;,3000.00
1999,Chevy,&quot;Venture &quot;&quot;Extended Edition&quot;&quot;&quot;,&quot;&quot;,4900.00
1999,Chevy,&quot;Venture &quot;&quot;Extended Edition, Very Large&quot;&quot;&quot;,,5000.00
1996,Jeep,Grand Cherokee,&quot;MUST SELL!
air, moon roof, loaded&quot;,4799.00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now as a Ruby developer, particularly that has been infected by Rails, you’d be able to imagine this as an array of hashes, with keys/values using the column header, as the keys symbolized, and the values converted to numerics and blank ones converted to nil:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[
  {:year =&amp;gt; 1997, :make =&amp;gt; 'Ford', :model =&amp;gt; 'E350', :description =&amp;gt; 'ac, abs, moon', :price =&amp;gt; 3000.00},
  {:year =&amp;gt; 1999, :make =&amp;gt; 'Chevy', :model =&amp;gt; 'Venture &quot;Extended Edition&quot;', :description =&amp;gt; nil, :price =&amp;gt; 4900.00},
  {:year =&amp;gt; 1999, :make =&amp;gt; 'Chevy', :model =&amp;gt; 'Venture &quot;Extended Edition, Very Large&quot;', :description =&amp;gt; nil, :price =&amp;gt; 5000.00},
  {:year =&amp;gt; 1996, :make =&amp;gt; 'Jeep', :model =&amp;gt; 'Grand Cherokee', :description =&amp;gt; &quot;MUST SELL!\nair, moon roof, loaded&quot;, :price =&amp;gt; 4799.00}
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It might be tempting to just use regular expressions or read each line and &lt;code class=&quot;highlighter-rouge&quot;&gt;split(',')&lt;/code&gt;, but there are many nuances to the CSV format. Ruby’s stdlib &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html&quot;&gt;includes CSV support&lt;/a&gt; to help us realize this dream with minimal hassle.&lt;/p&gt;

&lt;p&gt;The documentation is unclear on the differences between methods of opening a CSV, nor does it help realize this Ruby data structure easily. We’ll be walking through the discovery of this from the documentation, but if you want the final solution jump down to the last code snippet.&lt;/p&gt;

&lt;p&gt;A closer look at &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#method-c-new&quot;&gt;#new&lt;/a&gt; shows that it can take either a String or an IO-like object. The latter is interesting if you’ve gotten uploaded data, like with &lt;a href=&quot;http://api.rubyonrails.org/classes/ActionDispatch/Http/UploadedFile.html&quot;&gt;ActionPack’s UploadedData&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;csv = CSV.new(body)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this csv loaded, you can use &lt;code class=&quot;highlighter-rouge&quot;&gt;to_a&lt;/code&gt; to get the array of data on it. This is a convenience method for &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#method-i-read&quot;&gt;#read&lt;/a&gt; and &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#method-i-readlines&quot;&gt;#readlines&lt;/a&gt;. This reads the remaining data in the string or IO you passed in, so any subsequent calls ends up returning an empty array.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;csv.to_a
# =&amp;gt; [[&quot;Year&quot;, &quot;Make&quot;, &quot;Model&quot;, &quot;Description&quot;, &quot;Price&quot;], [&quot;1997&quot;, &quot;Ford&quot;, &quot;E350&quot;, &quot;ac, abs, moon&quot;, &quot;3000.00&quot;], [&quot;1999&quot;, &quot;Chevy&quot;, &quot;Venture \&quot;Extended Edition\&quot;&quot;, &quot;&quot;, &quot;4900.00&quot;], [&quot;1999&quot;, &quot;Chevy&quot;, &quot;Venture \&quot;Extended Edition, Very Large\&quot;&quot;, nil, &quot;5000.00&quot;], [&quot;1996&quot;, &quot;Jeep&quot;, &quot;Grand Cherokee&quot;, &quot;MUST SELL!\nair, moon roof, loaded&quot;, &quot;4799.00&quot;]]
csv.to_a
# =&amp;gt; []
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This gives us an array of arrays, and the first element is an array with the headers. We are further than we started, but we still don’t have an array of hashes.&lt;/p&gt;

&lt;p&gt;One of new &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#method-i-readlines&quot;&gt;#new&lt;/a&gt;’s options is &lt;code class=&quot;highlighter-rouge&quot;&gt;:headers&lt;/code&gt;, which basically does just that:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;csv.to_a
#=&amp;gt; [#&amp;lt;CSV::Row &quot;Year&quot;:&quot;1997&quot; &quot;Make&quot;:&quot;Ford&quot; &quot;Model&quot;:&quot;E350&quot; &quot;Description&quot;:&quot;ac, abs, moon&quot; &quot;Price&quot;:&quot;3000.00&quot;&amp;gt;, #&amp;lt;CSV::Row &quot;Year&quot;:&quot;1999&quot; &quot;Make&quot;:&quot;Chevy&quot; &quot;Model&quot;:&quot;Venture \&quot;Extended Edition\&quot;&quot; &quot;Description&quot;:&quot;&quot; &quot;Price&quot;:&quot;4900.00&quot;&amp;gt;, #&amp;lt;CSV::Row &quot;Year&quot;:&quot;1999&quot; &quot;Make&quot;:&quot;Chevy&quot; &quot;Model&quot;:&quot;Venture \&quot;Extended Edition, Very Large\&quot;&quot; &quot;Description&quot;:nil &quot;Price&quot;:&quot;5000.00&quot;&amp;gt;, #&amp;lt;CSV::Row &quot;Year&quot;:&quot;1996&quot; &quot;Make&quot;:&quot;Jeep&quot; &quot;Model&quot;:&quot;Grand Cherokee&quot; &quot;Description&quot;:&quot;MUST SELL!\nair, moon roof, loaded&quot; &quot;Price&quot;:&quot;4799.00&quot;&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Actually, this looks like an array of &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV/Row.html&quot;&gt;CSV::Row&lt;/a&gt;s. It has a &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV/Row.html#method-i-to_hash&quot;&gt;#to_hash&lt;/a&gt;. We can use &lt;code class=&quot;highlighter-rouge&quot;&gt;map&lt;/code&gt; to apply that to each element in the array:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;csv.to_a.map {|row| row.to_hash }
# =&amp;gt; [{&quot;Year&quot;=&amp;gt;&quot;1997&quot;, &quot;Make&quot;=&amp;gt;&quot;Ford&quot;, &quot;Model&quot;=&amp;gt;&quot;E350&quot;, &quot;Description&quot;=&amp;gt;&quot;ac, abs, moon&quot;, &quot;Price&quot;=&amp;gt;&quot;3000.00&quot;}, {&quot;Year&quot;=&amp;gt;&quot;1999&quot;, &quot;Make&quot;=&amp;gt;&quot;Chevy&quot;, &quot;Model&quot;=&amp;gt;&quot;Venture \&quot;Extended Edition\&quot;&quot;, &quot;Description&quot;=&amp;gt;&quot;&quot;, &quot;Price&quot;=&amp;gt;&quot;4900.00&quot;}, {&quot;Year&quot;=&amp;gt;&quot;1999&quot;, &quot;Make&quot;=&amp;gt;&quot;Chevy&quot;, &quot;Model&quot;=&amp;gt;&quot;Venture \&quot;Extended Edition, Very Large\&quot;&quot;, &quot;Description&quot;=&amp;gt;nil, &quot;Price&quot;=&amp;gt;&quot;5000.00&quot;}, {&quot;Year&quot;=&amp;gt;&quot;1996&quot;, &quot;Make&quot;=&amp;gt;&quot;Jeep&quot;, &quot;Model&quot;=&amp;gt;&quot;Grand Cherokee&quot;, &quot;Description&quot;=&amp;gt;&quot;MUST SELL!\nair, moon roof, loaded&quot;, &quot;Price&quot;=&amp;gt;&quot;4799.00&quot;}]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We are getting warmer! It’s an array of hashes, but the keys are literally the values from the header row as Strings. Another peak back at &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#method-c-new&quot;&gt;#new&lt;/a&gt; shows a &lt;code class=&quot;highlighter-rouge&quot;&gt;:header_converters&lt;/code&gt; for converting the headers from their raw values. It’s not easy to find on the page, but &lt;a href=&quot;http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#HeaderConverters&quot;&gt;here is a list of all header converters&lt;/a&gt;, with &lt;code class=&quot;highlighter-rouge&quot;&gt;:symbol&lt;/code&gt; being the one we care about.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;csv = CSV.new(body, :headers =&amp;gt; true, :header_converters =&amp;gt; :symbol)
csv.to_a.map {|row| row.to_hash }
# =&amp;gt; [{:year=&amp;gt;&quot;1997&quot;, :make=&amp;gt;&quot;Ford&quot;, :model=&amp;gt;&quot;E350&quot;, :description=&amp;gt;&quot;ac, abs, moon&quot;, :price=&amp;gt;&quot;3000.00&quot;}, {:year=&amp;gt;&quot;1999&quot;, :make=&amp;gt;&quot;Chevy&quot;, :model=&amp;gt;&quot;Venture \&quot;Extended Edition\&quot;&quot;, :description=&amp;gt;&quot;&quot;, :price=&amp;gt;&quot;4900.00&quot;}, {:year=&amp;gt;&quot;1999&quot;, :make=&amp;gt;&quot;Chevy&quot;, :model=&amp;gt;&quot;Venture \&quot;Extended Edition, Very Large\&quot;&quot;, :description=&amp;gt;nil, :price=&amp;gt;&quot;5000.00&quot;}, {:year=&amp;gt;&quot;1996&quot;, :make=&amp;gt;&quot;Jeep&quot;, :model=&amp;gt;&quot;Grand Cherokee&quot;, :description=&amp;gt;&quot;MUST SELL!\nair, moon roof, loaded&quot;, :price=&amp;gt;&quot;4799.00&quot;}]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Even warmer! The hash keys are now symoblized, but we still have some wonky data in there. There’s blank strings (“”), and numerics as strings (“1999”, “4900.00”). Yet another option to &lt;a href=&quot;http://ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#method-c-new&quot;&gt;#new&lt;/a&gt; is &lt;code class=&quot;highlighter-rouge&quot;&gt;:converts&lt;/code&gt;, to convert each row’s values. &lt;a href=&quot;http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html#Converters&quot;&gt;Here is a list of available converts&lt;/a&gt;, but we want &lt;code class=&quot;highlighter-rouge&quot;&gt;:all&lt;/code&gt; which converts numerics (float and intergers) and date &amp;amp; datetimes:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;csv = CSV.new(body, :headers =&amp;gt; true, :header_converters =&amp;gt; :symbol, :converters =&amp;gt; :all)
csv.to_a.map {|row| row.to_hash }
#=&amp;gt; [{:year=&amp;gt;1997, :make=&amp;gt;&quot;Ford&quot;, :model=&amp;gt;&quot;E350&quot;, :description=&amp;gt;&quot;ac, abs, moon&quot;, :price=&amp;gt;3000.0}, {:year=&amp;gt;1999, :make=&amp;gt;&quot;Chevy&quot;, :model=&amp;gt;&quot;Venture \&quot;Extended Edition\&quot;&quot;, :description=&amp;gt;&quot;&quot;, :price=&amp;gt;4900.0}, {:year=&amp;gt;1999, :make=&amp;gt;&quot;Chevy&quot;, :model=&amp;gt;&quot;Venture \&quot;Extended Edition, Very Large\&quot;&quot;, :description=&amp;gt;nil, :price=&amp;gt;5000.0}, {:year=&amp;gt;1996, :make=&amp;gt;&quot;Jeep&quot;, :model=&amp;gt;&quot;Grand Cherokee&quot;, :description=&amp;gt;&quot;MUST SELL!\nair, moon roof, loaded&quot;, :price=&amp;gt;4799.0}]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;SO WARM! The only thing I can complain about is that there’s blank strings (“”). While there’s nothing else built in to help us, there is support for adding your own custom converters. You can add your own converts to &lt;code class=&quot;highlighter-rouge&quot;&gt;CSV::Converters&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;CSV::HeaderConverters&lt;/code&gt; as you want. These are just hashes, with the key being the name, and the value being a lambda that takes  a string and should return the converted value. You can then pass an array of converters to use to &lt;code class=&quot;highlighter-rouge&quot;&gt;#new&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CSV::Converters[:blank_to_nil] = lambda do |field|
  field &amp;amp;&amp;amp; field.empty? ? nil : field
end
csv = CSV.new(body, :headers =&amp;gt; true, :header_converters =&amp;gt; :symbol, :converters =&amp;gt; [:all, :blank_to_nil])
csv.to_a.map {|row| row.to_hash }
# =&amp;gt; [{:year=&amp;gt;1997, :make=&amp;gt;&quot;Ford&quot;, :model=&amp;gt;&quot;E350&quot;, :description=&amp;gt;&quot;ac, abs, moon&quot;, :price=&amp;gt;3000.0}, {:year=&amp;gt;1999, :make=&amp;gt;&quot;Chevy&quot;, :model=&amp;gt;&quot;Venture \&quot;Extended Edition\&quot;&quot;, :description=&amp;gt;nil, :price=&amp;gt;4900.0}, {:year=&amp;gt;1999, :make=&amp;gt;&quot;Chevy&quot;, :model=&amp;gt;&quot;Venture \&quot;Extended Edition, Very Large\&quot;&quot;, :description=&amp;gt;nil, :price=&amp;gt;5000.0}, {:year=&amp;gt;1996, :make=&amp;gt;&quot;Jeep&quot;, :model=&amp;gt;&quot;Grand Cherokee&quot;, :description=&amp;gt;&quot;MUST SELL!\nair, moon roof, loaded&quot;, :price=&amp;gt;4799.0}]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;HOT HOT HOT. That is basically exactly what I set out to do, so we are all done here.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Rsync an iTunes Library like a boss</title>
		<link href="http://technicalpickles.com/posts/rsync-an-itunes-library-like-a-boss" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/rsync-an-itunes-library-like-a-boss</id>
		<updated>2011-08-02T00:00:00+00:00</updated>
		<content type="html">&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;

&lt;p&gt;Shiny new MacBook Air, and I wanted to do this install from scratch (no TimeMachine recovery), but still get the good stuff from my old laptop. Also, rsync is awesome.&lt;/p&gt;

&lt;h2 id=&quot;prepare-old-computer&quot;&gt;Prepare old computer&lt;/h2&gt;

&lt;p&gt;On old computer, from iTunes: &lt;code class=&quot;highlighter-rouge&quot;&gt;File -&amp;gt;  Library -&amp;gt; Organize Library&lt;/code&gt; and check &lt;code class=&quot;highlighter-rouge&quot;&gt;Consolidate files&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After this looks like it finished (would be displayed in status area, i.e. song progress), &lt;strong&gt;quit iTunes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;code class=&quot;highlighter-rouge&quot;&gt;System Preferences&lt;/code&gt;, go to &lt;code class=&quot;highlighter-rouge&quot;&gt;Sharing&lt;/code&gt; and make sure &lt;code class=&quot;highlighter-rouge&quot;&gt;Remote Login&lt;/code&gt; is enabled.&lt;/p&gt;

&lt;p&gt;Make note of this computer’s IP address. You can use &lt;code class=&quot;highlighter-rouge&quot;&gt;/sbin/ifconfig&lt;/code&gt; or the &lt;code class=&quot;highlighter-rouge&quot;&gt;Network&lt;/code&gt; preferences.&lt;/p&gt;

&lt;h2 id=&quot;prepare-new-computer&quot;&gt;Prepare new computer&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Quit iTunes.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Backup existing files, just in case:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~/Music
mv iTunes iTunes.bak
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;rsync-in-boss-like-fashion&quot;&gt;rsync in boss like fashion:&lt;/h2&gt;

&lt;p&gt;Remember that IP address from earlier? Let’s pretend it’s 192.168.1.4 for demonstration.&lt;/p&gt;

&lt;p&gt;On new computer:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~/Music
rsync -av --delete 192.168.1.4:Music/iTunes .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now wait, and have a glass of scotch.&lt;/p&gt;

&lt;h2 id=&quot;the-result&quot;&gt;The result&lt;/h2&gt;

&lt;p&gt;Open iTunes on your new computer, and you should have all these things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Music&lt;/li&gt;
  &lt;li&gt;Playlists&lt;/li&gt;
  &lt;li&gt;Song plays&lt;/li&gt;
  &lt;li&gt;Ratings&lt;/li&gt;
  &lt;li&gt;Videos&lt;/li&gt;
  &lt;li&gt;Syncability with iPhones/iPods&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;troubleshooting&quot;&gt;Troubleshooting&lt;/h2&gt;

&lt;p&gt;The first time I tried this, it didn’t recognize anything in the Library. Not sure if these matter, or if the second run was just luckier. In any event, these are things I tried, and are reflected above when it’s pretty likely to have helped:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Tried to add existing Library to itself. Cancelled partway through&lt;/li&gt;
  &lt;li&gt;On old computer, exported Library using &lt;code class=&quot;highlighter-rouge&quot;&gt;File -&amp;gt; Library -&amp;gt; Export Library&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Added –delete to rsync run&lt;/li&gt;
  &lt;li&gt;Made sure iTunes wasn’t running during rsync&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<title>Capybara (and Cucumber) and Domains</title>
		<link href="http://technicalpickles.com/posts/capybara-and-domains" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/capybara-and-domains</id>
		<updated>2011-04-07T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;While hacking on a new application, it took a good few weeks to find out how to, using &lt;a href=&quot;http://cukes.info/&quot;&gt;cucumber&lt;/a&gt; and &lt;a href=&quot;https://github.com/jnicklas/capybara&quot;&gt;Capybara&lt;/a&gt;, interact with domains that weren’t localhost. Consider this a note to my future self :)&lt;/p&gt;

&lt;p&gt;Let’s imagine your Rails application has a parimary domain and allows users to have subdomains (as if, granted by the power of &lt;a href=&quot;https://github.com/mbleigh/subdomain-fu&quot;&gt;subdomain-fu&lt;/a&gt;). By default, everything is hitting localhost, for example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/about&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; http://localhost/about&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To get around this, you can tell Capybara to use a different domain:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Capybara&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;default_host&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;awesome.com&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/about&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; http://awesome.com/about&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I found it useful to make a step to navigate to a particular domain:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;When&lt;/span&gt; &lt;span class=&quot;sr&quot;&gt;/^I visit (.*)$/&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;site_domain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;site_domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;localhost&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site_domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;the main domain&quot;&lt;/span&gt;

  &lt;span class=&quot;no&quot;&gt;Capybara&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;default_host&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site_domain&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Armed with this, we can write our features:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cucumber&quot; data-lang=&quot;cucumber&quot;&gt;&lt;span class=&quot;kn&quot;&gt;Scenario&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; Visit the main domain
  &lt;span class=&quot;nf&quot;&gt;When &lt;/span&gt;I visit the main domain
  &lt;span class=&quot;nf&quot;&gt;Then &lt;/span&gt;I should see information about the main domain

&lt;span class=&quot;kn&quot;&gt;Scenario&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; Visit another domain
  &lt;span class=&quot;nf&quot;&gt;When &lt;/span&gt;I visit awesome.com
  &lt;span class=&quot;nf&quot;&gt;Then &lt;/span&gt;I should see awesomeness&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</content>
	</entry>
	
	<entry>
		<title>Rails Permanent Redirects</title>
		<link href="http://technicalpickles.com/posts/rails-permanent-redirects" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/rails-permanent-redirects</id>
		<updated>2011-02-14T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;I feel silly writing something so short and simple, but horribly outdated information when googling ‘rails permanent redirect’ drove me to it. I’m just hoping the SEO gods smile upon me.&lt;/p&gt;

&lt;p&gt;So, my friends, check it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;redirect_to &quot;http://somewhere.com&quot;, :status =&amp;gt; 301
redirect_to posts_url, :status =&amp;gt; :moved_permanently
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For the curious, &lt;code class=&quot;highlighter-rouge&quot;&gt;:status&lt;/code&gt; “can either be a standard &lt;a href=&quot;http://www.iana.org/assignments/http-status-codes&quot;&gt;HTTP Status code&lt;/a&gt; as an integer, or a symbol representing the downcased, underscored and symbolized description” (&lt;a href=&quot;http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to&quot;&gt;source&lt;/a&gt;)&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Crosspost: Developing and testing system automation with capistrano-cowboy</title>
		<link href="http://technicalpickles.com/posts/crosspost-developing-and-testing-system-automation-with-capistrano-cowboy" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/crosspost-developing-and-testing-system-automation-with-capistrano-cowboy</id>
		<updated>2010-12-17T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;Just this morning, I posted an article about &lt;a href=&quot;http://blog.railsmachine.com/articles/2010/12/16/developing-and-testing-system-automation-with-capistrano-cowboy/&quot;&gt;developing and testing system automation with capistrano-cowboy&lt;/a&gt; over at the &lt;a href=&quot;http://blog.railsmachine.com/&quot;&gt;Rails Machine Blog&lt;/a&gt;. Check it out :)&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Jeweler 1.5.0</title>
		<link href="http://technicalpickles.com/posts/jeweler-1-5-0" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/jeweler-1-5-0</id>
		<updated>2010-11-13T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;It’s been awhile, dear reader. After a lull in development, Jeweler 1.5.1 is finally released. I spent a lot of time cleaning up and revising the README, so I’m just going to put that right here. It tells the story of going from 0 to jewelered, so I don’t think it’s a cop out :)&lt;/p&gt;

&lt;h1 id=&quot;jeweler-craft-the-perfect-rubygem&quot;&gt;Jeweler: Craft the perfect RubyGem&lt;/h1&gt;

&lt;p&gt;Jeweler provides the noble ruby developer with two primary features:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a library for managing and releasing RubyGem projects&lt;/li&gt;
  &lt;li&gt;a scaffold generator for starting new RubyGem projects&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;hello-world&quot;&gt;Hello, world&lt;/h2&gt;

&lt;p&gt;Use RubyGems to install the heck out of jeweler to get started:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ gem install jeweler
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With jeweler installed, you can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;jeweler&lt;/code&gt; command to generate a new project. For the most basic use, just give it a name:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ jeweler hello-gem
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This requires some Git configuration (like name, email, GitHub account, etc), but &lt;code class=&quot;highlighter-rouge&quot;&gt;jeweler&lt;/code&gt; will prompt along the way.&lt;/p&gt;

&lt;p&gt;Your new &lt;code class=&quot;highlighter-rouge&quot;&gt;hello-gem&lt;/code&gt; gem is ready in the &lt;code class=&quot;highlighter-rouge&quot;&gt;hello-gem&lt;/code&gt; directory. Take a peek, and you’ll see several files and directories&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Rakefile&lt;/code&gt; setup for jeweler, running tests, generating documentation, and releasing to &lt;a href=&quot;http://rubygems.org/&quot;&gt;rubygems.org&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;README.rdoc&lt;/code&gt; with contribution guidelines and copyright info crediting you&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;LICENSE&lt;/code&gt; with the MIT licensed crediting you&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Gemfile&lt;/code&gt; with development dependencies filled in&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;lib/hello-gem.rb&lt;/code&gt; waiting for you to code&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;test/&lt;/code&gt; containing a (failing) shoulda test suite &lt;a href=&quot;http://github.com/thoughtbot/shoulda&quot;&gt;shoulda&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;more-jeweler-options&quot;&gt;More &lt;code class=&quot;highlighter-rouge&quot;&gt;jeweler&lt;/code&gt; options&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;jeweler&lt;/code&gt; command supports a lot of options. Mostly, they are for generating baked in support for this test framework, or that.&lt;/p&gt;

&lt;p&gt;Check out &lt;code class=&quot;highlighter-rouge&quot;&gt;jeweler --help&lt;/code&gt; for the most up to date options.&lt;/p&gt;

&lt;h2 id=&quot;hello-rake-tasks&quot;&gt;Hello, rake tasks&lt;/h2&gt;

&lt;p&gt;Beyond just editing source code, you’ll be interacting with your gem using &lt;code class=&quot;highlighter-rouge&quot;&gt;rake&lt;/code&gt; a lot. To see all the tasks available with a brief description, you can run:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake -T
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You’ll need a version before you can start installing your gem locally. The easiest way is with the &lt;code class=&quot;highlighter-rouge&quot;&gt;version:write&lt;/code&gt; Rake task. Let’s imagine you start with 0.1.0&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake version:write MAJOR=0 MINOR=1 PATCH=0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can now go forth and develop, now that there’s an initial version defined. Eventually, you should install and test the gem:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;install&lt;/code&gt; rake task builds the gem and &lt;code class=&quot;highlighter-rouge&quot;&gt;gem install&lt;/code&gt;s it. You’re all set if you’re using &lt;a href=&quot;http://rvm.beginrescueend.com/&quot;&gt;RVM&lt;/a&gt;, but you may need to run it with sudo if you have a system-installed ruby:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo rake install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;releasing&quot;&gt;Releasing&lt;/h3&gt;

&lt;p&gt;At last, it’s time to &lt;a href=&quot;http://img.skitch.com/20100310-nrgxbwqm58tibiq2un6mujqmm5.png&quot;&gt;ship it&lt;/a&gt;! Make sure you have everything committed and pushed, then go wild:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will automatically:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Generate &lt;code class=&quot;highlighter-rouge&quot;&gt;hello-gem.gemspec&lt;/code&gt; and commit it&lt;/li&gt;
  &lt;li&gt;Use &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; to tag &lt;code class=&quot;highlighter-rouge&quot;&gt;v0.1.0&lt;/code&gt; and push it&lt;/li&gt;
  &lt;li&gt;Build &lt;code class=&quot;highlighter-rouge&quot;&gt;hello-gem-0.1.0.gem&lt;/code&gt; and push it to &lt;a href=&quot;http://rubygems.org/gems/&quot;&gt;rubygems.org&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;version-bumping&quot;&gt;Version bumping&lt;/h3&gt;

&lt;p&gt;It feels good to release code. Do it, do it often. But before that, bump the version. Then release it. There’s a few ways to update the version:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# version:write like before
$ rake version:write MAJOR=0 MINOR=3 PATCH=0

# bump just major, ie 0.1.0 -&amp;gt; 1.0.0
$ rake version:bump:major

# bump just minor, ie 0.1.0 -&amp;gt; 0.2.0
$ rake version:bump:minor

# bump just patch, ie 0.1.0 -&amp;gt; 0.1.1
$ rake version:bump:patch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then it’s the same &lt;code class=&quot;highlighter-rouge&quot;&gt;release&lt;/code&gt; we used before:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;customizing-your-gem&quot;&gt;Customizing your gem&lt;/h2&gt;

&lt;p&gt;If you’ve been following along so far, your gem is just a blank slate. You’re going to need to make it colorful and full of metadata.&lt;/p&gt;

&lt;p&gt;You can customize your gem by updating your &lt;code class=&quot;highlighter-rouge&quot;&gt;Rakefile&lt;/code&gt;. With a newly generated project, it will look something like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;require 'jeweler'
Jeweler::Tasks.new do |gem|
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
  gem.name = &quot;whatwhatwhat&quot;
  gem.summary = %Q{TODO: one-line summary of your gem}
  gem.description = %Q{TODO: longer description of your gem}
  gem.email = &quot;josh@technicalpickles.com&quot;
  gem.homepage = &quot;http://github.com/technicalpickles/whatwhatwhat&quot;
  gem.authors = [&quot;Joshua Nichols&quot;]
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’s crucial to understand the &lt;code class=&quot;highlighter-rouge&quot;&gt;gem&lt;/code&gt; object is just a Gem::Specification. You can read up about it at &lt;a href=&quot;http://docs.rubygems.org/read/chapter/20&quot;&gt;docs.rubygems.org/read/chapter/20&lt;/a&gt;. This is the most basic way of specifying a gem, Jeweler-managed or not. Jeweler just exposes this to you, in addition to providing some reasonable defaults, which we’ll explore now.&lt;/p&gt;

&lt;h3 id=&quot;project-information&quot;&gt;Project information&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.name = &quot;whatwhatwhat&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Every gem has a name. Among other things, the gem name is how you are able to &lt;code class=&quot;highlighter-rouge&quot;&gt;gem install&lt;/code&gt; it. &lt;a href=&quot;http://docs.rubygems.org/read/chapter/20#name&quot;&gt;Reference&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.summary = %Q{TODO: longer description of your gem}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is a one line summary of your gem. This is displayed, for example, when you use &lt;code class=&quot;highlighter-rouge&quot;&gt;gem list --details&lt;/code&gt; or view it on &lt;a href=&quot;http://rubygems.org/gems/&quot;&gt;rubygems.org&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.description = %Q{TODO: longer description of your gem}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Description is a longer description. Scholars ascertain that knowledge of where the description is used was lost centuries ago.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.email = &quot;josh@technicalpickles.com&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This should be a way to get a hold of you regarding the gem.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.homepage = &quot;http://github.com/technicalpickles/whatwhatwhat&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The homepage should have more information about your gem. The jeweler generator guesses this based on the assumption your code lives on &lt;a href=&quot;http://github.com/&quot;&gt;GitHub&lt;/a&gt;, using your Git configuration to find your GitHub username. This is displayed by &lt;code class=&quot;highlighter-rouge&quot;&gt;gem list --details&lt;/code&gt; and on rubygems.org.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.authors = [&quot;Joshua Nichols&quot;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hey, this is you, the author (or me in this case). The &lt;code class=&quot;highlighter-rouge&quot;&gt;jeweler&lt;/code&gt; generator also guesses this from your Git configuration. This is displayed by &lt;code class=&quot;highlighter-rouge&quot;&gt;gem list --details&lt;/code&gt; and on rubygems.org.&lt;/p&gt;

&lt;h3 id=&quot;files&quot;&gt;Files&lt;/h3&gt;

&lt;p&gt;The quickest way to add more files is to &lt;code class=&quot;highlighter-rouge&quot;&gt;git add&lt;/code&gt; them. Jeweler uses your Git repository to populate your gem’s files by including added and committed and excluding &lt;code class=&quot;highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt;d. In most cases, this is reasonable enough.&lt;/p&gt;

&lt;p&gt;If you need to tweak the files, that’s cool. Jeweler populates &lt;code class=&quot;highlighter-rouge&quot;&gt;gem.files&lt;/code&gt; as a &lt;code class=&quot;highlighter-rouge&quot;&gt;Rake::FileList&lt;/code&gt;. It’s like a normal array, except you can &lt;code class=&quot;highlighter-rouge&quot;&gt;include&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;exclude&lt;/code&gt; file globs:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.files.exclude 'tmp' # exclude temporary directory
gem.files.include 'lib/foo/bar.rb' # explicitly include lib/foo/bar.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If that’s not enough, you can just set &lt;code class=&quot;highlighter-rouge&quot;&gt;gem.files&lt;/code&gt; outright&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.files = Dir.glob('lib/**/*.rb')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;dependencies&quot;&gt;Dependencies&lt;/h3&gt;

&lt;p&gt;Dependencies let you define other gems that your gem needs to function. &lt;code class=&quot;highlighter-rouge&quot;&gt;gem install your-gem&lt;/code&gt; will install your-gem’s dependencies along with it, and when you use your-gem in an application, the dependencies will be made available. Use &lt;code class=&quot;highlighter-rouge&quot;&gt;gem.add_dependency&lt;/code&gt; to register them. &lt;a href=&quot;http://docs.rubygems.org/read/chapter/20#dependencies&quot;&gt;Reference&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.add_dependency 'nokogiri'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will ensure a version of &lt;code class=&quot;highlighter-rouge&quot;&gt;nokogiri&lt;/code&gt; is installed, but it doesn’t require anything more than that. You can provide extra args to be more specific:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.add_dependency 'nokogiri', '= 1.2.1' # exactly version 1.2.1
gem.add_dependency 'nokogiri', '&amp;gt;= 1.2.1' # greater than or equal to 1.2.1, ie, 1.2.1, 1.2.2, 1.3.0, 2.0.0, etc
gem.add_dependency 'nokogiri', '&amp;gt;= 1.2.1', '&amp;lt; 1.3.0' # greater than or equal to 1.2.1, but less than 1.3.0
gem.add_dependency 'nokogiri', '~&amp;gt; 1.2.1' # same thing, but more concise
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When specifying which version is required, there’s a bit of the condunrum. You want to allow the most versions possible, but you want to be sure they are compatible. Using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;= 1.2.1&lt;/code&gt; is fine most of the time, except until the point that 2.0.0 comes out and totally breaks backwards the API. That’s when it’s good to use &lt;code class=&quot;highlighter-rouge&quot;&gt;~&amp;gt; 1.2.1&lt;/code&gt;, which requires any version in the &lt;code class=&quot;highlighter-rouge&quot;&gt;1.2&lt;/code&gt; family, starting with &lt;code class=&quot;highlighter-rouge&quot;&gt;1.2.1&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;executables&quot;&gt;Executables&lt;/h3&gt;

&lt;p&gt;Executables let your gem install shell commands. Just put any executable scripts in the &lt;code class=&quot;highlighter-rouge&quot;&gt;bin/&lt;/code&gt; directory, make sure they are added using &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt;, and Jeweler will take care of the rest.&lt;/p&gt;

&lt;p&gt;When you need more finely grained control over it, you can set it yourself:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.executables = ['foo'] # note, it's the file name relative to `bin/`, not the project root
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;versioning&quot;&gt;Versioning&lt;/h3&gt;

&lt;p&gt;We discussed earlier how to bump the version. The rake tasks are really just convience methods for manipulating the &lt;code class=&quot;highlighter-rouge&quot;&gt;VERSION&lt;/code&gt; file. It just contains a version string, like &lt;code class=&quot;highlighter-rouge&quot;&gt;1.2.3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;VERSION&lt;/code&gt; is a convention used by Jeweler, and is used to populate &lt;code class=&quot;highlighter-rouge&quot;&gt;gem.version&lt;/code&gt;. You can actually set this yourself, and Jeweler won’t try to override it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem.version = '1.2.3'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A common pattern is to have this in a version constant in your library. This is convenient, because users of the library can query the version they are using at runtime.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# in lib/foo/version.rb
class Foo
  module Version
    MAJOR = 1
    MINOR = 2
    PATCH = 3
    BUILD = 'pre3'

    STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
  end
end

# in Rakefile
require 'jeweler'
require './lib/foo/version.rb'
Jeweler::Tasks.new do |gem|
  # snip
  gem.version = Foo::Version::STRING
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;rake-tasks&quot;&gt;Rake tasks&lt;/h3&gt;

&lt;p&gt;Jeweler lives inside of Rake. As a result, they are dear friends. But, that friendship doesn’t interfere with typical Rake operations.&lt;/p&gt;

&lt;p&gt;That means you can define your own namespaces, tasks, or use third party Rake libraries without cause for concern.&lt;/p&gt;

&lt;h2 id=&quot;contributing-to-jeweler&quot;&gt;Contributing to Jeweler&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet&lt;/li&gt;
  &lt;li&gt;Check out the &lt;a href=&quot;http://github.com/technicalpickles/jeweler/issues&quot;&gt;issue tracker&lt;/a&gt; to make sure someone already hasn’t requested it and/or contributed it&lt;/li&gt;
  &lt;li&gt;Fork the project&lt;/li&gt;
  &lt;li&gt;Start a feature/bugfix branch&lt;/li&gt;
  &lt;li&gt;Commit and push until you are happy with your contribution&lt;/li&gt;
  &lt;li&gt;Make sure to add tests for the feature/bugfix. This is important so I don’t break it in a future version unintentionally.&lt;/li&gt;
  &lt;li&gt;Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate it to its own commit so I can cherry-pick around it.&lt;/li&gt;
&lt;/ul&gt;

</content>
	</entry>
	
	<entry>
		<title>Never Leave Your Dotfiles Behind Again With Homesick</title>
		<link href="http://technicalpickles.com/posts/never-leave-your-dotfiles-behind-again-with-homesick" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/never-leave-your-dotfiles-behind-again-with-homesick</id>
		<updated>2010-03-31T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;I was on a server the other day, and was getting a little weepy. I missed my dotfiles. The thought of doing this manually, especially on multiple servers, was even worse than the homesickness I had. Basically, I wanted:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A convention for laying out dotfiles repos&lt;/li&gt;
  &lt;li&gt;A command to clone my dotfiles from git&lt;/li&gt;
  &lt;li&gt;A command to symlink the files into my home directory&lt;/li&gt;
  &lt;li&gt;A delicious scotch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lots of folks, myself included, have started sharing their configuration files, ie &lt;code class=&quot;highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;.screenrc&lt;/code&gt;, on &lt;a href=&quot;http://github.com&quot;&gt;GitHub&lt;/a&gt; and elsewhere. Sometimes though, I think they are really more for personal backup and maybe to just share tips and tricks, rather than share them for consumption by others.&lt;/p&gt;

&lt;p&gt;Why would I say such a thing? Well, let’s say you found someone that has put together the ultimate set vim configuration. How would you go about using them?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git clone whatever&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Peek into the repo, try to figure out how it’s laid out and where the config files are&lt;/li&gt;
  &lt;li&gt;Are the files at the top level of the repo?&lt;/li&gt;
  &lt;li&gt;Do they include the dot in the file name, ie &lt;code class=&quot;highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt;, or exclude it so it’s easier to see when doing &lt;code class=&quot;highlighter-rouge&quot;&gt;ls&lt;/code&gt;, ie &lt;code class=&quot;highlighter-rouge&quot;&gt;vimrc&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Symlink the files into your home directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After deliberating and coding for an evening, I had something to show for my troubles: &lt;a href=&quot;http://github.com/technicalpickles/homesick&quot;&gt;homesick&lt;/a&gt;. Basically, it provides what I just described, less the scotch.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ gem install homesick
$ homesick clone git://github.com/technicalpickles/pickled-vim.git
   git clone  git://github.com/technicalpickles/pickled-vim.git to /Users/technicalpickles/.homesick/repos/pickled-vim
$ homesick list
 pickled-vim  git://github.com/technicalpickles/pickled-vim.git
$ homesick symlink pickled-vim
     symlink  /Users/technicalpickles/.homesick/repos/pickled-vim/home/.gvimrc to /Users/technicalpickles/.gvimrc
     symlink  /Users/technicalpickles/.homesick/repos/pickled-vim/home/.vim to /Users/technicalpickles/.vim
     symlink  /Users/technicalpickles/.homesick/repos/pickled-vim/home/.vimrc to /Users/technicalpickles/.vimrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;homesick assumes a few things about the repos it clones and how to symlink files from them:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It’s a git repo (duh)&lt;/li&gt;
  &lt;li&gt;It contains a &lt;code class=&quot;highlighter-rouge&quot;&gt;home&lt;/code&gt; directory&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;home&lt;/code&gt; contain any number of dotfiles, including the &lt;code class=&quot;highlighter-rouge&quot;&gt;.&lt;/code&gt; prefix&lt;/li&gt;
  &lt;li&gt;Any file in &lt;code class=&quot;highlighter-rouge&quot;&gt;home&lt;/code&gt; will be symlinked into the current user’s home directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;homesick uses &lt;a href=&quot;http://github.com/wycats/thor&quot;&gt;thor&lt;/a&gt; under the hood, which provides a number of advantages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Pretty, pretty colors&lt;/li&gt;
  &lt;li&gt;Simple parsing of commandline arguments&lt;/li&gt;
  &lt;li&gt;Helper methods for creating files, directories, etc&lt;/li&gt;
  &lt;li&gt;Builtin support for running the command without output, or only pretending to run the commands, or forcing the commands no matter what the cost&lt;/li&gt;
  &lt;li&gt;Ease to add new ‘commands’, ie &lt;code class=&quot;highlighter-rouge&quot;&gt;symlink&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;clone&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Conflict resolution when files already exist&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, I’m pretty happy where homesick is right now. It does what I wanted it to do, and looks pretty while doing so, so I can’t really complain.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>A Pattern For Using Standard In And Out In Your Ruby Code</title>
		<link href="http://technicalpickles.com/posts/a-pattern-for-using-standard-in-and-out-in-your-ruby-code" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/a-pattern-for-using-standard-in-and-out-in-your-ruby-code</id>
		<updated>2009-12-13T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;If you think about it, programming is really about taking some input from a user, and displaying some output back to them. When we’re down at the command line, this usually means reading from the standard input and writing to the standard output.&lt;/p&gt;

&lt;p&gt;In ruby, you’ll see code that interacts with standard in and standard out like so:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyApp&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;prompt_user&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;gets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chomp&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; 'ponies'&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;User selected: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Displays User selected: ponies&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Sadly though, when it comes testing, usually the input/output stuff is glossed over. Why though?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Extraneous output is often generated during testing&lt;/li&gt;
  &lt;li&gt;It’s hard to specify what stdin returns and verify what stdout displays back to the user&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;“Don’t let not having the tools be your trepidation… I will never make a criticism if I don’t have a resolution.” - &lt;a href=&quot;http://www.youtube.com/watch?v=4YBxeDN4tbk&quot;&gt;Joel Bauer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Allow me, then, to outline a pattern for using standard input and standard output in your Ruby code in a way that’s testable.&lt;/p&gt;

&lt;p&gt;First off, don’t use just ‘puts’ and ‘gets’ by themselves. If you check out the &lt;a href=&quot;http://ruby-doc.org/core/classes/Kernel.html#M005954&quot;&gt;rdoc for &lt;code class=&quot;highlighter-rouge&quot;&gt;Kernel&lt;/code&gt;&lt;/a&gt;, you’ll see that these are just shorthand for &lt;code class=&quot;highlighter-rouge&quot;&gt;$stdout.puts&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;$stdin.gets&lt;/code&gt;. So we’ll change our code to be more like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyApp&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;prompt_user&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;gets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chomp&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; 'ponies'&lt;/span&gt;
    &lt;span class=&quot;vg&quot;&gt;$stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;User selected: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Displays User selected: ponies&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Tests still pass, right? Alright, next we’re going to create some &lt;code class=&quot;highlighter-rouge&quot;&gt;attr_accessor&lt;/code&gt;s on &lt;code class=&quot;highlighter-rouge&quot;&gt;MyApp&lt;/code&gt; for the input/output. By default, we’ll make them point at &lt;code class=&quot;highlighter-rouge&quot;&gt;$stdin&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;$stdout&lt;/code&gt; so they’ll work as expected by default, but during your tests… well, we’ll get to that in a minute.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyApp&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:output&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;input&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$stdout&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;prompt_user&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;gets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chomp&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; 'ponies'&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;User selected: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Displays User selected: ponies&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user_selection&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we’re setup to test this proper like.&lt;/p&gt;

&lt;p&gt;But first, we need a little background on &lt;code class=&quot;highlighter-rouge&quot;&gt;$stdin&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;$stdout&lt;/code&gt;. If you pop up an &lt;code class=&quot;highlighter-rouge&quot;&gt;irb&lt;/code&gt; session and enter &lt;code class=&quot;highlighter-rouge&quot;&gt;$stdout.class&lt;/code&gt;, you’ll see its is &lt;a href=&quot;http://ruby-doc.org/core/classes/IO.html&quot;&gt;IO&lt;/a&gt;. I’m not going to go into much more detail, but suffice to say, &lt;a href=&quot;http://ruby-doc.org/core/classes/StringIO.html&quot;&gt;StringIO&lt;/a&gt; exists as a way to make an &lt;code class=&quot;highlighter-rouge&quot;&gt;IO&lt;/code&gt; that you can muck around with more easily for testing. To provide input to &lt;code class=&quot;highlighter-rouge&quot;&gt;StringIO&lt;/code&gt;, you pass a string to its constructor. To check the output to &lt;code class=&quot;highlighter-rouge&quot;&gt;StringIO&lt;/code&gt;, use the &lt;code class=&quot;highlighter-rouge&quot;&gt;string&lt;/code&gt; method to return it. Knowing that, we can test it now:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;describe&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;MyApp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;prompt_user&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;before&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@my_app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;MyApp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;

    &lt;span class=&quot;vi&quot;&gt;@input&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;StringIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ponies&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;StringIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;

    &lt;span class=&quot;vi&quot;&gt;@my_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@input&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@my_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@output&lt;/span&gt;

    &lt;span class=&quot;vi&quot;&gt;@user_selection&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@my_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;prompt_user&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;should output the user's input&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=~&lt;/span&gt; &lt;span class=&quot;sr&quot;&gt;/ponies/&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;should return the user's selection&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@user_selection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ponies&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And there we go: ruby code using stdin and stdout that we’re able to test easily.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Managing binaries of your gem with Jeweler</title>
		<link href="http://technicalpickles.com/posts/managing-binaries-with-jeweler" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/managing-binaries-with-jeweler</id>
		<updated>2009-11-18T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;It’s pretty useful to inclue a binary with your gems. Maybe it generates something. Or perhaps it’s what your gem is all about. Regardless, adding a binary to your gem is almost effortless.&lt;/p&gt;

&lt;p&gt;Basically, it plays out like this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Create a &lt;code class=&quot;highlighter-rouge&quot;&gt;bin&lt;/code&gt; directory&lt;/li&gt;
  &lt;li&gt;Make a ruby script in the &lt;code class=&quot;highlighter-rouge&quot;&gt;bin&lt;/code&gt; directory (I’ll provide an example in a second)&lt;/li&gt;
  &lt;li&gt;Make the ruby script executable&lt;/li&gt;
  &lt;li&gt;Regenerate the gemspec with &lt;code class=&quot;highlighter-rouge&quot;&gt;rake gemspec&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git add&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;git commit&lt;/code&gt; the executable and gemspec&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that we don’t actually touch the &lt;code class=&quot;highlighter-rouge&quot;&gt;Rakefile&lt;/code&gt; to update the gemspec &lt;code class=&quot;highlighter-rouge&quot;&gt;Jeweler::Tasks&lt;/code&gt;. Under the hood, Jeweler checks the &lt;code class=&quot;highlighter-rouge&quot;&gt;bin&lt;/code&gt; directory and populates the &lt;a href=&quot;http://docs.rubygems.org/read/chapter/20#executables&quot;&gt;executables&lt;/a&gt; section of the gemspec accordingly.&lt;/p&gt;

&lt;h2 id=&quot;an-example-binary&quot;&gt;An example binary&lt;/h2&gt;

&lt;p&gt;Let’s use the &lt;code class=&quot;highlighter-rouge&quot;&gt;jeweler&lt;/code&gt; binary as a template. A few goals I have are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It should be runnable regardless of where ruby is on the path&lt;/li&gt;
  &lt;li&gt;It should be runnable from within the project directory, and NOT have to be installed&lt;/li&gt;
  &lt;li&gt;It should be a good unix citizen and return an appropriate exit code&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env ruby&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$LOAD_PATH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;unshift&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;__FILE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'..'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'lib'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'jeweler/generator'&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Jeweler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Generator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;run!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ARGV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;On line 1, we have a shebang. &lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/bin/env ruby&lt;/code&gt; basically finds any executable in the &lt;code class=&quot;highlighter-rouge&quot;&gt;PATH&lt;/code&gt;, and executes that.&lt;/p&gt;

&lt;p&gt;On line 2, we manipulate the the &lt;code class=&quot;highlighter-rouge&quot;&gt;LOAD_PATH&lt;/code&gt; so that the lib directory is searched first. This lets us just run &lt;code class=&quot;highlighter-rouge&quot;&gt;bin/jeweler&lt;/code&gt; and have it load this copy of jeweler, rather than using the installed rubygems&lt;/p&gt;

&lt;p&gt;On line 3, we require the code we’re going to be using.&lt;/p&gt;

&lt;p&gt;Lastly, on line 4, we call the the &lt;code class=&quot;highlighter-rouge&quot;&gt;run!&lt;/code&gt; method. This takes the arguments passed to the binary, does stuff, and eventually returns an integer. &lt;code class=&quot;highlighter-rouge&quot;&gt;exit&lt;/code&gt; then, uh, exists, with this as the exit code.&lt;/p&gt;

&lt;p&gt;I like this particular approach because it’s a lot easier to test a method on class returning something, than it is to stub out &lt;code class=&quot;highlighter-rouge&quot;&gt;exit&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;straying-from-the-defaults&quot;&gt;Straying from the defaults&lt;/h2&gt;

&lt;p&gt;Maybe you don’t like there automatically being stuff. Perhaps there’s stuff that you that’s only for development. Or something super secret. The trick here is to modify &lt;code class=&quot;highlighter-rouge&quot;&gt;executables&lt;/code&gt; in your &lt;code class=&quot;highlighter-rouge&quot;&gt;Rakefile&lt;/code&gt;’s &lt;code class=&quot;highlighter-rouge&quot;&gt;Jeweler::Tasks&lt;/code&gt;. For example, you could go:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Jeweler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# The plebs aren't ready for super-jeweler yet...&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;gem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;executables&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;jeweler&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# note, this is just the file name, not the full path inside of bin&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</content>
	</entry>
	
	<entry>
		<title>Some jeweler resources you probably didn't know about</title>
		<link href="http://technicalpickles.com/posts/some-jeweler-resources-you-probably-didnt-know-about" rel="alternate" type="text/html"/>
		<id>http://technicalpickles.com/posts/some-jeweler-resources-you-probably-didnt-know-about</id>
		<updated>2009-11-03T00:00:00+00:00</updated>
		<content type="html">&lt;p&gt;If you’ve been using &lt;a href=&quot;http://technicalpickles&quot;&gt;jeweler&lt;/a&gt; for awhile or are just starting out, there are a few resources you should be aware of. I admit, maybe I’ve been a little undervocal about their existence. Anyways, here they are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The &lt;a href=&quot;http://github.com/technicalpickles/jeweler/blob/master/README.markdown&quot;&gt;README&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;http://wiki.github.com/technicalpickles/jeweler&quot;&gt;Wiki&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;http://github.com/technicalpickles/jeweler/issues&quot;&gt;Issue Tracker&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;irc://irc.freenode.net/jeweler&quot;&gt;IRC Channel&lt;/a&gt; (#jeweler on irc.freenode.net)&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;http://groups.google.com/group/jeweler-rb&quot;&gt;Mailing List&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://wiki.github.com/technicalpickles/jeweler/projects-using-jeweler&quot;&gt;Projects using jeweler&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;irc://irc.freenode.net/jeweler&quot;&gt;RDoc&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;github.com/technicalpickles/jeweler&quot;&gt;Source&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IF you have questions or whatnot, I’d suggest trying these resources out in that order.&lt;/p&gt;

&lt;p&gt;For the IRC channel, I’ll try to be available when I can. If a particular question/issue might take more than a bit to deal with, there’s a good chance I’ll direct you to post an ticket on the &lt;a href=&quot;http://github.com/technicalpickles/jeweler/issues&quot;&gt;Issue Tracker&lt;/a&gt; or to post on the &lt;a href=&quot;http://groups.google.com/group/jeweler-rb&quot;&gt;Mailing List&lt;/a&gt; for a less synchronous response.&lt;/p&gt;

&lt;p&gt;As for the &lt;a href=&quot;http://github.com/technicalpickles/jeweler/issues&quot;&gt;Issue Tracker&lt;/a&gt; and &lt;a href=&quot;http://groups.google.com/group/jeweler-rb&quot;&gt;Mailing List&lt;/a&gt;, I’ll try to respond as a quickly as is reasonable with stuff like ‘Hmm, yes, this is definitely a bug’ or ‘Could you elaborate a bit more?’ or ‘Ah, you probably want to take a look at this documentation’ or ‘Oh, that works, it’s just not documented well yet’.&lt;/p&gt;

&lt;p&gt;Also, I’d like to note that sending email or GitHub messages directly to me is probably not the way to work things out. I only say this because they tend to get lost in my inbox, and when I go to work on jeweler, the places I check are almost always going to be the &lt;a href=&quot;http://github.com/technicalpickles/jeweler/issues&quot;&gt;Issue Tracker&lt;/a&gt; and &lt;a href=&quot;http://groups.google.com/group/jeweler-rb&quot;&gt;Mailing List&lt;/a&gt;. So, don’t be offended if I end up forwarding your questions to the mailing list and answer them there. I’m doing this mostly for public archival and accountability.&lt;/p&gt;

&lt;p&gt;I guess that’s it. Open the flood gates, and I’ll see how long I can manage this…&lt;/p&gt;
</content>
	</entry>
	
</feed>
