<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mark Needham &#187; Ruby</title>
	<atom:link href="http://www.markhneedham.com/blog/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ruby: Refactoring from hash to object</title>
		<link>http://www.markhneedham.com/blog/2011/02/27/ruby-refactoring-from-hash-to-object/</link>
		<comments>http://www.markhneedham.com/blog/2011/02/27/ruby-refactoring-from-hash-to-object/#comments</comments>
		<pubDate>Sun, 27 Feb 2011 20:10:50 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Incremental Refactoring]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3390</guid>
		<description><![CDATA[Something I&#8217;ve noticed when I play around with Ruby in my own time is that I nearly always end up with the situation where I&#8217;m passing hashes all over my code and to start with it&#8217;s not a big deal. Unfortunately I eventually get to the stage where I&#8217;m effectively modelling an object inside a [...]]]></description>
			<content:encoded><![CDATA[<p>Something I&#8217;ve noticed when I play around with Ruby in my own time is that I nearly always end up with the situation where I&#8217;m passing hashes all over my code and to start with it&#8217;s not a big deal.</p>
<p>Unfortunately I eventually get to the stage where I&#8217;m effectively modelling an object inside a hash and it all gets very difficult to understand.</p>
<p>I&#8217;ve written a few times before about <a href="http://www.markhneedham.com/blog/category/coding/incremental-refactoring/">incrementally refactoring</a> code so this seemed like a pretty good chance for me to try that out.</p>
<p>The code in the view looked something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;% @tweets.each do |tweet| %&gt;
  &lt;%= tweet[:key] %&gt;  &lt;%= tweet[:value][:something_else] %&gt;
&lt;% end %&gt;</pre></div></div>

<p><cite>@tweets</cite> was being populated directly from a call to CouchDB so to start with I needed to change it from being a collection of hashes to a collection of objects:</p>
<p>I changed the Sinatra calling code from:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#0066ff; font-weight:bold;">@tweets</span> = get_the_couchdb_tweets_hash
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  tweets_hash = get_the_couchdb_tweets_hash
  <span style="color:#0066ff; font-weight:bold;">@tweets</span> = tweets_hash.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>tweet<span style="color:#006600; font-weight:bold;">|</span> TweetViewModel.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>tweet<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>where <cite>TweetViewModel</cite> is defined like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> TweetViewModel
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:key</span>, <span style="color:#ff3333; font-weight:bold;">:value</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>tweet_hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@key</span> = tweet_hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:key</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@value</span> = tweet_hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:value</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> get<span style="color:#006600; font-weight:bold;">&#40;</span>lookup<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> lookup == <span style="color:#ff3333; font-weight:bold;">:key</span>
      key
    <span style="color:#9966CC; font-weight:bold;">else</span>
      value
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  alias_method :<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:get</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The next step was to get rid of the <cite>get</cite> method and rename those <cite>attr_accessor</cite> methods to something more intention revealing.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> TweetViewModel
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:url</span>, <span style="color:#ff3333; font-weight:bold;">:messages</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>tweet_hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@url</span> = tweet_hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:key</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@messages</span> = tweet_hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:value</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;% @tweets.each do |tweet| %&gt;
  &lt;%= tweet.url %&gt;  &lt;%= tweet.messages[:something_else] %&gt;
&lt;% end %&gt;</pre></div></div>

<p>I originally didn&#8217;t realise how easy it would be to make the <cite>TweetViewModel</cite> pretend to temporarily be a <cite>Hash</cite> but it actually made it really easy for me to change the code and know that it was working the whole way.</p>
<p>For someone with more Ruby experience perhaps it wouldn&#8217;t be necessary to break out the refactoring like this because they could fairly confidently do it in one go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/02/27/ruby-refactoring-from-hash-to-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby: Where to define the method?</title>
		<link>http://www.markhneedham.com/blog/2011/02/03/ruby-where-to-define-the-method/</link>
		<comments>http://www.markhneedham.com/blog/2011/02/03/ruby-where-to-define-the-method/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 19:37:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3330</guid>
		<description><![CDATA[In our application we deal with items which can be put into a shopping cart. An item is defined like so: class Item &#60; ActiveRecord::Base &#160; end One problem that we had to solve recently was working out how to display a message to the user if the item they wanted to buy was out [...]]]></description>
			<content:encoded><![CDATA[<p>In our application we deal with items which can be put into a shopping cart.</p>
<p>An item is defined like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Item <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>One problem that we had to solve recently was working out how to display a message to the user if the item they wanted to buy was out of stock.</p>
<p>We can find out if items are out of stock by making a call to an external service:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> out_of_stock_items
  items_from_external_service_call.<span style="color:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
    item = look_up_item_by<span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    item.<span style="color:#9900CC;">number_available</span> = i<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:number_available</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    ...
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Line 5 is the interesting one because we needed to work out where to define the &#8216;number_available&#8217; method.</p>
<p>The easiest way to do it is this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Item <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:number_available</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>But it seems a bit misleading because the concept of availability doesn&#8217;t exist in every context that we use the &#8216;Item&#8217; object.</p>
<p>Another approach could be to add an instance method to each of the cart items in that context which is a bit more complicated:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> out_of_stock_items
  items_from_external_service_call.<span style="color:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
    item = look_up_item_by<span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> item.<span style="color:#9900CC;">number_available</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@number_available</span> = value
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> item.<span style="color:#9900CC;">number_available</span>
      <span style="color:#0066ff; font-weight:bold;">@number_available</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    item.<span style="color:#9900CC;">number_available</span> = i<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:number_available</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    ...
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Right now we&#8217;ve gone for the easier option and put the method onto the &#8216;Item&#8217; class but I&#8217;d be curious how others would solve this problem&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/02/03/ruby-where-to-define-the-method/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Rails: Using helpers inside a controller</title>
		<link>http://www.markhneedham.com/blog/2011/01/11/rails-using-helpers-inside-a-controller/</link>
		<comments>http://www.markhneedham.com/blog/2011/01/11/rails-using-helpers-inside-a-controller/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 17:09:49 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3271</guid>
		<description><![CDATA[For about an hour or so this afternoon we were following the somewhat evil practice of using a method defined in a helper inside a controller. The method was defined in the ApplicationHelper module: module ApplicationHelper def foo # do something end end So we initially assumed that we&#8217;d just be able to reference that [...]]]></description>
			<content:encoded><![CDATA[<p>For about an hour or so this afternoon we were following the somewhat evil practice of using a method defined in a helper inside a controller.</p>
<p>The method was defined in the ApplicationHelper module:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ApplicationHelper
	<span style="color:#9966CC; font-weight:bold;">def</span> foo
		<span style="color:#008000; font-style:italic;"># do something</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So we initially assumed that we&#8217;d just be able to reference that method inside any of our controllers since they all derive from ApplicationController.</p>
<p>That wasn&#8217;t the case so our next attempt was to try and add it as a helper:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FooController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
	helper <span style="color:#ff3333; font-weight:bold;">:application</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Which makes it accessible from the view but not from the controller&#8230;</p>
<p>Eventually we called <a href="">Ashwin</a> to help us out and he came across <a href="http://snippets.dzone.com/posts/show/1799">this thread on dzone</a>.</p>
<p>About half way down the page ovhaag points out that we can use &#8216;@template&#8217; to get access to helper methods:</p>
<blockquote><p>
In any controller, there is a &#8220;@template&#8221;-instance and you can call helper methods on this.</p>
<p>I found this trick in <a href="http://media.railscasts.com/videos/132_helpers_outside_views.mov">http://media.railscasts.com/videos/132_helpers_outside_views.mov</a><br />
&#8230;<br />
Ryan is not sure if this use is intended but it is very short and today it works.
</p></blockquote>
<p>We can use that instance variable like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FooController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
	<span style="color:#9966CC; font-weight:bold;">def</span> our_method
		<span style="color:#008000; font-style:italic;"># We can call foo like this</span>
		<span style="color:#0066ff; font-weight:bold;">@template</span>.<span style="color:#9900CC;">foo</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We eventually found out another way to do what we wanted but it seems like a neat little trick</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/01/11/rails-using-helpers-inside-a-controller/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://media.railscasts.com/videos/132_helpers_outside_views.mov" length="10900895" type="video/quicktime" />
		</item>
		<item>
		<title>Ruby: Sorting by boolean fields</title>
		<link>http://www.markhneedham.com/blog/2011/01/08/ruby-sorting-by-boolean-fields/</link>
		<comments>http://www.markhneedham.com/blog/2011/01/08/ruby-sorting-by-boolean-fields/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 13:15:19 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3255</guid>
		<description><![CDATA[We were doing a bit of work on RapidFTR in the ThoughtWorks Pune office today and one problem my pair and I were trying to solve was how to sort a collection of objects by a boolean field. Therefore given the following array of values: form_sections = &#91;FormSection.new&#40;:enabled =&#62; false, :name =&#62; &#34;a&#34;, :order =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>We were doing a bit of work on <a href="http://rapidftr.com/">RapidFTR</a> in the ThoughtWorks Pune office today and one problem my pair and I were trying to solve was how to sort a collection of objects by a boolean field.</p>
<p>Therefore given the following array of values:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">form_sections = <span style="color:#006600; font-weight:bold;">&#91;</span>FormSection.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:enabled</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;a&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>, 
                 FormSection.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:enabled</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;b&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>We wanted to display those form sections which were disabled at the bottom of the page.</p>
<p>We originally tried the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">form_sections.<span style="color:#9900CC;">sort_by</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>row.<span style="color:#9900CC;">enabled</span>, row.<span style="color:#9900CC;">order</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>But got the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">undefined method <span style="color:#996600;">`&lt;=&gt;' for true:TrueClass</span></pre></div></div>

<p>We figured we&#8217;d need to convert the true and false values to equivalent values which is demonstrated on <a href="http://stackoverflow.com/questions/903877/sort-objects-by-boolean-values-in-ruby">this Stack Overflow post</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">form_sections.<span style="color:#9900CC;">sort_by</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>row.<span style="color:#9900CC;">enabled</span> ? <span style="color:#006666;">0</span> : <span style="color:#006666;">1</span>, row.<span style="color:#9900CC;">order</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>I didn&#8217;t realise it would be that simple to do &#8211; it&#8217;s pretty neat.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/01/08/ruby-sorting-by-boolean-fields/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby: One method, two parameter types</title>
		<link>http://www.markhneedham.com/blog/2010/12/07/ruby-one-method-two-parameter-types/</link>
		<comments>http://www.markhneedham.com/blog/2010/12/07/ruby-one-method-two-parameter-types/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 05:01:44 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3193</guid>
		<description><![CDATA[One interesting thing that I&#8217;ve noticed while coding in Ruby is that due to the dynamicness of the language it&#8217;s possible to pass values of different types into a given method as parameters. For example, I&#8217;ve recently come across a few examples of methods designed like this: 1 2 3 4 5 6 def calculate_foo_prices&#40;foos&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>One interesting thing that I&#8217;ve noticed while coding in Ruby is that due to the dynamicness of the language it&#8217;s possible to pass values of different types into a given method as parameters.</p>
<p>For example, I&#8217;ve recently come across a few examples of methods designed like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span>foos<span style="color:#006600; font-weight:bold;">&#41;</span>
   ...
   <span style="color:#006600; font-weight:bold;">&#91;</span>foos<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">flatten</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>foo<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># do something</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This allows us to use the method like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># foos would come in as an array from the UI</span>
foos = <span style="color:#006600; font-weight:bold;">&#91;</span>Foo.<span style="color:#9900CC;">new</span>, Foo.<span style="color:#9900CC;">new</span>, Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#93;</span>
calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span>foos<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Or like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span>Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>It becomes quite confusing to understand why what is supposedly already a collection is being put inside an array on line 3 of the first example when you first read it.</p>
<p>An alternative would be to pull out a different method for calculating the price of the single Foo:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> calculate_foo_price<span style="color:#006600; font-weight:bold;">&#40;</span>foo<span style="color:#006600; font-weight:bold;">&#41;</span>
   calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>foo<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And then simplify the original method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span>foos<span style="color:#006600; font-weight:bold;">&#41;</span>
   ...
   <span style="color:#9900CC;">foos</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>foo<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># do something</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>While writing this I was thinking that another way could be to change the original method to look like this by using the splat operator:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>foos<span style="color:#006600; font-weight:bold;">&#41;</span>
   ...
   <span style="color:#9900CC;">foos</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>foo<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># do something</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Which means that we can use the same method for both situations:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span>Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># foos would come in as an array from the UI</span>
foos = <span style="color:#006600; font-weight:bold;">&#91;</span>Foo.<span style="color:#9900CC;">new</span>, Foo.<span style="color:#9900CC;">new</span>, Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#93;</span>
calculate_foo_prices<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>foos<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>I&#8217;m guessing the latter is more idiomatic Ruby or perhaps there&#8217;s another way I&#8217;m not aware of yet?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/12/07/ruby-one-method-two-parameter-types/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby: Exiting a &#8216;loop&#8217; early</title>
		<link>http://www.markhneedham.com/blog/2010/12/01/ruby-exiting-a-loop-early/</link>
		<comments>http://www.markhneedham.com/blog/2010/12/01/ruby-exiting-a-loop-early/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 17:56:51 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3186</guid>
		<description><![CDATA[We recently had a problem to solve which at its core required us to iterate through a collection, look up a value for each key and then exit as soon as we&#8217;d found a value. The original solution looped through the collection and then explicitly returned once a value had been found: def iterative_version v [...]]]></description>
			<content:encoded><![CDATA[<p>We recently had a problem to solve which at its core required us to iterate through a collection, look up a value for each key and then exit as soon as we&#8217;d found a value.</p>
<p>The original solution looped through the collection and then explicitly returned once a value had been found:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> iterative_version
  v = <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
    v = long_running_method i
    <span style="color:#0000FF; font-weight:bold;">return</span> v <span style="color:#9966CC; font-weight:bold;">unless</span> v.<span style="color:#0000FF; font-weight:bold;">nil</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
  v
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> long_running_method<span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;inside the long running method with #{value}&quot;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> value <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">3</span>
  value
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Which we run like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;iterative value is #{iterative_version.to_s}&quot;</span></pre></div></div>

<p>This prints the following when we run it:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">inside the long running method with 1
&quot;iterative value is 1&quot;</pre></div></div>

<p>I figured there must be a more functional way to solve the problem and I eventually came up with this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> functional_version
  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> long_running_method i <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">find</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> !i.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Which prints the following when we run it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">inside the long running method with <span style="color:#006666;">1</span>
inside the long running method with <span style="color:#006666;">2</span>
inside the long running method with <span style="color:#006666;">3</span>
inside the long running method with <span style="color:#006666;">4</span>
inside the long running method with <span style="color:#006666;">5</span>
<span style="color:#996600;">&quot;functional value is 1&quot;</span></pre></div></div>

<p>The problem is that collections in Ruby are eager evaluated so we evaluate every single item in the collection before we get the first non nil value.</p>
<p>Luckily the <a href="http://flori.github.com/lazylist/">lazylist</a> gem comes to our rescue and allows us to solve the problem in a functional way:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'lazylist'</span>
<span style="color:#9966CC; font-weight:bold;">def</span> lazy_version
  lazy_list<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">find</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> !i.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> lazy_list<span style="color:#006600; font-weight:bold;">&#40;</span>values<span style="color:#006600; font-weight:bold;">&#41;</span>
  list<span style="color:#006600; font-weight:bold;">&#40;</span>long_running_method<span style="color:#006600; font-weight:bold;">&#40;</span>values.<span style="color:#9900CC;">first</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> lazy_list<span style="color:#006600; font-weight:bold;">&#40;</span>values <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#91;</span>values.<span style="color:#9900CC;">first</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> 
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Running that gives us this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">inside the long running method with 1
&quot;lazy value is 1&quot;</pre></div></div>

<p>I&#8217;ve never come across a problem where I needed to use a lazy list but finally I have and I think the version which uses it is pretty neat.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/12/01/ruby-exiting-a-loop-early/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Ruby: Checking for environment variables in a script</title>
		<link>http://www.markhneedham.com/blog/2010/11/24/ruby-checking-for-environment-variables-in-a-script/</link>
		<comments>http://www.markhneedham.com/blog/2010/11/24/ruby-checking-for-environment-variables-in-a-script/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 18:34:45 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3152</guid>
		<description><![CDATA[I&#8217;ve been working on a Ruby script to allow us to automate part of our Solr data setup and part of the task was to check that some environment variables were set and throw an exception if not. I got a bit stuck initially trying to work out how to return a message showing only [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a Ruby script to allow us to automate part of our <a href="http://lucene.apache.org/solr/">Solr</a> data setup and part of the task was to check that some environment variables were set and throw an exception if not.</p>
<p>I got a bit stuck initially trying to work out how to return a message showing only the missing environment variables but it turned out to be pretty simple when I came back to it a couple of hours later.</p>
<p>So for my future reference than anything else, this is what I ended up with:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  variables = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>VARIABLE_1 VARIABLE_2<span style="color:#006600; font-weight:bold;">&#125;</span>
  missing = variables.<span style="color:#9900CC;">find_all</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> ENV<span style="color:#006600; font-weight:bold;">&#91;</span>v<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">unless</span> missing.<span style="color:#9900CC;">empty</span>?
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;The following variables are missing and are needed to run this script: #{missing.join(', ')}.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>I recently came across &#8216;%w&#8217; which creates a string array out of the values that we specify.</p>
<p>In this case we therefore end up with&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>VARIABLE_1 VARIABLE_2<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;VARIABLE_1&quot;</span>, <span style="color:#996600;">&quot;VARIABLE_2&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>&#8230;which I think is pretty neat!</p>
<p>The other neat method is &#8216;join&#8217; on line 4 which concatenates all the elements of an array while putting the provided separator in between each element.</p>
<p>In this case if we had neither of the variables specified we&#8217;d end up with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;VARIABLE_1&quot;</span>, <span style="color:#996600;">&quot;VARIABLE_2&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">', '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#996600;">&quot;VARIABLE_1, VARIABLE2&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/11/24/ruby-checking-for-environment-variables-in-a-script/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Capistrano, sed, escaping forward slashes and &#8216;p&#8217; is not &#8216;puts&#8217;!</title>
		<link>http://www.markhneedham.com/blog/2010/11/18/capistrano-sed-escaping-forward-slashes-and-p-is-not-puts/</link>
		<comments>http://www.markhneedham.com/blog/2010/11/18/capistrano-sed-escaping-forward-slashes-and-p-is-not-puts/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 18:40:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3131</guid>
		<description><![CDATA[Priyank and I have been working on automating part of our deployment process and one task we needed to do as part of this is replace some variables used in one of our shell scripts. All the variables in the script refer to production specific locations but we needed to change a couple of them [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/priyaaank">Priyank</a> and I have been working on automating part of our deployment process and one task we needed to do as part of this is replace some variables used in one of our shell scripts.</p>
<p>All the variables in the script refer to production specific locations but we needed to change a couple of them in order to run the script in our QA environment.</p>
<p>We&#8217;re therefore written a sed command, which we call from <a href="https://github.com/capistrano/capistrano">Capistrano</a>, to allow us to do this.</p>
<p>The Capistrano script looks a little like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">task <span style="color:#ff3333; font-weight:bold;">:replace_in_shell</span> <span style="color:#9966CC; font-weight:bold;">do</span>
	directory = <span style="color:#996600;">&quot;/my/directory/path&quot;</span>
	sed_command = <span style="color:#996600;">&quot;sed 's/^some_key.*$/#{directory}/' shell_script.sh &gt; shell_script_with_qa_variables.sh&quot;</span>
	run sed_command
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Unfortunately this creates the following sed command which isn&#8217;t actually valid syntactically:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">sed 's/^some_key.*$//my/directory/path/' shell_script.sh &gt; shell_script_with_qa_variables.sh</pre></div></div>

<p>We decided to use &#8216;gsub&#8217; to escape all the forward slashes in the directory path and to work out which parameters we needed to pass to &#8216;gsub&#8217; we started using irb.</p>
<p>Executing gsub with the appropriate parameters leads us to believe that 2 backslashes will be added:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p299 <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#996600;">&quot;/my/directory/path&quot;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span>, <span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>/my<span style="color:#000099;">\\</span>/directory<span style="color:#000099;">\\</span>/path&quot;</span></pre></div></div>

<p>This is because there IRB is implicitly called &#8216;inspect&#8217; on the result which shows a different string than what we would actually get.</p>
<p>While writing this blog post I&#8217;ve also learnt (thanks to <a href="http://twitter.com/ashwinraghav">Ashwin</a>) that &#8216;p&#8217; is not the same as &#8216;puts&#8217; which is what I originally thought and has been driving me crazy as I try to understand why everything I print includes an extra backslash!</p>
<p>The following code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;/mark/dir/&quot;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span>, <span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>is the same as typing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;/mark/dir/&quot;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span>, <span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span></pre></div></div>

<p>We were able to change our Capistrano script to escape forward slashes like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">task <span style="color:#ff3333; font-weight:bold;">:replace_in_shell</span> <span style="color:#9966CC; font-weight:bold;">do</span>
	directory = <span style="color:#996600;">&quot;/my/directory/path&quot;</span>
	sed_command = <span style="color:#996600;">&quot;sed 's/^some_key.*$/#{directory.gsub(&quot;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;, &quot;</span>\\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;}/' shell_script.sh &gt; shell_script_with_qa_variables.sh&quot;</span>
	run sed_command
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/11/18/capistrano-sed-escaping-forward-slashes-and-p-is-not-puts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails: A slightly misleading error</title>
		<link>http://www.markhneedham.com/blog/2010/11/16/rails-a-slightly-misleading-error/</link>
		<comments>http://www.markhneedham.com/blog/2010/11/16/rails-a-slightly-misleading-error/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 21:17:00 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3127</guid>
		<description><![CDATA[We recently created a new project to handle the reporting part of our application and as with all our projects we decided not to checkin any configuration &#8220;.yml&#8217; files but rather &#8216;.yml.example&#8217; files which people can then customise for their own environments. So in our config directory would look something like this when you first [...]]]></description>
			<content:encoded><![CDATA[<p>We recently created a new project to handle the reporting part of our application and as with all our projects we decided not to checkin any configuration &#8220;.yml&#8217; files but rather &#8216;.yml.example&#8217; files which people can then customise for their own environments.</p>
<p>So in our config directory would look something like this when you first checkout the project:</p>
<ul>
<li>config
<ul>
<li>database.yml.example</li>
<li>some.yml.example</li>
</ul>
</ul>
<p>And we&#8217;d need to copy those files to get &#8216;.yml&#8217; versions, changing any parameters that we need to for our local environment.</p>
<p>The disadvantage of this approach is that you have an extra step on using a project for the first time, a step that I&#8217;ve been meaning to automate.</p>
<p>Several people ran into a somewhat confusing error message when running our rake file after forgetting to create these &#8216;.yml&#8217; files which looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; rake db:migrate
(in /Users/mneedham/SandBox/project)
rake aborted!
uninitialized constant ActiveRecord
&nbsp;
(See full trace by running task with --trace)</pre></div></div>

<p>Running with &#8211;trace didn&#8217;t reveal our mistake but interestingly launching &#8216;script/console&#8217; did!</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">script/console
Loading development environment (Rails 2.3.5)
/Users/mneedham/SandBox/project/config/environment.rb:4:in `initialize':Errno::ENOENT: No such file or directory - /Users/mneedham/SandBox/project/config/some.yml
/Users/mneedham/SandBox/project/vendor/rails/railties/lib/rails/backtrace_cleaner.rb:2:NameError: uninitialized constant ActiveSupport::BacktraceCleaner
/Users/mneedham/SandBox/project/vendor/rails/railties/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController
ruby-1.8.7-p299 &gt;</pre></div></div>

<p>This is the environment.rb file in which we were loading that yml file:</p>
<p>config/environment.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">....
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'yaml'</span>
some_file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;some.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
...</pre></div></div>

<p>It&#8217;s a relatively simple mistake to make so I was surprised that rake didn&#8217;t tell us that the file didn&#8217;t exist rather than failing when trying to require ActiveRecord.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/11/16/rails-a-slightly-misleading-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Active Record: Nested attributes</title>
		<link>http://www.markhneedham.com/blog/2010/11/09/active-record-nested-attributes/</link>
		<comments>http://www.markhneedham.com/blog/2010/11/09/active-record-nested-attributes/#comments</comments>
		<pubDate>Tue, 09 Nov 2010 18:37:10 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[active-record]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3105</guid>
		<description><![CDATA[I recently learnt about quite a neat feature of Active Record called nested attributes which allows you to save attributes on associated records of a parent model. It&#8217;s been quite useful for us as we have a few pages in our application where the user is able to update models like this. We would typically [...]]]></description>
			<content:encoded><![CDATA[<p>I recently learnt about quite a neat feature of Active Record called <a href="http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html">nested attributes</a> which allows you to save attributes on associated records of a parent model. </p>
<p>It&#8217;s been quite useful for us as we have a few pages in our application where the user is able to update models like this.</p>
<p>We would typically end up with parameters coming into the controller like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FoosController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
   <span style="color:#9966CC; font-weight:bold;">def</span> update
      <span style="color:#008000; font-style:italic;"># params = { :id =&gt; &quot;1&quot;, :foo =&gt; { :baz =&gt; &quot;new_baz&quot;, :bar_attributes =&gt; { :value =&gt; &quot;something&quot; } } }</span>
      Foo.<span style="color:#9900CC;">update_instance</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span>, params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:foo</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      ...
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Our original implementation of &#8216;update_instance&#8217; looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   has_one <span style="color:#ff3333; font-weight:bold;">:bar</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> update_instance<span style="color:#006600; font-weight:bold;">&#40;</span>id, attributes_to_update<span style="color:#006600; font-weight:bold;">&#41;</span>
         instance = Foo.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
         instance.<span style="color:#9900CC;">attributes</span> = attributes_to_update
         instance
      <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Unfortunately when we execute that code the &#8216;bar&#8217; association gets completely removed because we didn&#8217;t specify the id of &#8216;bar&#8217; when we were updating the attributes.</p>
<p>We need to change the code slightly to make sure it doesn&#8217;t do that:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   has_one <span style="color:#ff3333; font-weight:bold;">:bar</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> update_instance<span style="color:#006600; font-weight:bold;">&#40;</span>id, attributes_to_update<span style="color:#006600; font-weight:bold;">&#41;</span>
         instance = Foo.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
         attributes_to_update<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:bar_attributes</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span> = instance.<span style="color:#9900CC;">bar</span>.<span style="color:#9900CC;">id</span>
         instance.<span style="color:#9900CC;">attributes</span> = attributes_to_update
         instance
      <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It now works as we&#8217;d expect.</p>
<p>There&#8217;s other cool stuff that you can do with nested attributes described on the documentation page if you have &#8216;has_many&#8217; associations but for now we&#8217;re just using the simpler &#8216;has_one&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/11/09/active-record-nested-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

