<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Adventures with Ruby</title>
	
	<link>http://iain.nl</link>
	<description />
	<lastBuildDate>Fri, 30 Jul 2010 14:23:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/iain-nl" /><feedburner:info uri="iain-nl" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Domain Driven Design: Building Blocks in Ruby</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/lm4nMxbNi9M/</link>
		<comments>http://iain.nl/2010/07/domain-driven-design-building-blocks-in-ruby/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 20:54:52 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=816</guid>
		<description><![CDATA[A few weeks ago I talked about Domain Driven Design in Ruby at the local Ruby user group: Rotterdam.rb. It had a great turnup (even though the weather prevented some from coming) and there was a good discussion going on. Thank you for that! Follow @rotterdamrb to hear about future meetups with free beer and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://rotterdam-rb.org/"><img src="http://iain.nl/wp-content/uploads/2010/07/rotterdam-rb-150x150.png" alt="" title="rotterdam-rb" width="150" height="150" class="alignright size-thumbnail wp-image-827" /></a>A few weeks ago I talked about Domain Driven Design in Ruby at the local Ruby user group: <a href="http://rotterdam-rb.org/">Rotterdam.rb</a>. It had a great turnup (even though the weather prevented some from coming) and there was a good discussion going on. Thank you for that! Follow <a href="http://twitter.com/rotterdamrb">@rotterdamrb</a> to hear about future meetups with free beer and pizza, sponsored by <a href="http://finalist.nl/">Finalist IT Group</a>!</p>
<p>It was a long talk, so I couldn&#8217;t cover all the topics I wanted to cover. I talked about ubiquitous language, bounded contexts, core and support domains, and showed some ways to do this in Ruby, using modules. Basically, I concentrated on techniques to organize <a href="http://en.wikipedia.org/wiki/Essential_complexity">essential complexity</a>, while keeping an eye on practical usage with Ruby and Rails.</p>
<p>When you talk about <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">Domain Driven Design</a>, you hardly cannot do that without mentioning the three types of objects that Eric Evans discusses in his book. It&#8217;s Entity objects, Value objects and Service objects. Still I managed to do just that. Time to fix it; here is what I failed to mention.</p>
<h3>Entities</h3>
<p>Entity objects are objects that represent something in the real world and can be referenced as such. Translated into Rails terms these would be instances of (most of) your models. You can reference a post instance by getting it via its id from the database.</p>
<p>These objects are prime candidates for using plugins like <a href="http://norman.github.com/friendly_id/">friendly_id</a>, making the fact that these objects have a real identity in real life clearer. This is because you can now reference them by name, instead of some database id. To use friendly_id with Rails 3, point your gemfile to the <a href="http://github.com/norman/friendly_id/tree/edge">&#8216;edge&#8217; branch on github</a>.</p>
<h3>Value Objects</h3>
<p>Objects that don&#8217;t have any real identity are called &#8220;Value objects&#8221;. Any object that is a value object has no real identity, nor is it important to know its identity.</p>
<p>Addresses are a good example. The value of the address (e.g. street, house number, city, country) is important. But it&#8217;s less obvious to store this in a database and reference it by an id. This id would be purely superficial and have no meaning in the domain you are designing.</p>
<p>A pure Ruby way to do this with Structs, which I have mentioned before on this blog. If you&#8217;re using a document based database, like MongoDB, these would obviously be embedded documents. With ActiveRecord you can use the <a href="http://apidock.com/rails/ActiveRecord/Aggregations/ClassMethods/composed_of"><tt>composed_of</tt>-method</a>. Allow me to demonstrate that:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#7c7c7c"># Attributes of Person include:</font>
<font color="#7c7c7c"># </font>
<font color="#7c7c7c"># * first_name&nbsp;&nbsp;string</font>
<font color="#7c7c7c"># * name_infix&nbsp;&nbsp;string</font>
<font color="#7c7c7c"># * last_name&nbsp;&nbsp; string</font>
<font color="#7c7c7c"># * male&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean</font>
<font color="#7c7c7c">#</font>
<font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Person</font>&nbsp;&lt; <font color="#ffffb6">ActiveRecord</font>::<font color="#ffffb6">Base</font>
&nbsp;&nbsp;composed_of <font color="#99cc99">:name</font>,&nbsp;&nbsp; <font color="#99cc99">:mapping</font>&nbsp;=&gt; <font color="#ffffb6">Name</font>.members
&nbsp;&nbsp;composed_of <font color="#99cc99">:gender</font>, <font color="#99cc99">:mapping</font>&nbsp;=&gt; <font color="#ffffb6">Gender</font>.members
<font color="#96cbfe">end</font></pre>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Name</font>&nbsp;&lt; <font color="#ffffb6">Struct</font>.new(<font color="#99cc99">:first_name</font>, <font color="#99cc99">:name_infix</font>, <font color="#99cc99">:last_name</font>, <font color="#99cc99">:gender</font>)

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">to_s</font>
&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;first_name, name_infix, last_name ].select(&amp;<font color="#99cc99">:present?</font>).join(<font color="#336633">'</font><font color="#a8ff60">&nbsp;</font><font color="#336633">'</font>)
&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">first_name</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">super</font>.presence || title
&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">title</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffffb6">I18n</font>.t(gender.key, <font color="#99cc99">:scope</font>&nbsp;=&gt; <font color="#99cc99">:titles</font>)
&nbsp;&nbsp;<font color="#96cbfe">end</font>

<font color="#96cbfe">end</font></pre>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Gender</font>&nbsp;&lt; <font color="#ffffb6">Struct</font>.new(<font color="#99cc99">:male</font>)

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">to_s</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffffb6">I18n</font>.t(key, <font color="#99cc99">:scope</font>&nbsp;=&gt; <font color="#99cc99">:genders</font>)
&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">key</font>
&nbsp;&nbsp;&nbsp;&nbsp;male ? <font color="#99cc99">:male</font>&nbsp;: <font color="#99cc99">:female</font>
&nbsp;&nbsp;<font color="#96cbfe">end</font>

<font color="#96cbfe">end</font></pre>
<p>By defining <tt>to_s</tt> on these structs you can output their gender or name in views without doing anything special:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#e18964">%</font><font color="#6699cc">dl</font><font color="#00a0a0">[</font><font color="#c6c5fe">@person</font><font color="#00a0a0">]</font>
&nbsp;&nbsp;<font color="#e18964">%</font><font color="#6699cc">dt</font><font color="#e18964">=</font>&nbsp;<font color="#ffffb6">Person</font>.human_attribute_name(<font color="#99cc99">:name</font>)
&nbsp;&nbsp;<font color="#e18964">%</font><font color="#6699cc">dd</font><font color="#e18964">=</font>&nbsp;<font color="#c6c5fe">@person</font>.name
&nbsp;&nbsp;
&nbsp;&nbsp;<font color="#e18964">%</font><font color="#6699cc">dt</font><font color="#e18964">=</font>&nbsp;<font color="#ffffb6">Person</font>.human_attribute_name(<font color="#99cc99">:gender</font>)
&nbsp;&nbsp;<font color="#e18964">%</font><font color="#6699cc">dd</font><font color="#e18964">=</font>&nbsp;<font color="#c6c5fe">@person</font>.gender</pre>
<p>In this example, <tt>Person</tt> is actually an aggregate of the entity <tt>Person</tt> and two value objects, called <tt>Name</tt> and <tt>Gender</tt>. Although all attributes are flattened out in your persistence layer (in this case, your database table); they do have a deeper <strong>struct</strong>ure in your code. And it&#8217;s easier to test too!</p>
<h3>Services</h3>
<p>Things that aren&#8217;t really a &#8216;thing&#8217; in the domain you&#8217;re designing are usually services. They are not really part of any entity or value object, but do something with them.</p>
<p>In Rails, these would be <a href="http://guides.rubyonrails.org/activerecord_validations_callbacks.html#observers">observers</a> or plain Ruby objects lying around. Maybe it&#8217;s time to call them what they are and place them in <tt>app/services</tt>.</p>
<p>Every Rails developer knows the pattern &#8220;Fat Model, Skinny Controller&#8221;. This is a pattern to remember that you shouldn&#8217;t put model logic in your controller but in your model. But this pattern is often taken too far. There are people that give the <tt>params</tt>-object, or worse, the entire controller-instance, to the model and do their shit there. This is not right. Use a service for that.</p>
<p>Pagination and searching are good candidates for a service. But this blog post is on the long side, so I&#8217;ll save an example implementation of that for another time. No post is complete without a promise to a follow-up that never comes, right?</p>
<h3>Conclusion</h3>
<p>It&#8217;s the same as I said during my talk: Rails helps you by keeping accidental complexity at a minimum. You can use the techniques described in Domain Driven Design to organize essential complexity and make your application more maintainable. Just be careful not to over-engineer it, that would defeat the purpose. Always be critical of your own code and continue to ask yourself the same question: <em>&#8220;Does this make my code better?&#8221;</em></p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/lm4nMxbNi9M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/07/domain-driven-design-building-blocks-in-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/07/domain-driven-design-building-blocks-in-ruby/</feedburner:origLink></item>
		<item>
		<title>Customizing IRB, 2010 edition</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/s1e26gIFt2w/</link>
		<comments>http://iain.nl/2010/07/customizing-irb-2010-edition/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 15:52:06 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=736</guid>
		<description><![CDATA[TL;DR: Check out my new .irbrc-file! Customizing my work environment is a nerdish hobby of mine. I spend far to much time tweaking my terminal. While I&#8217;ll save my terminal customizations for another time, I&#8217;ll show you my IRB tweaks in this post. There are several tools to improve your IRB, and some of them [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: right; font-size: 12px;"><strong>TL;DR</strong>: Check out my new <a href="http://github.com/iain/osx_settings/blob/master/.irbrc"><tt>.irbrc</tt>-file</a>!</p>
<p>Customizing my work environment is a nerdish hobby of mine. I spend far to much time tweaking my terminal. While I&#8217;ll save my terminal customizations for another time, I&#8217;ll show you my IRB tweaks in this post.</p>
<p>There are several tools to improve your IRB, and some of them have been around for ages. But the arrival of <a href="http://gembundler.com/">Bundler</a> makes it difficult to use them. Bundler creates a bubble in which you have to specify your dependencies explicitly. Furthermore, with project specific gemsets, provided by the ever so awesome <a href="http://rvm.beginrescueend.com/">RVM</a>, we need to install these IRB extensions for every project.</p>
<p>This means that you cannot be sure that extensions like Wirble are available in your new and shiny Rails console. There is only one way around that: add them to your Gemfile. This is what I usually add:</p>
<pre class="ir_black" style="background: #000000; color: #f6f3e8;"><!-- you're looking at ir_black colorscheme exported by macvim, I don't write my HTML like this AT ALL, seriously!  But, why in FSM's name are you reading my source? --><font face="Monaco, Courier new, monospace"><font color="#96cbfe">group</font>&nbsp;<font color="#99cc99">:development</font>&nbsp;<font color="#6699cc">do</font> 
&nbsp;&nbsp;<font color="#96cbfe">gem</font>&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">wirble</font><font color="#336633">&quot;</font> 
&nbsp;&nbsp;<font color="#96cbfe">gem</font>&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">hirb</font><font color="#336633">&quot;</font> 
&nbsp;&nbsp;<font color="#96cbfe">gem</font>&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">awesome_print</font><font color="#336633">&quot;</font>, <font color="#99cc99">:require</font>&nbsp;=&gt; <font color="#336633">&quot;</font><font color="#a8ff60">ap</font><font color="#336633">&quot;</font> 
&nbsp;&nbsp;<font color="#96cbfe">gem</font>&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">interactive_editor</font><font color="#336633">&quot;</font> 
<font color="#6699cc">end</font></font></pre>
<h3>Extension Loading</h3>
<p>To load the IRB extensions without blowing up in your face when they&#8217;re not available, I gently try to load them, and configure them only when that is successful. <a href="http://github.com/iain/osx_settings/blob/master/.irbrc">You can download my <tt>.irbrc</tt> on github</a>. Here is what it looks like:</p>
<p><img src="http://iain.nl/wp-content/uploads/2010/07/irb.png" alt="" title="irb" width="737" height="188" class="alignnone size-full wp-image-756 ir_black" /></p>
<p>When you start IRB, it shows a line with the extensions loaded. If it&#8217;s gray, it&#8217;s not appropriate (like rails2 in this example), loaded extensions are green and extensions that are not available are in red.</p>
<h3>Showing Queries in ActiveRecord 3</h3>
<p>As you can see, the queries done by ActiveRecord are displayed in the same way as they are displayed in your log files. In Rails 2, you would&#8217;ve done this by redirecting the log output to <tt>STDOUT</tt>. In Rails 3 you need to subscribe to the &#8216;<tt>sql.active_record</tt>&#8216;-notifications.</p>
<p>This could in theory also be done for other Rails 3 compatible ORMs like Mongoid, but I haven&#8217;t looked into that yet.</p>
<h3>Hirb</h3>
<p><a href="http://tagaholic.me/hirb/">Hirb</a> formats objects into pretty tables, as you can see in the picture above. It also provides some scrolling possibilities like the command line tools less and more. Very handy!</p>
<h3>Wirble</h3>
<p>The first IRB extension anyone uses. <a href="http://pablotron.org/software/wirble/">Wirble</a> provides you with history and syntax highlighting.</p>
<h3>Awesome Print</h3>
<p>While Wirble colorizes the output to improve readability, it can get cluttered really fast, especially when you&#8217;re dealing with nested hashes and arrays. <a href="http://github.com/michaeldv/awesome_print">AwesomePrint</a> helps to untangle your object mess:</p>
<p><img src="http://iain.nl/wp-content/uploads/2010/07/awesomeprint.png" alt="" title="awesomeprint" width="278" height="150" class="alignnone size-full wp-image-763 ir_black" /></p>
<h3>Print Methods</h3>
<p>The &#8216;<tt>pm</tt>&#8216;-extension I found <a href="http://snippets.dzone.com/posts/show/2916">on the intertubes</a> some time ago, lists the methods and what arguments they take on any given object. You can filter it, by providing a regex. This is what it looks like:</p>
<p><img src="http://iain.nl/wp-content/uploads/2010/07/pm.png" alt="" title="pm" width="324" height="138" class="alignnone size-full wp-image-754 ir_black" /></p>
<p>It&#8217;s not a gem, but a snippet pasted directly into my irbrc, so it&#8217;s always available.</p>
<h3>Interactive Editor</h3>
<p>Open vim (or any other editor) from IRB, edit your code, save it, close your editor and the code gets executed. Open vim again and your code is visible and editable again. Very awesome! <a href="http://github.com/jberkel/interactive_editor">Check it out</a>!</p>
<h3>More</h3>
<p>Yeah, there more. There&#8217;s <a href="http://tagaholic.me/2009/07/16/bond-from-irb-with-completion-love.html">bond</a>, which makes autocompletion better, and <a href="http://utilitybelt.rubyforge.org/">utility belt</a>, and more. I can&#8217;t remember to use them all, so I haven&#8217;t included them into my irbrc. They certainly are cool enough for you to check out! Also, a lot of great tips are <a href="http://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick">here on Stack Overflow</a>.</p>
<p>If you have any good tips, please share them! Oh, and the other OSX tweaks I use are on <a href="http://github.com/iain/osx_settings">github</a>.</p>
<p>PS. For those that don&#8217;t know how to load this: put the <tt>.irbrc</tt> file in your home directory and it will load automatically.</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/s1e26gIFt2w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/07/customizing-irb-2010-edition/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/07/customizing-irb-2010-edition/</feedburner:origLink></item>
		<item>
		<title>Using Sass with jQuery UI</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/JKFx8dDNQWg/</link>
		<comments>http://iain.nl/2010/07/using-sass-with-jquery-ui/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 14:28:12 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=726</guid>
		<description><![CDATA[Want to build a quick website or backend? Want to make it look good, but without adding too much effort in it? Then this recipe is for you! The jQuery UI framework contains some nice styles and some nice javascript to accompany it. But the class names you&#8217;re ought to be using are awkward. Nobody [...]]]></description>
			<content:encoded><![CDATA[<p>Want to build a quick website or backend? Want to make it look good, but without adding too much effort in it? Then this recipe is for you!</p>
<p>The <a href="http://jqueryui.com">jQuery UI framework</a> contains some nice styles and some nice javascript to accompany it. But the class names you&#8217;re ought to be using are awkward. Nobody wants to add &#8220;ui-widget-header&#8221; to their classes. We have standards; we want semantical html and css, even if we don&#8217;t want to be doing much styling ourselves.</p>
<p>Editing the css file that jQuery UI gives you is not an option; that would be a hideous mess even before we get started. Luckily, <a href="http://sass-lang.com">Sass</a> can help us. Sass introduced the &#8220;<tt>@extend</tt>&#8221; method since version 3, which we can (ab)use.</p>
<p>We need to convert the css file jQuery gave us to Sass. I&#8217;m a big fan of sass, not scss, so I&#8217;ll be using that:</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace">sass-convert --from css --to sass \
&nbsp;&nbsp;path/to/jquery-ui-1.8.2.custom.css &gt; app/stylesheets/_jquery_ui.sass
</font></pre>
<p>I&#8217;ve included an underscore so it won&#8217;t be compiled to a real css file when I run my application. I&#8217;ve also configured Sass to load my sass files from <tt>app/stylesheets</tt>.</p>
<p>Next, create your own sass/scss file and use <tt>@extend</tt>:</p>
<table>
<tr>
<th>screen.sass</th>
<th>screen.scss</th>
</tr>
<tr>
<td style="vertical-align: top;">
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#96cbfe">@import jquery_ui</font>

<font color="#e18964">.</font><font color="#ffffb6">project-header</font> 
&nbsp;&nbsp;@extend <font color="#e18964">.</font><font color="#ffffb6">ui-widget</font> 
&nbsp;&nbsp;@extend <font color="#e18964">.</font><font color="#ffffb6">ui-widget-header</font> 
&nbsp;&nbsp;@extend <font color="#e18964">.</font><font color="#ffffb6">ui-corner-all</font> 
</font></pre>
</td>
<td>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#96cbfe">@import &quot;jquery_ui&quot;;</font>

<font color="#e18964">.</font><font color="#ffffb6">project-header</font>&nbsp;<font color="#ffd2a7">{</font>
&nbsp;&nbsp;<font color="#96cbfe">@extend</font>&nbsp;<font color="#e18964">.</font><font color="#ffffb6">ui-widget</font>;
&nbsp;&nbsp;<font color="#96cbfe">@extend</font>&nbsp;<font color="#e18964">.</font><font color="#ffffb6">ui-widget-header</font>;
&nbsp;&nbsp;<font color="#96cbfe">@extend</font>&nbsp;<font color="#e18964">.</font><font color="#ffffb6">ui-corner-all</font>;
<font color="#ffd2a7">}</font>
</font></pre>
</td>
</tr>
</table>
<p>Now you just need to add the class &#8220;<tt>project-header</tt>&#8221; to the appropriate HTML element, include the compiled &#8220;screen.css&#8221; tag in your layout and you&#8217;re done!</p>
<p>Be sure to use the javascript too, for nice interactions. The javascript will add classes dynamically to certain elements. They will still work.</p>
<p>So what really happens? Well, <tt>@extend</tt> appends your own selector to the jQuery selector you specified. So the compiled jQuery css would&#8217;ve looked like this before:</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#ffd2a7">.ui-widget-header</font>&nbsp;<font color="#ffd2a7">{</font>
&nbsp;&nbsp;<font color="#ffffb6">border</font>: <font color="#ff73fd">1px</font>&nbsp;<font color="#ffffb6">solid</font>&nbsp;<font color="#99cc99">#aaaaaa</font>;
&nbsp;&nbsp;<font color="#7c7c7c">/* more... */</font>
<font color="#ffd2a7">}</font></font></pre>
<p>But after doing our <tt>@extend</tt>-trick, it will now compile to:</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#ffd2a7">.ui-widget-header</font><span style="background-color: #000000"><font color="#f6f3e8">,</font></span>&nbsp;<font color="#ffd2a7">.project-title</font>&nbsp;<font color="#ffd2a7">{</font>
&nbsp;&nbsp;<font color="#ffffb6">border</font>: <font color="#ff73fd">1px</font>&nbsp;<font color="#ffffb6">solid</font>&nbsp;<font color="#99cc99">#aaaaaa</font>;
&nbsp;&nbsp;<font color="#7c7c7c">/* more... */</font>
<font color="#ffd2a7">}</font></font></pre>
<p>Sass parsing is incredibly smart. Really smart. The original jQuery css will be changed automatically. Awesome! And without hardly any effort from my side!</p>
<p>For more awesomeness in styling: Have a look at <a href="http://compass-style.org">compass</a>. It will be worth your time!</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/JKFx8dDNQWg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/07/using-sass-with-jquery-ui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/07/using-sass-with-jquery-ui/</feedburner:origLink></item>
		<item>
		<title>About structs</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/JO1DxweEWrE/</link>
		<comments>http://iain.nl/2010/06/about-structs/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 08:51:45 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=723</guid>
		<description><![CDATA[Recently I talked about a monkey patch called attr_initializer, allowing you to write code like this: class&#160;FooBar &#160;&#160;attr_initializer :foo, :bar &#160;&#160;def&#160;to_s &#160;&#160;&#160;&#160;&#34;your #{foo}&#160;is #{bar}&#34; &#160;&#160;end end FooBar.new('foo', 'bar') But there is a way of doing it without a monkey patch. Use the Struct. FooBar&#160;= Struct.new(:foo, :bar) do &#160;&#160;def&#160;to_s &#160;&#160;&#160;&#160;&#34;your #{foo}&#160;is #{bar}&#34; &#160;&#160;end end FooBar.new('foo', 'bar') [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I talked about a <a href="http://iain.nl/2010/04/monkey-patch-of-the-month-attr_initializer/">monkey patch called attr_initializer</a>, allowing you to write code like this:</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">FooBar</font>
&nbsp;&nbsp;attr_initializer <font color="#99cc99">:foo</font>, <font color="#99cc99">:bar</font>
&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">to_s</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">your </font><font color="#00a0a0">#{</font>foo<font color="#00a0a0">}</font><font color="#a8ff60">&nbsp;is </font><font color="#00a0a0">#{</font>bar<font color="#00a0a0">}</font><font color="#336633">&quot;</font>
&nbsp;&nbsp;<font color="#96cbfe">end</font>
<font color="#96cbfe">end</font>

<font color="#ffffb6">FooBar</font>.new(<font color="#336633">'</font><font color="#a8ff60">foo</font><font color="#336633">'</font>, <font color="#336633">'</font><font color="#a8ff60">bar</font><font color="#336633">'</font>)</font></pre>
<p>But there is a way of doing it without a monkey patch. Use the <a href="http://apidock.com/ruby/Struct">Struct</a>.</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#ffffb6">FooBar</font>&nbsp;= <font color="#ffffb6">Struct</font>.new(<font color="#99cc99">:foo</font>, <font color="#99cc99">:bar</font>) <font color="#6699cc">do</font>
&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">to_s</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">your </font><font color="#00a0a0">#{</font>foo<font color="#00a0a0">}</font><font color="#a8ff60">&nbsp;is </font><font color="#00a0a0">#{</font>bar<font color="#00a0a0">}</font><font color="#336633">&quot;</font>
&nbsp;&nbsp;<font color="#96cbfe">end</font>
<font color="#6699cc">end</font>

<font color="#ffffb6">FooBar</font>.new(<font color="#336633">'</font><font color="#a8ff60">foo</font><font color="#336633">'</font>, <font color="#336633">'</font><font color="#a8ff60">bar</font><font color="#336633">'</font>)
</font></pre>
<p>Pretty cool.</p>
<h3>Update</h3>
<p>As was pointed out in my comments by <a href="http://rstankov.com/">Radoslav Stankov</a> (which you can&#8217;t see anymore, because I switched to Disqus), you can also do this:</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">FooBar</font>&nbsp;&lt; <font color="#ffffb6">Struct</font>.new(<font color="#99cc99">:foo</font>, <font color="#99cc99">:bar</font>)
&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">to_s</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">your </font><font color="#00a0a0">#{</font>foo<font color="#00a0a0">}</font><font color="#a8ff60">&nbsp;is </font><font color="#00a0a0">#{</font>bar<font color="#00a0a0">}</font><font color="#336633">&quot;</font>
&nbsp;&nbsp;<font color="#96cbfe">end</font>
<font color="#96cbfe">end</font>

<font color="#ffffb6">FooBar</font>.new(<font color="#336633">'</font><font color="#a8ff60">foo</font><font color="#336633">'</font>, <font color="#336633">'</font><font color="#a8ff60">bar</font><font color="#336633">'</font>)
</font></pre>
<p>I use this one more often, actually.</p>
<p>One final note: the parameters here aren&#8217;t stored as instance variables, they can only be accessed through their accessor methods.</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/JO1DxweEWrE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/06/about-structs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/06/about-structs/</feedburner:origLink></item>
		<item>
		<title>Cheating Performance With A Little Javascript</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/iufofXBUUpc/</link>
		<comments>http://iain.nl/2010/05/cheating-performance-with-a-little-javascript/#comments</comments>
		<pubDate>Thu, 20 May 2010 19:13:28 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=717</guid>
		<description><![CDATA[This little trick works with most websites with a menubar. When you have a menu that changes color pending on which page you are, you can cheat a bit on the perceived performance of loading pages. And all with just a little bit of Javascript. Intercept the links to change the state to active, before [...]]]></description>
			<content:encoded><![CDATA[<p>This little trick works with most websites with a menubar. When you have a menu that changes color pending on which page you are, you can cheat a bit on the perceived performance of loading pages. And all with just a little bit of Javascript. Intercept the links to change the state to active, before actually loading the page. The menu will feel much more responsive!</p>
<p>Imagine you add a class to the menu item that is active, like this:</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace"><font color="#96cbfe">&lt;</font>nav<font color="#96cbfe">&nbsp;</font><font color="#ffffb6">id</font><font color="#96cbfe">=</font><font color="#a8ff60">&quot;menu&quot;</font><font color="#96cbfe">&gt;</font>
&nbsp;&nbsp;<font color="#96cbfe">&lt;</font><font color="#6699cc">ul</font><font color="#96cbfe">&gt;</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">&lt;</font><font color="#6699cc">li</font><font color="#96cbfe">&gt;&lt;</font><font color="#6699cc">a</font><font color="#96cbfe">&nbsp;</font><font color="#ffffb6">href</font><font color="#96cbfe">=</font><font color="#a8ff60">&quot;foo.html&quot;</font><font color="#96cbfe">&gt;</font><font color="#80a0ff"><u>Foo</u></font><font color="#c6c5fe">&lt;/</font><font color="#6699cc">a</font><font color="#c6c5fe">&gt;&lt;/</font><font color="#6699cc">li</font><font color="#c6c5fe">&gt;</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">&lt;</font><font color="#6699cc">li</font><font color="#96cbfe">&nbsp;</font><font color="#ffffb6">class</font><font color="#96cbfe">=</font><font color="#a8ff60">&quot;active&quot;</font><font color="#96cbfe">&gt;&lt;</font><font color="#6699cc">a</font><font color="#96cbfe">&nbsp;</font><font color="#ffffb6">href</font><font color="#96cbfe">=</font><font color="#a8ff60">&quot;bar.html&quot;</font><font color="#96cbfe">&gt;</font><font color="#80a0ff"><u>Bar</u></font><font color="#c6c5fe">&lt;/</font><font color="#6699cc">a</font><font color="#c6c5fe">&gt;&lt;/</font><font color="#6699cc">li</font><font color="#c6c5fe">&gt;</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">&lt;</font><font color="#6699cc">li</font><font color="#96cbfe">&gt;&lt;</font><font color="#6699cc">a</font><font color="#96cbfe">&nbsp;</font><font color="#ffffb6">href</font><font color="#96cbfe">=</font><font color="#a8ff60">&quot;baz.html&quot;</font><font color="#96cbfe">&gt;</font><font color="#80a0ff"><u>Baz</u></font><font color="#c6c5fe">&lt;/</font><font color="#6699cc">a</font><font color="#c6c5fe">&gt;&lt;/</font><font color="#6699cc">li</font><font color="#c6c5fe">&gt;</font>
&nbsp;&nbsp;<font color="#c6c5fe">&lt;/</font><font color="#6699cc">ul</font><font color="#c6c5fe">&gt;</font>
<font color="#c6c5fe">&lt;/</font>nav<font color="#c6c5fe">&gt;</font>
</font></pre>
<p>Then all you would need to do is something like this with jQuery:</p>
<pre style="background: #000000; color: #f6f3e8" class="ir_black"><font face="Monaco, monospace">$(<font color="#ffd2a7">function</font>()&nbsp;<font color="#ffd2a7">{</font>
&nbsp;&nbsp;$(<font color="#a8ff60">'nav#menu a'</font>).click(<font color="#ffd2a7">function</font>()<font color="#ffd2a7">{</font>
&nbsp;&nbsp;&nbsp;&nbsp;$(<font color="#a8ff60">'nav#menu li'</font>).removeClass(<font color="#a8ff60">'active'</font>);
&nbsp;&nbsp;&nbsp;&nbsp;$(<font color="#c6c5fe">this</font>).blur().parents(<font color="#a8ff60">'li'</font>).addClass(<font color="#a8ff60">'active'</font>);
&nbsp;&nbsp;<font color="#ffd2a7">}</font>);
<font color="#ffd2a7">}</font>);</font></pre>
<p>I&#8217;ve added a <tt>blur()</tt>-command in between, so the dotted lines disappear earlier.</p>
<p>Try it out, you can really notice the difference. It works best when the menu items don&#8217;t move, but only change appearance.</p>
<h3>Update</h3>
<p>Because I switched to Disqus, some nice comments have gone. <a href="http://twitter.com/sborsje">Stefan Borsje</a>, of <a href="http://pressdoc.com">Pressdoc</a> fame, replied with this concern:</p>
<blockquote><p>To be honest, I don’t like it very much. It really changes a basic way the web works; you click on something, the page loads (and optionally shows an activity indicator) and you get to see the result. With this method you already get see part of the result even before the actual loading fase, which could easily confuse users by letting them think the page is already done but nothing has changed.</p></blockquote>
<p>I agree that you should be careful applying this technique. But the clicking of the link is done instantly, so the user would see the page loading immediately, thus he/she wouldn&#8217;t think the page is done. This technique works best if your pages are speedy enough, but can use just a bit more oomph.</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/iufofXBUUpc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/05/cheating-performance-with-a-little-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/05/cheating-performance-with-a-little-javascript/</feedburner:origLink></item>
		<item>
		<title>Helpers Are Code Too!</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/bwVeNQ4A-Qg/</link>
		<comments>http://iain.nl/2010/05/helpers-are-code-too/#comments</comments>
		<pubDate>Sat, 15 May 2010 15:17:20 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=703</guid>
		<description><![CDATA[I think I&#8217;ve talked about this before, and there has been a Railscasts episode about it too, but I want to touch on it again. I know we&#8217;re supposed to keep views simple, but that doesn&#8217;t mean that helpers can only contain methods. Rails gives you a helper module for every controller. The problem with [...]]]></description>
			<content:encoded><![CDATA[<p>I think <a href="http://iain.nl/2008/04/bringing-objects-to-views/">I&#8217;ve talked about this</a> before, and there has been a <a href="http://railscasts.com/episodes/101-refactoring-out-helper-object">Railscasts episode</a> about it too, but I want to touch on it again. I know we&#8217;re supposed to keep views simple, but that doesn&#8217;t mean that helpers can only contain methods.</p>
<p>Rails gives you a helper module for every controller. The problem with modules is that they don&#8217;t contain state and are usually used to just put a lot of methods in. But this can grow quickly out of hand when the stuff that you&#8217;re building is a little more complex.</p>
<p>Never forget to run tools like <a href="http://wiki.github.com/kevinrutherford/reek/">Reek</a> on your helpers as well. What do they say? Are your methods too long? Do your methods require too many arguments? Is the complexity too high? Do your methods have <em>feature envy</em>?</p>
<p>I&#8217;ve seen quite a lot of Rails projects and helpers are almost always the forgotten parts of the application. Every Rails developer knows about the &#8220;Skinny Controller, Fat Model&#8221;-principle. Better teams also make skinny models, by using modules and creating macro&#8217;s. But helpers are usually ugly. And they shouldn&#8217;t!</p>
<p><strong>Helpers are code too! They want your love and dedication!</strong></p>
<p>There are a couple of ways to keep it clean. Sometimes they are called &#8220;Presenter Objects&#8221; or something similar. It doesn&#8217;t matter how you call them, and you don&#8217;t have to use any complicated gems or libraries. Regular classes suffice.</p>
<p>Here&#8217;s an example of a typical helper:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">module</font>&nbsp;<font color="#ffffb6">PostsHelper</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">post_title</font>(post)
&nbsp;&nbsp;&nbsp;&nbsp;header_class = post.published? ? <font color="#336633">&quot;</font><font color="#a8ff60">published</font><font color="#336633">&quot;</font>&nbsp;: <font color="#336633">&quot;</font><font color="#a8ff60">normal</font><font color="#336633">&quot;</font>
&nbsp;&nbsp;&nbsp;&nbsp;header = content_tag(<font color="#99cc99">:h2</font>, post.title, <font color="#99cc99">:class</font>&nbsp;=&gt; header_class)
&nbsp;&nbsp;&nbsp;&nbsp;content_tag(<font color="#99cc99">:div</font>, <font color="#99cc99">:class</font>&nbsp;=&gt; <font color="#336633">&quot;</font><font color="#a8ff60">post header</font><font color="#336633">&quot;</font>) <font color="#6699cc">do</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#6699cc">if</font>&nbsp;post.user == current_user
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;header, edit_post(post), destroy_post(post) ].compact.join(<font color="#336633">'</font><font color="#a8ff60">&nbsp;</font><font color="#336633">'</font>)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#6699cc">else</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#6699cc">end</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#6699cc">end</font>
&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">edit_post</font>(post)
&nbsp;&nbsp;&nbsp;&nbsp;link_to(<font color="#336633">'</font><font color="#a8ff60">edit</font><font color="#336633">'</font>, edit_post_path(post))
&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">destroy_post</font>(post)
&nbsp;&nbsp;&nbsp;&nbsp;link_to(<font color="#336633">'</font><font color="#a8ff60">delete</font><font color="#336633">'</font>, post, <font color="#99cc99">:method</font>&nbsp;=&gt; <font color="#99cc99">:delete</font>, <font color="#99cc99">:confirm</font>&nbsp;=&gt; <font color="#336633">&quot;</font><font color="#a8ff60">are you sure?</font><font color="#336633">&quot;</font>)
&nbsp;&nbsp;<font color="#96cbfe">end</font>

<font color="#96cbfe">end</font></pre>
<p>You might choose a different route, like putting the <tt>if</tt>-statement in the view. But that&#8217;s beside the point. The point is that this is what usually happens when you don&#8217;t use objects as they are supposed to be used. Look at the post argument for example. It&#8217;s on every frickin&#8217; method! That&#8217;s not <abbr title="Don't Repeat Yourself">DRY</abbr>!</p>
<p>And what if you need to extend it? What if you need approve and reject links as well? What if the destroy link needs to only show when the post has been approved? It&#8217;s becoming more and more messy. Time to refactor!</p>
<p>This is an example of how I would do it:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">module</font>&nbsp;<font color="#ffffb6">PostsHelper</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">post_title</font>(post)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffffb6">PostTitle</font>.new(<font color="#99cc99">self</font>, post).to_html
&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">PostTitle</font>

&nbsp;&nbsp;&nbsp;&nbsp;attr_initializer <font color="#99cc99">:template</font>, <font color="#99cc99">:post</font>
&nbsp;&nbsp;&nbsp;&nbsp;delegate <font color="#99cc99">:content_tag</font>, <font color="#99cc99">:link_to</font>, <font color="#99cc99">:current_user</font>, <font color="#99cc99">:to</font>&nbsp;=&gt; <font color="#99cc99">:template</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">to_html</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content_tag(<font color="#99cc99">:div</font>, elements, <font color="#99cc99">:class</font>&nbsp;=&gt; <font color="#336633">&quot;</font><font color="#a8ff60">post header</font><font color="#336633">&quot;</font>)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">elements</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;header, edit, delete, approve, reject ].compact.join(<font color="#336633">'</font><font color="#a8ff60">&nbsp;</font><font color="#336633">'</font>)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">header</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content_tag(<font color="#99cc99">:h2</font>, post.title, <font color="#99cc99">:class</font>&nbsp;=&gt; header_class)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">header_class</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;post.published? ? <font color="#336633">&quot;</font><font color="#a8ff60">published</font><font color="#336633">&quot;</font>&nbsp;: <font color="#336633">&quot;</font><font color="#a8ff60">normal</font><font color="#336633">&quot;</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">edit</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;link_to(<font color="#336633">'</font><font color="#a8ff60">edit</font><font color="#336633">'</font>, edit_post_path(post)) <font color="#6699cc">if</font>&nbsp;my_post?
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">my_post?</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;post.user == current_user
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#7c7c7c"># etc... (you get the drift)</font>

&nbsp;&nbsp;<font color="#96cbfe">end</font>

<font color="#96cbfe">end</font></pre>
<p>Nice! Small testable methods! Readable code! Less repetition. <em>Run Reek on that!</em></p>
<p>If you&#8217;re wondering what the <tt>attr_initializer</tt> is, it&#8217;s a monkey patch, that I&#8217;ve <a href="http://iain.nl/2010/04/monkey-patch-of-the-month-attr_initializer/">described here</a>. The <a href="http://apidock.com/rails/Module/delegate">delegate-method</a> is something ActiveSupport offers you. Use it, it&#8217;s super effective!</p>
<p>Wait a minute. Did I say &#8220;testable&#8221;? Yes, I most certainly did! As with any code, this needs to be tested. But it&#8217;s not that hard anymore! If you&#8217;re using <a href="http://rspec.info">Rspec</a>, you can use the <a href="http://rspec.info/rails/writing/helpers.html">helper specs</a> it provides. But you don&#8217;t need to!</p>
<p>The <tt>PostTitle</tt>-class is a regular Ruby class, and so every test framework can test with it without much effort. You might want to use stubs and mocks for the other helper methods you call. Or subclass <tt>PostTitle</tt>, define the used helper methods and use this to subclass in your tests. The <tt>delegate</tt> specifies which other helper methods are used.</p>
<p>Remember that what you program is entirely up to you. You, and only you, have to decide when a simple method is enough, or whether you need to add a class, or something similar. You can also consider using <a href="http://builder.rubyforge.org/">Builder</a> for some parts, if the HTML generation becomes too hard.</p>
<p>The main point is that any part of your application needs to receive the same attention to detail. Don&#8217;t hide your dead bodies (of code) in helpers, or anywhere! Be critical everywhere and all the time! Run Reek regularly. It&#8217;s a depressing tool and you don&#8217;t need to fix every message every time. Use it wisely and pay attention. There is <strong>no</strong> excuse for ugly code!</p>
<p>In this example, I would use a partial, but when the view logic increases, you should be thinking about extracting it.</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/bwVeNQ4A-Qg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/05/helpers-are-code-too/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/05/helpers-are-code-too/</feedburner:origLink></item>
		<item>
		<title>Conway’s Game of Life; or: Writing Perl in Ruby</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/EtsjYyS_WIo/</link>
		<comments>http://iain.nl/2010/04/conways-game-of-life-or-writing-perl-in-ruby/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 14:49:04 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=690</guid>
		<description><![CDATA[We had a programming exercise this week at work. We were set out to write Conway&#8217;s Game of Life, using pair programming and TDD. It was a fun and educational evening. I even did some Scala. Programmers humor dictates me that from time to time I say things like &#8220;In Ruby, that&#8217;s only one line!&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>We had a programming exercise this week at work. We were set out to write <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway&#8217;s Game of Life</a>, using pair programming and TDD. It was a fun and educational evening. I even did some Scala.</p>
<p>Programmers humor dictates me that from time to time I say things like &#8220;In Ruby, that&#8217;s only one line!&#8221; or &#8220;In Ruby, that would cost me just one minute!&#8221;. Putting money where my mouth is, I wrote the entire application in just one line of Ruby.</p>
<p>So here it is. It looks more like Perl than Ruby. But it works!</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#ffffb6">String</font>.class_eval{define_method(<font color="#99cc99">:to_grid</font>){(<font color="#99cc99">self</font>&nbsp;=~ <font color="#ff8000">/</font><font color="#e18964">\A</font><font color="#e18964">(</font><font color="#e18964">\d</font><font color="#e18964">+</font><font color="#e18964">)</font><font color="#b18a3d">x</font><font color="#e18964">(</font><font color="#e18964">\d</font><font color="#e18964">+</font><font color="#e18964">)</font><font color="#e18964">\z</font><font color="#ff8000">/</font>&nbsp;? 
(<font color="#ff73fd">0</font>...split(<font color="#336633">'</font><font color="#a8ff60">x</font><font color="#336633">'</font>).last.to_i).map{|<font color="#c6c5fe">_</font>|&nbsp;(<font color="#ff73fd">0</font>...split(<font color="#336633">'</font><font color="#a8ff60">x</font><font color="#336633">'</font>).first.to_i).map{|<font color="#c6c5fe">_</font>|&nbsp;rand &gt; <font color="#ff73fd">0.5</font>&nbsp;} } : 
split(<font color="#336633">&quot;</font><font color="#e18964">\n</font><font color="#336633">&quot;</font>).map{|<font color="#c6c5fe">row</font>|&nbsp;row.split(<font color="#ff8000">//</font>).map{|<font color="#c6c5fe">cell_string</font>|&nbsp;cell_string == <font color="#336633">&quot;</font><font color="#a8ff60">o</font><font color="#336633">&quot;</font>&nbsp;} }
).tap{|<font color="#c6c5fe">grid</font>|&nbsp;grid.class.class_eval{define_method(<font color="#99cc99">:next</font>){each{|<font color="#c6c5fe">row</font>|&nbsp;
row.each{|<font color="#c6c5fe">cell</font>|&nbsp;cell.class.class_eval{define_method(<font color="#99cc99">:next?</font>){|<font color="#c6c5fe">neighbours</font>|&nbsp;
(<font color="#99cc99">self</font>&nbsp;&amp;&amp; (<font color="#ff73fd">2</font>..<font color="#ff73fd">3</font>).include?(neighbours.select{|<font color="#c6c5fe">me</font>|&nbsp;me }.size)) || 
(!<font color="#99cc99">self</font>&nbsp;&amp;&amp; neighbours.select{|<font color="#c6c5fe">me</font>|&nbsp;me }.size == <font color="#ff73fd">3</font>)}}}} &amp;&amp; 
enum_for(<font color="#99cc99">:each_with_index</font>).map{|<font color="#c6c5fe">row</font>, <font color="#c6c5fe">row_num</font>|&nbsp;row.enum_for(<font color="#99cc99">:each_with_index</font>).map{|<font color="#c6c5fe">cell</font>, <font color="#c6c5fe">col_num</font>|&nbsp;
cell.next?([&nbsp;at(row_num - <font color="#ff73fd">1</font>).at(col_num - <font color="#ff73fd">1</font>), at(row_num - <font color="#ff73fd">1</font>).at(col_num), 
at(row_num - <font color="#ff73fd">1</font>).at((col_num + <font color="#ff73fd">1</font>) % row.size), row.at((col_num + <font color="#ff73fd">1</font>) % row.size), 
row.at(col_num - <font color="#ff73fd">1</font>), at((row_num + <font color="#ff73fd">1</font>) % size).at(col_num - <font color="#ff73fd">1</font>), 
at((row_num + <font color="#ff73fd">1</font>) % size).at(col_num), at((row_num + <font color="#ff73fd">1</font>) % size).at((col_num + <font color="#ff73fd">1</font>) % row.size) ]) 
} }} &amp;&amp; define_method(<font color="#99cc99">:to_s</font>){map{|<font color="#c6c5fe">row</font>|&nbsp;row.map{|<font color="#c6c5fe">cell</font>|&nbsp;cell ? <font color="#336633">&quot;</font><font color="#a8ff60">o</font><font color="#336633">&quot;</font>&nbsp;: <font color="#336633">&quot;</font><font color="#a8ff60">.</font><font color="#336633">&quot;</font>&nbsp;}.join }.join(<font color="#336633">&quot;</font><font color="#e18964">\n</font><font color="#336633">&quot;</font>)}}}}}
</pre>
<p>I didn&#8217;t cheat! No semicolons where harmed during the making of the spaghetti code. I did enter some newlines in the code shown above to prevent wild scrollbars from appearing.</p>
<p>You can use it like this:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#7c7c7c"># generate a random grid</font>
grid = <font color="#336633">&quot;</font><font color="#a8ff60">100x30</font><font color="#336633">&quot;</font>.to_grid
<font color="#7c7c7c"># show the grid:</font>
puts grid.to_s
<font color="#7c7c7c"># get the next generation of the grid</font>
new_grid = grid.next
<font color="#7c7c7c"># convert a drawn out grid to a grid</font>
new_grid.to_s.to_grid == new_grid <font color="#7c7c7c"># returns true</font>
</pre>
<p>If you want to know how it works, or make an animation of it, <a href="http://gist.github.com/384487">it&#8217;s all here</a>.</p>
<p>This is what it looks like:</p>
<p><img src="http://iain.nl/wp-content/uploads/2010/04/Screen-shot-2010-04-30-at-16.42.51.png" alt="Conway&#039;s Game of Life" title="Conway&#039;s Game of Life" width="754" height="497" class="alignnone size-full wp-image-698 ir_black" /></p>
<p>It was a fun exercise, but I don&#8217;t think I&#8217;ll be writing all my code like this in the future :)</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/EtsjYyS_WIo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/04/conways-game-of-life-or-writing-perl-in-ruby/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/04/conways-game-of-life-or-writing-perl-in-ruby/</feedburner:origLink></item>
		<item>
		<title>ViewTranslator, a dynamic dispatcher</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/RFjo8qe_HY8/</link>
		<comments>http://iain.nl/2010/04/viewtranslator-a-dynamic-dispatcher/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 23:10:20 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=684</guid>
		<description><![CDATA[I&#8217;m a sucker for syntax. So once again, here&#8217;s a small experiment. It might not be the most useful snippet out there, but maybe it inspires you to do something awesome. The &#8216;Dynamic Dispatcher&#8217; is a common pattern in Ruby. Here I&#8217;ll demonstrate it to add some syntactic sugar. In views you can automatically scope [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a sucker for syntax. So once again, here&#8217;s a small experiment. It might not be the most useful snippet out there, but maybe it inspires you to do something awesome. The &#8216;Dynamic Dispatcher&#8217; is a common pattern in Ruby. Here I&#8217;ll demonstrate it to add some syntactic sugar.</p>
<p>In views you can automatically scope translations to the view you&#8217;re working in.</p>
<p>So this HAML code (in the <tt>users#index</tt> view):</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#e18964">=</font>&nbsp;t(<font color="#336633">'</font><font color="#a8ff60">.foo</font><font color="#336633">'</font>)
</pre>
<p>It&#8217;s the same as:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#e18964">=</font>&nbsp;t(<font color="#99cc99">:foo</font>, <font color="#99cc99">:scope</font>&nbsp;=&gt; [<font color="#99cc99">:users</font>, <font color="#99cc99">:index</font>])</pre>
<p>I use this technique a lot.</p>
<p>Anyway, we could clean up it even more, by making a dynamic dispatcher. It would look something like this:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">module</font>&nbsp;<font color="#ffffb6">ViewTranslatorHelper</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">vt</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#c6c5fe">@view_translator</font>&nbsp;||= <font color="#ffffb6">ViewTranslator</font>.new(<font color="#99cc99">self</font>)
&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">ViewTranslator</font>&nbsp;&lt; <font color="#ffffb6">ActiveSupport</font>::<font color="#ffffb6">BasicObject</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">initialize</font>(template)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#c6c5fe">@template</font>&nbsp;= template
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">method_missing</font>(method, options = {})
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffffb6">ViewTranslator</font>.class_eval &lt;&lt;-<font color="#336633">RUBY</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def </font><font color="#00a0a0">#{</font>method<font color="#00a0a0">}</font><font color="#a8ff60">(options = {})</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@template.t(&quot;.</font><font color="#00a0a0">#{</font>method<font color="#00a0a0">}</font><font color="#a8ff60">&quot;, options)</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#336633">RUBY</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__send__(method, options)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#96cbfe">end</font>

&nbsp;&nbsp;<font color="#96cbfe">end</font>

<font color="#96cbfe">end</font></pre>
<p>And now you can write:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#e18964">=</font>&nbsp;vt.foo</pre>
<h2>How does this work?</h2>
<p>The <tt>vt</tt> method returns <tt>ViewTranslator</tt> instance. It is cached inside an instance variable. The <tt>ViewTranslator</tt> object inherits from <tt>BasicObject</tt> (<tt>ActiveSupport</tt>&#8216;s <tt>BasicObject</tt> uses Ruby 1.9 if it is on Ruby 1.9, and constructs it&#8217;s own when on a lower Ruby version). <tt>BasicObject</tt> is an object that knows no methods, except methods like <tt>__id__</tt> and <tt>__send__</tt>. This makes it ideal for using dynamic dispatchers.</p>
<p>When we define <tt>method_missing</tt> every single method we call on it will be passed to there. We could call <tt>@template.t</tt> directly from here, but we don&#8217;t. To know why, we must know how <tt>method_missing</tt> works. When you call a method on an object, it looks to see if the object knows the method. When it doesn&#8217;t know it, it looks to it&#8217;s superclass and tries again. This happens all the way until it reaches the top of the chain. In Ruby 1.8 that is <tt>Object</tt>, because every object inherits from <tt>Object</tt>. Ruby 1.9 goes one step further and goes to <tt>BasicObject</tt>. If a method is not found anywhere, it will go to the original object you called the method on and it calls <tt>method_missing</tt>. Since that usually isn&#8217;t there, it goes up the superclass chain until it comes to (<tt>Basic</tt>)<tt>Object</tt>. There it exists. It will raise the exception we all know and hate: <tt>NoMethodError</tt>. You can do this yourself too:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black">> "any object".method_missing(:to_s)
NoMethodError: undefined method `to_s' for "any object":String
</pre>
<p>You see, even though the method <tt>to_s</tt> does exist on the string, we stepped halfway in the process of a method call. The error message is a bit confusing, but the we just called a method on the superclass of <tt>String</tt>. Anyway, by defining <tt>method_missing</tt> on our own object, it cuts this chain short. To cut it even shorter, I define the method itself, so it doesn&#8217;t need to go through this process at all. After it&#8217;s defined I call the freshly created method.</p>
<p>Now, to be honest. This is not at all that expensive to use method_missing in this case. The chain is only classes long, so it&#8217;s hardly putting a dent in your performance. There are cases were this is <em>very</em> important though. One such case is <tt>ActiveRecord</tt>. When you call a method on a new <tt>ActiveRecord</tt>-object for the first time, you reach <tt>method_missing</tt>. It needs to look at the database to find out if it is an attribute. Looking inside the database is very expensive, so <tt>method_missing</tt> creates methods for all attributes. If the attribute exists, it will be called, and it&#8217;ll be a normal method call from then on.</p>
<p>Thanks for reading. If you found it informative: I love feedback ;)</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/RFjo8qe_HY8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/04/viewtranslator-a-dynamic-dispatcher/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/04/viewtranslator-a-dynamic-dispatcher/</feedburner:origLink></item>
		<item>
		<title>Qu’est-ce que c’est Rubyesque?</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/1nKqwqSLhzs/</link>
		<comments>http://iain.nl/2010/04/quest-ce-que-cest-rubyesque/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 09:14:36 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=669</guid>
		<description><![CDATA[This is a translation of the post I originally wrote down in Dutch for my company. I recently gave a presentation at my company Finalist IT Group about the philosophy behind Ruby. Code written with this philosophy in mind is called &#8216;Rubyesque&#8217;. It&#8217;s not hard science, so we had plenty ingredients for a discussion. The [...]]]></description>
			<content:encoded><![CDATA[<p>This is a translation of <a href="http://blog.finalist.com/2010/04/12/quest-ce-que-cest-rubyesque/">the post I originally wrote down in Dutch</a> for my company.</p>
<p>I recently gave a presentation at my company Finalist IT Group about the philosophy behind Ruby. Code written with this philosophy in mind is called &#8216;Rubyesque&#8217;. It&#8217;s not hard science, so we had plenty ingredients for a discussion.</p>
<p>The reason behind the presentation was because we&#8217;re in need of more Ruby developers and looked inside our own company for Java developers that want to learn Ruby. Because Java and the philosophy behind it are so radically different from Ruby, we organized a discussion to explore those differences.</p>
<h3>About programming and the &#8216;flow&#8217;</h3>
<p>The Rubyesque mindset is all about opinions. Opinions tend to vary from person to person. By reading a lot of blogs and code I do see some sort of consensus. This opinion is not always explicitly mentioned, so I don&#8217;t pretend to speak in absolute truths or be complete.</p>
<p>Programming is usually compared with other professions to give an idea what it&#8217;s like to do it. Ruby programmers tend to compare programming with craftsmanship. I even go one step further. I think programming Ruby is a form of art of playing an instrument. Programming is a creative process. You&#8217;re programming to express meaning and intent. The only way to achieve this, is if you know your instrument so intensely that it&#8217;s not compromising you&#8217;re ability to express yourself.</p>
<p>Every programming language has its own style. You can call it its &#8216;flow&#8217;. It&#8217;s wise to go with the flow. It&#8217;s easier to use the language as it&#8217;s intended to be used than to go against it. For example, don&#8217;t try to force <a href="http://en.wikipedia.org/wiki/Class-based_programming">inheritance</a> in a language that is intended to be <a href="http://en.wikipedia.org/wiki/Prototype-based_programming">prototype based</a>. Program according to the flow and other programmers can understand and maintain your code better.</p>
<h3>About Ruby</h3>
<p>Everything in Ruby is aimed towards making the programmer happy. A happy programmer is better motivated to deliver great code and will feel responsible to do so. And better code makes everyone happy.</p>
<p>It&#8217;s the machine that is supposed to do the heavy lifting, not the programmer. This means that Ruby isn&#8217;t particularly fast, but since machine power is cheaper than man power, it&#8217;s not that big of a deal.</p>
<p>Ruby has a couple of traits that make it a joy to work with:</p>
<ul>
<li>Dynamic and open to bend it to your liking</li>
<li>Few rules, so easy to learn</li>
<li>There are &#8216;shortcuts&#8217; for common tasks</li>
<li>Many ways to reuse code, such as mixins</li>
<li>Optional punctuation to improve readability</li>
<li>Methods like <tt><a href="http://www.rubyist.net/~slagell/ruby/accessors.html">attr_accessor</a></tt> to become more <a href="http://en.wikipedia.org/wiki/DRY">DRY</a></li>
</ul>
<h3>Dynamic</h3>
<p>In a dynamic language it&#8217;s not important to know which type of object you&#8217;re dealing with, but it&#8217;s more important to know what it can do for you. This is called <a href="http://en.wikipedia.org/wiki/Duck_typing">duck typing</a>. This isn&#8217;t only handy in making mock object, but also means that you don&#8217;t have to abuse inheritance to comply to some method you&#8217;d like to call.</p>
<p>A nice example is the Rack specification. The header object you&#8217;re supposed to return needs to have the method <tt>each</tt>. It doesn&#8217;t say that it needs to be a hash. You could use a hash, but if it makes more in your sense to have some other object, it&#8217;s fine. You don&#8217;t have to inherit from hash, just to implement the <tt>each</tt> method.</p>
<p>The <em>flow</em> in Ruby is not to perform any check of the objects. If you&#8217;re explicitly want an integer, just call <tt>to_i</tt> inside the method. Any code that checks the type is usually considered to be distracting from the real meaning of the code. It&#8217;s the programmer that uses the gem/library responsibility to use the right objects, and not the responsibility of the gem-maker. The naming of methods and variables need to be obvious enough, so that it shouldn&#8217;t be a guessing game.</p>
<h3>Open</h3>
<p>In Ruby, nothing is final. This allows for the ability to add and change anything in any object, including core classes. This is called Monkey Patching or <a href="http://en.wikipedia.org/wiki/Duck_punching">duck punching</a>. This results in even nicer and more readable code, like:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#ff73fd">15</font>.minutes.ago</pre>
<p>Nobody needs to explain this. Nobody needs to look up any documents to understand what is going on here. Monkey patching can improve the readability of the code immensely.</p>
<p>I understand that this technique may be considered extreme by some people. It doesn&#8217;t comply to the strict rules that govern most other languages. Still, these kinds of techniques are very handy and addicting.</p>
<p>Monkey patching is dangerous. It&#8217;s your own responsibility to use it responsible. Use tests to ensure that you don&#8217;t break anything unintentionally.</p>
<h3>Punctuation</h3>
<p>Some of Ruby&#8217;s punctuation is optional. Ruby programmers love to leave them out. It&#8217;s mostly a matter of taste. Readability is key here.</p>
<p>Rubyists rather write this:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Post</font>
&nbsp;&nbsp;belongs_to <font color="#99cc99">:author</font>, <font color="#99cc99">:dependent</font>&nbsp;=&gt; <font color="#99cc99">:destroy</font>
<font color="#96cbfe">end</font></pre>
<p>Than the same code with all punctuation visible:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Post</font>
&nbsp;&nbsp;belongs_to(<font color="#99cc99">:author</font>, {<font color="#99cc99">:dependent</font>&nbsp;=&gt; <font color="#99cc99">:destroy</font>})
<font color="#96cbfe">end</font></pre>
<p>It&#8217;s not important that <tt>{:dependent => :destroy}</tt> is a hash. Nor is it important that <tt>belong_to</tt> is a method, and that it takes two arguments. It&#8217;s much more important that the post belongs to an author and that the post will be destroyed when the author is being destroyed.</p>
<p>By not using all the possible punctuation, it becomes easier for our brains to focus on <em>what</em> is actually written, rather that <em>which objects</em> might be used to express it.</p>
<h3>Few rules, many shortcuts</h3>
<p>There are only a few genuine language constructs. There are just three elements in the language that have any real meaning. Those are objects, methods and blocks/closures. The only way in which classes and modules are special is by their implementation, but they are still regular objects. There are just a few other constructs, most notably <tt>if</tt>, <tt>while</tt> and the logic operators like <tt>&#038;&#038;</tt> and <tt>||</tt>.</p>
<p>The rest of the keywords you see are just shortcuts, you can type to perform common tasks. Like defining a class:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Car</font>&nbsp;&lt; <font color="#ffffb6">Vehicle</font>
&nbsp;&nbsp;<font color="#7c7c7c"># ...</font>
<font color="#96cbfe">end</font></pre>
<p>The Ruby interpreter reads this as:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#ffffb6">Car</font>&nbsp;= <font color="#ffffb6">Class</font>.new(<font color="#ffffb6">Vehicle</font>) <font color="#6699cc">do</font>
&nbsp;&nbsp;<font color="#7c7c7c"># ...</font>
<font color="#6699cc">end</font></pre>
<p>Defining a class is actually just a method called on an object. The first argument is another object (the class to inherit from) and the second argument is a block. You can write it like this if you want to and play with it too, like dynamically making many different classes.</p>
<p>Some other examples:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black">counter += <font color="#ff73fd">1</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#7c7c7c"># this is a shortcut</font>
counter = counter + <font color="#ff73fd">1</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#7c7c7c"># without the shortcut, but also without punctuation</font>
counter = counter.+(<font color="#ff73fd">1</font>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#7c7c7c"># without the shortcut and with all punctuation (yes, + is just a method)</font></pre>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#c6c5fe">@user</font>&nbsp;||= <font color="#ffffb6">User</font>.new&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#7c7c7c"># this is called a 'teapot'-operator or 'or-or-equals'</font>
<font color="#c6c5fe">@user</font>&nbsp;= <font color="#c6c5fe">@user</font>&nbsp;|| <font color="#ffffb6">User</font>.new&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#7c7c7c"># assign @user to @user if it exists, otherwise create a new user</font></pre>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#6699cc">exit</font>&nbsp;<font color="#6699cc">unless</font>&nbsp;busy <font color="#6699cc">or</font>&nbsp;cancelled&nbsp;&nbsp;<font color="#7c7c7c"># 'unless' is a shortcut for 'if !()'</font>
<font color="#6699cc">exit</font>&nbsp;<font color="#6699cc">if</font>&nbsp;!(busy <font color="#6699cc">or</font>&nbsp;cancelled)&nbsp;&nbsp; <font color="#7c7c7c"># 'if' at the end of a sentence is also a shortcut</font>
<font color="#6699cc">if</font>&nbsp;!(busy <font color="#6699cc">or</font>&nbsp;cancelled)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#7c7c7c"># for a regular if</font>
&nbsp;&nbsp;<font color="#6699cc">exit</font>
<font color="#6699cc">end</font></pre>
<p>There are many more shortcuts. But why? Why are there so many ways to do the same thing? It&#8217;s not just readability, but also the ease in which you can translate the ideas in your head to code.</p>
<p>The smaller the difference between your thoughts and your code, the easier it is to program. Ruby gives you full freedom to express yourself how you feel fit. That doesn&#8217;t mean it always works out. If you have an unclear image of the problem you&#8217;re trying to solve, the code you write will be unclear too. Nothing stops you to write bad code. Ruby doesn&#8217;t force you in any way. These things cut both ways.</p>
<h3>Exposing intention</h3>
<p>Code needs to expose its intent. Ruby code is Rubyesque if it uses the above mentioned techniques to expose its intent. Because Ruby has such clean syntax, most DSLs (Domain Specific Languages) will be written in Ruby. These kinds of DSLs are called internal.</p>
<p>The naming of variables and methods is extremely important. The meaning of the code must be clear the first time you read it.</p>
<p>A fantastic example is <a href="http://rspec.info">Rspec</a>, my favorite test framework.</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black">describe <font color="#ffffb6">Post</font>&nbsp;<font color="#6699cc">do</font>

&nbsp;&nbsp;context <font color="#336633">&quot;</font><font color="#a8ff60">when it's not approved</font><font color="#336633">&quot;</font>&nbsp;<font color="#6699cc">do</font>
&nbsp;&nbsp;&nbsp;&nbsp;subject { <font color="#ffffb6">Post</font>.new(<font color="#99cc99">:approved</font>&nbsp;=&gt; <font color="#99cc99">false</font>) }
&nbsp;&nbsp;&nbsp;&nbsp;it { should_not be_publishable }
&nbsp;&nbsp;<font color="#6699cc">end</font>

&nbsp;&nbsp;context <font color="#336633">&quot;</font><font color="#a8ff60">when it's approved</font><font color="#336633">&quot;</font>&nbsp;<font color="#6699cc">do</font>
&nbsp;&nbsp;&nbsp;&nbsp;subject { <font color="#ffffb6">Post</font>.new(<font color="#99cc99">:approved</font>&nbsp;=&gt; <font color="#99cc99">true</font>) }
&nbsp;&nbsp;&nbsp;&nbsp;it { should be_publishable }
&nbsp;&nbsp;<font color="#6699cc">end</font>

<font color="#6699cc">end</font></pre>
<p>The entire Ruby bag of tricks is being used to make it as natural as possible to write down your specifications. This example doesn&#8217;t like like regular Ruby code, but it still is. It&#8217;s nothing more than objects, methods and blocks.</p>
<p>This is very Rubyesque. There is almost no distraction from the meaning of the code. I can plainly read if my code still does what my customer wants and I can run the specs to see if my code does what I specified it to do. And even nicer: it&#8217;s incredibly easy! Rspec makes Test Driven Development very easy.</p>
<p>If you&#8217;re interested in the code to implement this example, take a look at <a href="http://gist.github.com/362030">this gist</a>.
</p>
<h3>Succinct and Concise</h3>
<p>Rubyesque code is succinct. That doesn&#8217;t just mean short lines (I try to limit myself to about 80 or 100 characters per line), but also about short methods. The code smell detector <a href="http://wiki.github.com/kevinrutherford/reek/">Reek</a> will raise a warning if your method is longer than 6 lines. Does this means you are not allowed to write longer methods? Of course not. Is it a good idea? No.</p>
<p>People are better at understanding short sentences. And it&#8217;s the people that need to understand the code and maintain. And we&#8217;re back to the philosophy behind Ruby. It&#8217;s meant to be understandable for people. It&#8217;s less important if the computer needs to do some extra work.</p>
<h3>Conclusion</h3>
<p>Ruby is a free language. You can bend the rules. The goal is to make you happy. Ruby was made to make you happy, to close the gap between human and computer language. Try to do that in the code you write too, it works!</p>
<p>I found that most people coming from languages like Java or PHP find Ruby a weird or magical language. That is only the appearance. Ruby is very easy to learn. It has few rules. Ruby programmers don&#8217;t code using rigid rules and dogmas, but experiment solving the same problem in different ways.</p>
<p>I hope you got some idea of what it means to program Rubyesque. I advise everybody to look at the different languages and their philosophies. See what philosophy suits you. If the Ruby philosophy suits you and you have the discipline needed to write Ruby, give it a try! Ruby transformed the way I feel about my job and never regretted learning it one bit.</p>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/1nKqwqSLhzs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/04/quest-ce-que-cest-rubyesque/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/04/quest-ce-que-cest-rubyesque/</feedburner:origLink></item>
		<item>
		<title>Monkey Patch of the Month: attr_initializer</title>
		<link>http://feedproxy.google.com/~r/iain-nl/~3/2jo6ZI3BHtk/</link>
		<comments>http://iain.nl/2010/04/monkey-patch-of-the-month-attr_initializer/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 06:57:29 +0000</pubDate>
		<dc:creator>Iain Hecker</dc:creator>
				<category><![CDATA[iain.nl]]></category>

		<guid isPermaLink="false">http://iain.nl/?p=660</guid>
		<description><![CDATA[So, about one month ago, I promised to share some useful monkey patches every month. Here is the second one. Your own monkey patches are still more than welcome! I often find myself writing code like this: class&#160;Foo &#160;&#160;attr_reader&#160;:bar, :baz &#160;&#160;def&#160;initialize(bar, baz) &#160;&#160;&#160;&#160;@bar, @baz&#160;= bar, baz &#160;&#160;end end This can be very annoying to maintain. [...]]]></description>
			<content:encoded><![CDATA[<p>So, about one month ago, I <a href="http://iain.nl/2010/03/monkey-patch-of-the-month-group_by/">promised</a> to share some useful monkey patches every month. Here is the second one. Your own monkey patches are still more than welcome!</p>
<div id="attachment_676" class="wp-caption alignright" style="width: 310px"><a href="http://en.wikipedia.org/wiki/Common_Squirrel_Monkey"><img src="http://iain.nl/wp-content/uploads/2010/04/800px-Saimiri_sciureus-1_Luc_Viatour-300x222.jpg" alt="" title="800px-Saimiri_sciureus-1_Luc_Viatour" width="300" height="222" class="size-medium wp-image-676" /></a><p class="wp-caption-text">Common Squirrel Monkey (from Wikipedia)</p></div>
<p>I often find myself writing code like this:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Foo</font>
&nbsp;&nbsp;<font color="#6699cc">attr_reader</font>&nbsp;<font color="#99cc99">:bar</font>, <font color="#99cc99">:baz</font>
&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">initialize</font>(bar, baz)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#c6c5fe">@bar</font>, <font color="#c6c5fe">@baz</font>&nbsp;= bar, baz
&nbsp;&nbsp;<font color="#96cbfe">end</font>
<font color="#96cbfe">end</font></pre>
<p>This can be very annoying to maintain. The variable names are repeated four times, within three lines of code!</p>
<p>Ideally, I&#8217;d want to write something like this:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Foo</font>
&nbsp;&nbsp;attr_initializer <font color="#99cc99">:bar</font>, <font color="#99cc99">:baz</font>
<font color="#96cbfe">end</font></pre>
<p>Much better, if you ask me. Here is one example of how to do this.</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">Class</font>
&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">attr_initializer</font>(*attributes)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#6699cc">attr_reader</font>&nbsp;*attributes
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#6699cc">class_eval</font>&nbsp;&lt;&lt;-<font color="#336633">RUBY</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(</font><font color="#00a0a0">#{</font>attributes.join(<font color="#336633">'</font><font color="#a8ff60">, </font><font color="#336633">'</font>)<font color="#00a0a0">}</font><font color="#a8ff60">)</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#00a0a0">#{</font>attributes.map{ |<font color="#c6c5fe">attribute</font>|&nbsp;<font color="#336633">&quot;</font><font color="#a8ff60">@</font><font color="#00a0a0">#{</font>attribute<font color="#00a0a0">}</font><font color="#336633">&quot;</font>&nbsp;}.join(<font color="#336633">'</font><font color="#a8ff60">, </font><font color="#336633">'</font>)<font color="#00a0a0">}</font><font color="#a8ff60">&nbsp;= </font><font color="#00a0a0">#{</font>attributes.join(<font color="#336633">'</font><font color="#a8ff60">, </font><font color="#336633">'</font>)<font color="#00a0a0">}</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end</font>
<font color="#a8ff60">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#336633">RUBY</font>
&nbsp;&nbsp;<font color="#96cbfe">end</font>
<font color="#96cbfe">end</font></pre>
<p>No piece of code is complete without tests, so this is it:</p>
<pre style="background: #000000; color: #f6f3e8; font-family: Monaco, monospace" class="ir_black"><font color="#96cbfe">class</font>&nbsp;<font color="#ffffb6">AttrInitializerTests</font>&nbsp;&lt; <font color="#ffffb6">Test</font>::<font color="#ffffb6">Unit</font>::<font color="#ffffb6">TestCase</font>

&nbsp;&nbsp;<font color="#96cbfe">def</font>&nbsp;<font color="#ffd2a7">test_attr_initializer</font>
&nbsp;&nbsp;&nbsp;&nbsp;klass = <font color="#ffffb6">Class</font>.new <font color="#6699cc">do</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr_initializer <font color="#99cc99">:foo</font>, <font color="#99cc99">:bar</font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#6699cc">end</font>
&nbsp;&nbsp;&nbsp;&nbsp;object = klass.new(<font color="#ff73fd">1</font>, <font color="#336633">'</font><font color="#a8ff60">b</font><font color="#336633">'</font>)
&nbsp;&nbsp;&nbsp;&nbsp;assert_equal <font color="#ff73fd">1</font>, object.foo
&nbsp;&nbsp;&nbsp;&nbsp;assert_equal <font color="#336633">'</font><font color="#a8ff60">b</font><font color="#336633">'</font>, object.bar
&nbsp;&nbsp;<font color="#96cbfe">end</font>
&nbsp;
<font color="#96cbfe">end</font></pre>
<img src="http://feeds.feedburner.com/~r/iain-nl/~4/2jo6ZI3BHtk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.nl/2010/04/monkey-patch-of-the-month-attr_initializer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://iain.nl/2010/04/monkey-patch-of-the-month-attr_initializer/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.352 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-07-30 15:23:56 -->
