<?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>threehv &#187; ruby</title>
	<atom:link href="http://www.3hv.co.uk/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3hv.co.uk/blog</link>
	<description>precision engineering for your website</description>
	<lastBuildDate>Fri, 13 Jan 2012 16:18:18 +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>Using ActiveRecord to connect to Sybase Adaptive Server Anywhere on Windows</title>
		<link>http://www.3hv.co.uk/blog/2009/12/21/using-activerecord-to-connect-to-sybase-adaptive-server-anywhere-on-windows/</link>
		<comments>http://www.3hv.co.uk/blog/2009/12/21/using-activerecord-to-connect-to-sybase-adaptive-server-anywhere-on-windows/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 21:18:33 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[adaptive server anywhere]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sybase]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=619</guid>
		<description><![CDATA[Just don&#8217;t ask why I know this. Please. It&#8217;s making me cry. Install Ruby using the One Click Installer. Install the ODBC module (by copying the SO files into the c:\Ruby\1.8\i386-mingw32 folder). Install the ActiveRecord ODBC adapter (gem install activerecord-odbc-adapter). Edit C:\Ruby\lib\ruby\gems\1.8\gems\activerecord-odbc-adapter-2.0\ lib\active_record\connection_adapters\odbc_adapter.rb &#8211; look for line 1588. Change the line from elsif dbmsName =~ [...]]]></description>
			<content:encoded><![CDATA[<p>Just don&#8217;t ask why I know this.  Please.  It&#8217;s making me cry.  </p>
<p>Install Ruby using the <a href="http://rubyinstaller.org/">One Click Installer</a>.<br />
Install the <a href="http://ch-werner.de/rubyodbc/">ODBC module</a> (by copying the SO files into the c:\Ruby\1.8\i386-mingw32 folder).<br />
Install the ActiveRecord ODBC adapter (gem install activerecord-odbc-adapter).<br />
Edit C:\Ruby\lib\ruby\gems\1.8\gems\activerecord-odbc-adapter-2.0\ lib\active_record\connection_adapters\odbc_adapter.rb &#8211; look for line 1588.<br />
Change the line from<code> elsif dbmsName =~ /SQLAnywhere/i</code> to <code>elsif dbmsName =~ /SQLAnywhere/i or dbmsName =~ /adaptiveserveranywhere/i</code></p>
<p>Write some test code &#8211; something like: </p>
<p><code><br />
require 'rubygems'<br />
require 'active_record'</p>
<p>ActiveRecord::Base.establish_connection(:adapter => :odbc, :dsn => 'mydsn', :username => 'myusername', :password => 'mypassword')</p>
<p>class Whatever < ActiveRecord::Base</p>
<p>end</p>
<p>Whatever.all<br />
</code><br />
Run this - hopefully you should get no errors.<br />
Then sit down and have a long think about what the hell you are doing.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/12/21/using-activerecord-to-connect-to-sybase-adaptive-server-anywhere-on-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>It happens to the best of us</title>
		<link>http://www.3hv.co.uk/blog/2009/10/05/it-happens-to-the-best-of-us/</link>
		<comments>http://www.3hv.co.uk/blog/2009/10/05/it-happens-to-the-best-of-us/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 16:21:53 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[Bug-Free Code]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Writing Reliable]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=581</guid>
		<description><![CDATA[We just had some customers report a bug. Not good. We didn&#8217;t get an exception email. All the tests passed. We couldn&#8217;t see anything untoward in the log files. But it was there. We could reproduce it, both in staging and in production. Not good at all. But the weirdest thing was we couldn&#8217;t figure [...]]]></description>
			<content:encoded><![CDATA[<p>We just had some customers report a bug. Not good. We didn&#8217;t get an exception email. All the tests passed. We couldn&#8217;t see anything untoward in the log files.  But it was there. We could reproduce it, both in staging and in production. Not good at all. </p>
<p>But the weirdest thing was we couldn&#8217;t figure out the cause. Well I could see why the code was failing (after adding some extra log messages). But &#8216;git blame&#8217; said those lines of code were unchanged in twelve months.  Why hadn&#8217;t people complained before?  Why hadn&#8217;t we <em>noticed</em> it?</p>
<p>After much hunting through log files we found the point when the feature last worked. It coincided with a deployment. That deployment was our Rails 2.3.4 forms vulnerability fix. And the bug was in a form &#8211; a missing form parameter that earlier versions of Rails ignored, the newer Rails was choking on.  </p>
<p>But why didn&#8217;t the tests catch it?  </p>
<p>After more hunting I saw that the Cucumber test that exercised the form didn&#8217;t have a &#8220;When I press the Update button&#8221; step.  And the subsequent tests were passing, even though the update button hadn&#8217;t been pressed.  </p>
<p>So I added the step in and made the feature pass.  Then deployed it as an emergency fix.  </p>
<p>However, what are the lessons to learn here (as there are always some)? </p>
<ul>
<li>Firstly, testing cannot catch everything.  </li>
<li>Secondly, the cracks in your tests are where the bugs are.  </li>
<li>Thirdly, we probably need some sort of peer review for tests.  I feel that this is more important than for code, because once the tests are right you can refactor the code without worry.  </li>
<li>Fourthly, you really need to <strong>log everything</strong>.  Absolutely everything.  Don&#8217;t worry about your huge log files &#8211; that&#8217;s what `logrotate` is for.  Get it written down so that one time when you have an obscure bug, you&#8217;ll be able to find it easily.  </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/10/05/it-happens-to-the-best-of-us/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why the lucky stiff</title>
		<link>http://www.3hv.co.uk/blog/2009/08/21/why-the-lucky-stiff/</link>
		<comments>http://www.3hv.co.uk/blog/2009/08/21/why-the-lucky-stiff/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 08:54:48 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[why the lucky stiff]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=565</guid>
		<description><![CDATA[So _why has vanished off the internet. I have no idea who he is or why he&#8217;s gone. But _why meant a lot to Ruby developers, myself included. He represented style and grace and art and weirdness &#8211; that Ruby was about more than just getting the job done in the most efficient way possible.]]></description>
			<content:encoded><![CDATA[<p>So _why has <a href="http://www.rubyinside.com/why-the-lucky-stiff-is-missing-2278.html">vanished</a> off the internet.  </p>
<p>I have no idea who he is or why he&#8217;s gone.  But _why meant a lot to Ruby developers, myself included.  He represented style and grace and art and weirdness &#8211; that Ruby was about more than just getting the job done in the most efficient way possible.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/08/21/why-the-lucky-stiff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Constructors in Ruby are not guaranteed to be called</title>
		<link>http://www.3hv.co.uk/blog/2009/06/03/constructors-in-ruby-are-not-guaranteed-to-be-called/</link>
		<comments>http://www.3hv.co.uk/blog/2009/06/03/constructors-in-ruby-are-not-guaranteed-to-be-called/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 21:28:58 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[constructors]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=557</guid>
		<description><![CDATA[Today, Caius made a discovery that shocked me. He had a class, descending from ActiveRecord::Base, with a custom constructor (initialize method). To debug it, he had the constructor raise an exception. In the console, Thingy.new(params) raised the exception as expected. But wotsit.thingies.find_by_field(value) did not. Even though it was instantiating an instance of Thingy and returning [...]]]></description>
			<content:encoded><![CDATA[<p>Today, <a href="http://caius.name/">Caius</a> made a discovery that shocked me.  </p>
<p>He had a class, descending from ActiveRecord::Base, with a custom constructor (initialize method).  To debug it, he had the constructor raise an exception.  In the console, <code>Thingy.new(params)</code> raised the exception as expected.  But <code>wotsit.thingies.find_by_field(value)</code> did not.  Even though it was instantiating an instance of Thingy and returning it.  </p>
<p>&#8220;It must not be calling the constructor&#8221; he said.<br />
&#8220;Rubbish&#8221; said I, &#8220;it&#8217;s a constructor.  Constructors are always called.  That&#8217;s the point of them&#8221;.  </p>
<p>But as he dug deeper it certainly looked like the constructor wasn&#8217;t being called.  </p>
<p>And then he found an article explaining that you should <a href="http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html">never rely</a> on things being set up on in an Active Record constructor.  Mainly because Active Record uses <code>allocate</code> to instantiate associated objects.  And what is this mysterious allocate?  Why <a href="http://whytheluckystiff.net/articles/rubyOneEightOh.html">explains it all</a>.  </p>
<p>To be honest, I&#8217;ve got mixed feelings about this.  I can see the use of &#8220;allocate&#8221; &#8211; why&#8217;s example of marshalling an object makes sense (I&#8217;m slightly less sure about the way that Active Record uses it to load associations).  But, to my mind, the definition of a constructor is &#8220;the code that is always called when an object is created&#8221;.  So maybe I should just stop thinking of <code>initialize</code> as a constructor and more as an initialiser.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/06/03/constructors-in-ruby-are-not-guaranteed-to-be-called/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Moving to Coda</title>
		<link>http://www.3hv.co.uk/blog/2009/04/22/moving-to-coda/</link>
		<comments>http://www.3hv.co.uk/blog/2009/04/22/moving-to-coda/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 21:35:47 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[coda]]></category>
		<category><![CDATA[editors]]></category>
		<category><![CDATA[espresso]]></category>
		<category><![CDATA[mac heist]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=460</guid>
		<description><![CDATA[Tomorrow I&#8217;m actually going to do it. I&#8217;m going to change the tools that I use to code in Ruby for the first time in four years. You see, I must be one of the very very few Ruby-programming Mac-users that doesn&#8217;t like Textmate. This is pretty much heresy to most Rubyists. I used to [...]]]></description>
			<content:encoded><![CDATA[<p>Tomorrow I&#8217;m actually going to do it.  </p>
<p>I&#8217;m going to change the tools that I use to code in Ruby for the first time in four years.  You see, I must be one of the very very few Ruby-programming Mac-users that doesn&#8217;t like <a href="http://macromates.com/">Textmate</a>.  </p>
<p>This is pretty much heresy to most Rubyists.  </p>
<p>I used to think I didn&#8217;t like Textmate because of the single window interface.  Multiple windows could be positioned exactly where I want them (that window only has a couple of lines in it so I can keep that tiny and see both that and that other file at the same time).  Tabs are all the same size for each file and you can&#8217;t keep them arranged as you want them. </p>
<p>But then I bought the <a href="http://www.macheist.com/">MacHeist</a> bundle.   And got a little web-developer&#8217;s editor called <a href="http://macrabbit.com/espresso/">Espresso</a>.  I really like MacRabbit&#8217;s <a href="http://macrabbit.com/cssedit/">CSSEdit</a> so was looking forward to trying Espresso.  And like it I did.  I even got over my tabbed interface problems (maybe nicely styled tabs with a list view beats ugly tabs with a drawer).  But there was a problem.  Espresso is crap for Rails development (despite <a href="http://github.com/elliottcable/ruby.sugar/commits/master">Elliott Cable&#8217;s ruby &#8220;sugar&#8221;</a>).  </p>
<p>So, having been spoilt by an &#8220;integrated&#8221; editor, but still not liking Textmate, where could I go?  <a href="http://www.panic.com/coda/">Coda</a>, by Panic, was where.  And I have to say I really like it.  It&#8217;s not perfect, it&#8217;s taken me about a week to get my head round how things hang together.  But it deals with ruby and rails and is <a href="http://www.panic.com/coda/developer/howto/plugins.php">extensible</a>.  </p>
<p>Which is enough to make me shell out the $99.  And change the way I work pretty fundamentally.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/04/22/moving-to-coda/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9 and gem compatibility</title>
		<link>http://www.3hv.co.uk/blog/2009/02/05/ruby-19-and-gem-compatibility/</link>
		<comments>http://www.3hv.co.uk/blog/2009/02/05/ruby-19-and-gem-compatibility/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 20:45:20 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[isitruby19]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=379</guid>
		<description><![CDATA[You may have heard that Ruby 1.9.1 is now out and &#8220;production-ready&#8221;.  This gives us a number of benefits, not least real threading and a general performance boost.  However, there is &#8220;production-ready&#8221; and there is &#8220;production-ready&#8221;; in particular, Ruby alone isn&#8217;t much use without the many gems that we all depend upon also being usable. [...]]]></description>
			<content:encoded><![CDATA[<p>You may have heard that Ruby 1.9.1 is now out and &#8220;production-ready&#8221;.  This gives us a number of benefits, not least real threading and a general performance boost.  However, there is &#8220;production-ready&#8221; and there is &#8220;production-ready&#8221;; in particular, Ruby alone isn&#8217;t much use without the many gems that we all depend upon also being usable.  </p>
<p>So, we at <a title="Brightbox - serious rails hosting" href="http://www.brightbox.co.uk" target="_blank">Brightbox</a> have just launched <a title="Is it ruby 1.9 compatible?" href="http://isitruby19.com" target="_blank">http://isitruby1.9.com</a>; a community site that lets you mark gems as working or not.  It&#8217;s quick and easy and will really help people out so please test your favourite gem, then stop by and record the results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/02/05/ruby-19-and-gem-compatibility/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MVC: A brief history of Models, Views and Controllers</title>
		<link>http://www.3hv.co.uk/blog/2009/01/31/mvc-a-brief-history-of-models-views-and-controllers/</link>
		<comments>http://www.3hv.co.uk/blog/2009/01/31/mvc-a-brief-history-of-models-views-and-controllers/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 17:12:58 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Smalltalk]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software design]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=367</guid>
		<description><![CDATA[Any web-developer Rubyist knows about models, views and controllers. The MVC paradigm is embedded in the structure of Rails and Merb and encouraged by Ramaze and Sinatra. If you&#8217;re a Mac developer or an iPhone bod then MVC is common practice there as well. Same goes for Sproutcore and even Microsoft is getting in on [...]]]></description>
			<content:encoded><![CDATA[<p>Any web-developer Rubyist knows about models, views and controllers.  The MVC paradigm is embedded in the structure of <a href="http://rubyonrails.org/">Rails</a> and <a href="http://merbivore.com/">Merb</a> and encouraged by <a href="http://ramaze.net/">Ramaze</a> and <a href="http://www.sinatrarb.com/">Sinatra</a>.  If you&#8217;re a <a href="http://developer.apple.com/cocoa/">Mac developer</a> or an <a href="http://developer.apple.com/iphone/">iPhone bod</a> then MVC is common practice there as well.  Same goes for <a href="http://www.sproutcore.com/">Sproutcore</a> and even Microsoft is <a href="http://www.asp.net/mvc/">getting in on the act</a>.  </p>
<p>But where does MVC come from?</p>
<p>Well, like most things I enjoy in the world of software development, MVC has its roots in Smalltalk. However, things used to look slightly different to the MVC we know and love.  In Smalltalk, the basic Object has a feature known as &#8220;dependencies&#8221; built-in; nowadays, we would call this the observer pattern.  Basically, any object can register an interest in any other object and will be notified whenever the target changes.  </p>
<pre><code>
target addDependent: listener.
</code></pre>
<p>Later, when something changes within target, it can decide to notify all its dependents (observers): </p>
<pre><code>
self changed: #SomeArbitraryMessage.
</code></pre>
<p>and the listener has its <tt>update: aSymbol</tt> method called.  </p>
<p>Building this notification mechanism right into the core of the system means that it can be used everywhere &#8211; especially when designing user-interfaces (remember, Smalltalk <em>was</em> the GUI for the Xerox Star, which his Steveness bought and adapted for the Lisa).  Models were the system components, the pieces of the system that actually do the work.  Views represented portions of the screen and controllers represented the keyboard and the mouse.  These parts were arranged as follows: <div id="attachment_368" class="wp-caption alignleft" style="width: 310px"><a href="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-smalltalk.png"><img src="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-smalltalk-300x145.png" alt="MVC in Smalltalk" title="MVC in Smalltalk" width="300" height="145" class="size-medium wp-image-368" /></a><p class="wp-caption-text">MVC in Smalltalk</p></div>  </p>
<p>The view would add itself as a dependent to the model and display the relevant aspects of the model on-screen.  The user would see this, manipulate it via the mouse and keyboard, which triggers the controller.  The controller sends messages to the model, which triggers its notifications, prompting the dependent view to redraw itself.  In other words, the controller and view are independent objects that know nothing about each other &#8211; the only tie between them is the controller&#8217;s knowledge of the model and the dependency mechanism.  </p>
<p>Contrast this to the MVC we know today: <div id="attachment_371" class="wp-caption alignright" style="width: 310px"><a href="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-rails.png"><img src="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-in-rails-300x144.png" alt="MVC in Rails" title="MVC in Rails" width="300" height="144" class="size-medium wp-image-371" /></a><p class="wp-caption-text">MVC in Rails</p></div></p>
<p>Here the user pokes the controller, which in turn sends messages to the model.  The model does its thing and then the controller extracts the relevant information and passes it to the view, which is then rendered to the user.  In this manner, the controller lives up to its name, orchestrating the entire cycle &#8211; and is the centre of all the coupling (as it knows about the view and the model).  </p>
<p>So why did things change (first in Smalltalk and then the following MVC frameworks)?</p>
<p>The first problem is that the controller takes input and the view handles output &#8211; as separate objects.  Which, especially for desktop applications, doesn&#8217;t really work; if you click on a text-field you expect different behaviour to clicking on a scrollbar.  </p>
<p>Secondly, imagine an array of models being shown in a list box.  The list box has the concept of a current selection.  So your array model (a model of models) needs to have a current selection, so that the view knows which is which.  But what if the same list is rendered in two places at the same time.  If you point both views at the same array model, they would both share the same selection properties.  So you actually end up with something more like: <div id="attachment_374" class="wp-caption alignnone" style="width: 310px"><a href="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-getting-complex.png"><img src="http://www.3hv.co.uk/blog/wp-content/uploads/2009/01/mvc-getting-complex-300x166.png" alt="Getting Complicated" title="Getting Complicated" width="300" height="166" class="size-medium wp-image-374" /></a><p class="wp-caption-text">Getting Complicated</p></div>  </p>
<p>As you can see it gets nasty pretty quickly.  And actually the controller is simply passing its messages directly through to the array model which is actually doing the routing of the other messages &#8211; the controller is pretty much redundant.  </p>
<p>So if you swap things around, merge the controller and array model (into some sort of array controller) and then add multiple array controllers &#8211; one per view &#8211; to deal with the selection issue, suddenly you have the newer style object layout.  Requests go into one of the controllers, it routes it to the model, then parcels up the results and invokes the view.  </p>
<p>Evolution in action.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/01/31/mvc-a-brief-history-of-models-views-and-controllers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The curious case of beauty in Ruby (or Rails vs Merb part 2)</title>
		<link>http://www.3hv.co.uk/blog/2008/12/27/the-curious-case-of-beauty-in-ruby-or-rails-vs-merb-part-2/</link>
		<comments>http://www.3hv.co.uk/blog/2008/12/27/the-curious-case-of-beauty-in-ruby-or-rails-vs-merb-part-2/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 00:51:41 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=349</guid>
		<description><![CDATA[I&#8217;m sure you&#8217;ve all heard the Rails 3 announcement. When I first found out my initial reaction was &#8220;fuck me&#8220;. But shortly after I was filled with a feeling of dread and general unease. And I didn&#8217;t know why &#8230;. Firstly, a bit of history. I first tried programming on a Commodore Vic 20, and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure you&#8217;ve all heard the <a href="http://weblog.rubyonrails.org/2008/12/23/merb-gets-merged-into-rails-3">Rails 3 announcement</a>.  When I first found out my initial reaction was &#8220;<a href="http://twitter.com/xbaz/status/1074944824">fuck me</a>&#8220;.  But shortly after I was filled with a feeling of <a href="http://twitter.com/xbaz/status/1076051798">dread and general unease</a>.  And I didn&#8217;t know why &#8230;.</p>
<p>Firstly, a bit of history.  </p>
<p>I first tried programming on a Commodore Vic 20, and then after that a C64.  C64 BASIC was very simple &#8211; if you wanted to do anything beyond PRINT statements you needed to POKE values into registers and control the hardware directly.  Great for learning how things actually worked.  And, to be fair, I was shit at it.  </p>
<p>But I do remember reading an article on a system called &#8220;Smalltalk&#8221; and its &#8220;Object Oriented Programming&#8221;.  Suddenly, programming made sense.  It read a bit like English.  You sent messages to the thing that knows how to answer your question.  It was like talking to people.  You ask Dave a <a href="http://www.eighteensixtyfive.co.uk">football</a> question.  You ask George a <a href="http://www.theresplendents.com">music</a> question.  Cos Dave knows crap all about music and George knows nothing about football.  </p>
<p>But, in those days, Smalltalk cost a fortune; there was no way a child like me could get hold of a Smalltalk environment.  So instead, I got hold of Turbo Pascal 6 With Objects (thanks Dad).  It was not Smalltalk but it read a bit like English and it had objects.  I played about with Turbo Pascal, went to university (where I didn&#8217;t do computing but did do some C++) and then got a job doing Delphi (Turbo Pascal for the 90s).  This object-oriented stuff really worked for me; I put a lot of effort into writing classes that had really simple public interfaces and with code that read like English.  And Pascal (the language underlying Delphi) was great for that.  Then I discovered Java, which meant I could write Delphi-like code but with having to deal with memory management.  I also discovered PHP, Python and Ruby.  None of which clicked with me; dynamic typing made me nervous (and PHP and Ruby seemed a bit ugly).  </p>
<p>However, I needed an ORM for a Delphi project and I thought I should try to copy an open source project.  Whilst searching I discovered Rails and thought &#8220;this is the one to copy&#8221;.  But a day into my &#8220;copy ActiveRecord into Delphi&#8221; plan I thought &#8220;this is just like Smalltalk&#8221;.  Why make an inferior copy when I can use something that&#8217;s not far off the Holy Grail.  Writing an application on Rails had the same effect on me as my original discovery of Smalltalk &#8211; it read like English, it felt fantastic.  So I gave up on Delphi and became a Rails programmer.  </p>
<p>What I liked about Rails was its emphasis on happiness.  When I wrote Rails code I felt like I was writing beautiful prose.  I would go back and refactor it until it read correctly.  This was not like pure Ruby, which was often ugly.  No; Rails had this idea about beauty in code that really got me excited.  It made me happy.  It also made decisions for me &#8211; put your code here, test it like this, set up your database this way.  But Rails had performance problems &#8211; so Merb was born.  A ground-up rewrite of many of Rails&#8217; ideas but with an emphasis on configurability and performance.  </p>
<p>Maybe it&#8217;s the <a href="http://engineyard.com">Engine Yard</a> connection (I turned Engine Yard down for a job because it didn&#8217;t &#8220;feel right&#8221;) &#8211; and now I work for <a href="http://www.brightbox.co.uk">Brightbox</a>, one of their competitors &#8211; but for some reason, every time I tried Merb I just couldn&#8217;t get into it.  It was weird.  Structurally and functionally it was the same as Rails &#8211; but it was Rails plus performance plus options.  And I didn&#8217;t like it.  I never got past the tutorials.  Merb emphasises clear and understandable code and was tested with RSpec (which I love).  Rails is hard to understand and uses Test::Unit (which is ugly).  But I love Rails and I can&#8217;t get into Merb.  I just couldn&#8217;t figure out why.  </p>
<p>Until today.  </p>
<p>Mr Hanson did a <a href="http://loudthinking.com/posts/37-bringing-merbs-providesdisplay-into-rails-3">blog post</a> on his first piece of Rails-Merb integration.  And something stood out at me.  As he was describing Merb&#8217;s provides/display functionality I noticed that I didn&#8217;t really &#8220;get it&#8221;.  <tt>provides</tt> made sense, but how does that relate to <tt>display</tt>.  Mr H addresses that directly: </p>
<blockquote><p>There were a couple of drawbacks with the provides/display duo, though, that we could deal with at the same time. The first was the lack of symmetry in the method names. The words &#8220;provides&#8221; and &#8220;display&#8221; doesn&#8217;t reflect their close relationship and if you throw in the fact that they&#8217;re actually both related to rendering, it&#8217;s gets even more muddy.</p></blockquote>
<p>And then he describes the Rails 3 version of the same functionality.  Instead of provides/display it becomes respond_to/respond_with.  In particular <tt>display @users</tt> becomes <tt>respond_with @users</tt>.  </p>
<p>It&#8217;s only a tiny thing.  Logically and functionally, they are exactly the same.  But DHH&#8217;s version has an emphasis <strong>on the words</strong> that are used.  How they couple together (display/provide versus respond_to/respond_with).  </p>
<p>And there is the reason that I was uneasy about Rails 3.  What if Rails lost this emphasis on the human factors &#8211; how the words mesh together &#8211; in the search of performance.  Merb is written functionally, Rails is written emotionally &#8211; Merb is about performance, Rails is about feelings.  </p>
<p>But DHH has made me feel much better about Rails 3 &#8211; he has shown that he will take Merb constructs and Railsify them, humanise them.  Because, although code is executed by computers, it is written, and more importantly, <em>read</em> by people like me.  </p>
<p>If you find this useful then please take a look at some of my other <a href="http://www.3hv.co.uk/blog">writing</a> &#8211; or recommend me on <a href="http://www.workingwithrails.com/person/8382-rahoul-baruah">Working with Rails</a>.  Cheers. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/12/27/the-curious-case-of-beauty-in-ruby-or-rails-vs-merb-part-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Writing tests for your controllers improves the design of your models</title>
		<link>http://www.3hv.co.uk/blog/2008/12/20/writing-tests-for-your-controllers-improves-the-design-of-your-models/</link>
		<comments>http://www.3hv.co.uk/blog/2008/12/20/writing-tests-for-your-controllers-improves-the-design-of-your-models/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 18:14:19 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=342</guid>
		<description><![CDATA[I&#8217;ve recently been updating some old code &#8211; partly written by someone else, partly written by myself. At the time, I thought I had written this code really well; looking back on it now, it looks awful. Fair enough, I&#8217;ve learnt a lot &#8211; I want to look back on old code and shudder. But [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been updating some old code &#8211; partly written by someone else, partly written by myself.  At the time, I thought I had written this code really well; looking back on it now, it looks awful.  Fair enough, I&#8217;ve learnt a lot &#8211; I want to look back on old code and shudder.  But also, there is very poor test coverage on this app and the tests that there are are quite unwieldy due to an over-reliance on fixtures.  </p>
<p>So I&#8217;ve been reworking them all using <a href="http://rspec.info/">RSpec</a>, my fork of <a href="http://github.com/rahoulb/rspec-rails/tree">RSpec-Rails</a> and my <a href="http://github.com/rahoulb/object-factory/tree/master">Object Factory</a> (which means I can avoid fixtures).  </p>
<p>Most of the work involves writing a spec that mimics the current behaviour (by inspecting the code and trying to match all paths through it), then refactoring the code, using the spec to prove that I haven&#8217;t broken it.  </p>
<p>But some points have some really horrible code (and lots of it) within the controllers.  As you probably know, <a href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model">Skinny Controllers</a> is the Rails way &#8211; your application logic belongs in your models (as they <em>are</em> your application) &#8211; the controller should just find or create the relevant model, ask it to do something and then render the results.  </p>
<p>Because of this, I opted to just rewrite the actions in question.  </p>
<p>To do this I started by writing a <a href="http://github.com/aslakhellesoy/cucumber/wikis">Cucumber feature</a> describing things from the user&#8217;s point of view.  Actually writing the steps that match the feature was a lot of work; because Cucumber is a full stack test you have to deal with all the dependencies that your individual action has (for example, are you logged in with the correct permissions with all associated objects created and in the right state?).  </p>
<p>Then I wrote a controller spec.  Controller specs in RSpec should use mock objects; you don&#8217;t really want to test the models, you just want to prove that the controller finds or creates the right model, asks it to do something and renders the correct output at the end.  </p>
<p>So a typical spec looks something like this (note that this is not RESTful as it was an existing part of the application that I am about to change): </p>
<pre><code>
  it "should process an item" do
    @item = mock_model Item, :work_type => :buy_stuff, :quantity => 2

    on_getting :process_item, :id => '1' do
     Item.should_receive(:find).with('1').and_return(@item)
      @stuff = mock_model Stuff
      @item.should_receive(:process).and_return(@stuff)
    end

    assigns[:stuff].should == @stuff
    response.should be_success
    response.should render_template('admin/orders/process_item')
  end
</code></pre>
<p>This basically says: </p>
<ul>
<li>We have an item, of type &#8220;buy stuff&#8221; with a quantity</li>
<li>When the process_item action is called, we expect that the controller will try to find the item with the given id</li>
<li>Then we should call process on the item and it should give us some stuff</li>
<li>The stuff should be stored in an instance variable, called stuff</li>
<li>And a page should be successfully rendered using the process_item template</li>
</ul>
<p>That&#8217;s a pretty succinct explanation of what the controller should do &#8211; I can&#8217;t think of many ways of making that skinnier.  It also bears no resemblance to the actual implementation of the action &#8211; which currently looks something like this: </p>
<pre><code>
def process_work_item
    @item =Item.find(params[:id])
    case @item.product.class.to_s.underscore.to_sym
    when :buy_stuff
      @stuff = Stuff.build_item(@item)
      @stuff.setup_new
      render :action => :process_stuff
    when :update_stuff
      @item.stuff.prepare_for_update
      render :action => :update_stuff
    else
      render :status => 404, :text => "Item product type #{@item.product.class.to_s} unknown."
    end
rescue ActiveRecord::RecordNotFound
  render_not_found
end
</code></pre>
<p>Pretty complicated &#8211; and as new types of item are added we need to add more and more clauses to that case statement.  </p>
<p>First things first &#8211; I&#8217;ve said that we should call  &#8220;process&#8221; on the item class.  So I add a pending spec to the Item specification &#8211; this is to remind me that I&#8217;ve got some work to implement later on.  </p>
<pre><code>
  it "should process itself based upon its work type"
</code></pre>
<p>Then I rework the controller so that the controller spec passes.  </p>
<pre><code>
    @item =Item.find(params[:id])
    @stuff = @item.process
    render :template => "admin/orders/process_item"
</code></pre>
<p>Pretty simple huh?</p>
<p>What we have done is shifted the logic from the controller into this new &#8220;process&#8221; method on the Item.  We have made it so that the controller knows virtually nothing about the item &#8211; all it knows is how to find it and that it has a process method.  All the implementation details are now hidden within the Item, out of the way of the outside world.  </p>
<p>Through the use of mocking we can ignore actual implementations and concentrate on presenting ourselves as simply, and minimally, as possible to the outside world.  This reduces coupling, increases flexibility and makes our code easier to read.  Don&#8217;t you agree?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/12/20/writing-tests-for-your-controllers-improves-the-design-of-your-models/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Acceptance Testing in Ruby, Rails, RSpec and Cucumber</title>
		<link>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/</link>
		<comments>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 18:16:13 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[Designing Great Software]]></category>
		<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Writing Reliable, Bug-Free Code]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=328</guid>
		<description><![CDATA[I&#8217;ve written up a new post at the Brightbox blog detailing how we are using RSpec and Cucumber to build acceptance tests for the next generation Brightbox systems.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written up a new post at the <a href="http://blog.brightbox.co.uk/posts/using-rspec-cucumber-and-user-stories-to-build-our-internal-systems">Brightbox blog</a> detailing how we are using <a href="http://rspec.info/">RSpec</a> and <a href="http://github.com/aslakhellesoy/cucumber/tree/master">Cucumber</a> to build acceptance tests for the next generation Brightbox systems.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2008/11/21/acceptance-testing-in-ruby-rails-rspec-and-cucumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.258 seconds -->

