<?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>Simone Carletti&#039;s Blog &#187; Programming</title>
	<atom:link href="http://www.simonecarletti.com/blog/tags/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.simonecarletti.com/blog</link>
	<description>Simone Carletti&#039;s personal ramblings on programming, syndication, search engines &#38; marketing.</description>
	<lastBuildDate>Tue, 07 Feb 2012 08:48:33 +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>Eloquent Ruby Book Review</title>
		<link>http://www.simonecarletti.com/blog/2011/10/eloquent-ruby/</link>
		<comments>http://www.simonecarletti.com/blog/2011/10/eloquent-ruby/#comments</comments>
		<pubDate>Mon, 03 Oct 2011 09:49:25 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[reviews]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1522</guid>
		<description><![CDATA[Eloquent Ruby by Russ Olsen is a book about the Ruby programming language that focuses on Ruby programming style by teaching you how to write your code as a real Rubyist.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.simonecarletti.com/blog/out/amzcom-eloquentruby"><img class="alignleft" title="Book Eloquent Ruby" src="http://share.weppos.net/simonecarletti/book-addison-eloquentruby-20110930-194837.png" alt="Book Eloquent Ruby" height="250" /></a> <a href="http://www.simonecarletti.com/blog/out/amzcom-eloquentruby"><em>Eloquent Ruby</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-eloquentruby">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-eloquentruby">UK</a>) by Russ Olsen is a book about the Ruby programming language that focuses on <strong>Ruby programming style</strong> by teaching you how to write your code as a real Rubyist.</p>
<p>This is the second Olsen&#8217;s book about Ruby. In 2007 he published <a title="Design Patterns in Ruby Book Review" href="http://www.simonecarletti.com/blog/2009/03/book-review-design-patterns-in-ruby/">Design Patterns in Ruby</a>, an other great example of how common programming practices like Design Patterns apply to the Ruby world.</p>
<p>Eloquent Ruby is a very lightweight and pleasant reading. The colloquial tone is friendly and engrossing. The books has plenty of code snippets and it requires only a few days to read it from start to end.</p>
<p>Be prepared, <strong>this is an unconventional Ruby book</strong>. To use author&#8217;s words</p>
<blockquote><p>This is a book about making that final leap, about absorbing the Ruby programming culture, about becoming truly fluent in Ruby.</p></blockquote>
<p>This is something I have been claiming for the last 5 years. Learning Ruby is not difficult, start thinking in Ruby and becoming a Rubyist is the real challenge.</p>
<blockquote><p>A formal understanding of the mechanics of Ruby isn&#8217;t the same as really looking at the programming world through Ruby-colored glasses. You need to absorb the cultural part of Ruby, to see how real Rubyist use the language to solve problems.</p></blockquote>
<p>This is one of the reasons that convinced me to <a href="http://www.simonecarletti.com/blog/2011/09/the-rubyist/">create the Rubyist</a>.</p>
<h2><span id="more-1522"></span>Structure</h2>
<p>The book is divided into 4 parts:</p>
<ol>
<li>The Basics</li>
<li>Classes, Modules, and Blocks</li>
<li>Metaprogramming</li>
<li>Pulling It All Together</li>
</ol>
<p>Each part is divided into chapters. The book counts 31 chapters and about 400 pages.</p>
<p>The first part covers some <strong>basic Ruby features</strong> in a way you normally won&#8217;t read in any other Ruby reference. Ah, if I only had the Symbol chapter available when I started learning Ruby!</p>
<p>The second part covers <strong>Classes, Modules and Blocks</strong> and it explains how to use modules successfully, how to deal with inheritance, equality and operators. This is by far my most favorite section.</p>
<p>The third part is about <strong>Metaprogramming</strong>. Whilst the name of the section is technically correct, it can be misleading. If you are looking for a complete Ruby and Metaprogramming course, check out <a href="http://www.simonecarletti.com/blog/out/amzcom-metaprogrammingruby"><em>Metaprogramming Ruby</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-metaprogrammingruby">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-metaprogrammingruby">UK</a>). This section covers common Ruby metaprogramming topic such as hooks, method_missing (a must read!) and monkey patching.</p>
<p>The forth part wraps several topics all together and talks about <strong>creating and implementing a DSL</strong> in Ruby.</p>
<p>The book ends with a <strong>rich list of books about Ruby and programming in general</strong>. The list contains amazing Ruby titles like <a href="http://www.simonecarletti.com/blog/out/amzcom-rubybestpractices"><em>Ruby Best Practices</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-rubybestpractices">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-rubybestpractices">UK</a>) or <a href="http://www.simonecarletti.com/blog/out/amzcom-rubyway"><em>The Ruby Way Second Edition</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-rubyway">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-rubyway">UK</a>), as well programming masterpieces like <a href="http://www.simonecarletti.com/blog/out/amzcom-elprogstyle"><em>The Elements of Programming Style</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-elprogstyle">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-elprogstyle">UK</a>). If you are looking for some inspiration about your next reading, you might probably find some there.</p>
<h3>Chapters</h3>
<ol>
<li>Write Code That Looks Like Ruby</li>
<li>Choose the Right Control Structure</li>
<li>Take Advantage of Ruby&#8217;s Smart Collections</li>
<li>Take Advantage of Ruby&#8217;s Smart Strings</li>
<li>Find The Right String With Regular Expressions</li>
<li>Use Symbols to Stand for Something</li>
<li>Treat Everything Like an Object &#8211; Because Everything Is</li>
<li>Embrace Dynamic Typing</li>
<li>Write Specs!</li>
<li>Construct Your Classes from Short, Focused Methods</li>
<li>Define Operators Respectfully</li>
<li>Create Classes That Understand Equality</li>
<li>Get The Behavior You Need with Singleton and Class Methods</li>
<li>Use Class Instance Variables</li>
<li>Use Modules as Name spaces</li>
<li>Use Modules as Mixins</li>
<li>Use Blocks to Iterate</li>
<li>Execute Around with a Block</li>
<li>Save Blocks to Execute Later</li>
<li>Use Hooks to Keep Your Program Informed</li>
<li>Use method_missing for Flexible Error Handling</li>
<li>Use method_missing for Delegation</li>
<li>Use method_missing to Build Flexible APIs</li>
<li>Update Existing Classes with Monkey Patching</li>
<li>Create Self-Modifying Classes</li>
<li>Create Classes That Modify Their Subclasses</li>
<li>Invent Internal DSLs</li>
<li>Build External DSLs for Flexible Syntax</li>
<li>Package Your Programs as Gems</li>
<li>Know Your Ruby Implementation</li>
<li>Keep An Open Mind to Go With Those Open Classes</li>
</ol>
<h2>Requirements</h2>
<p>This book assumes that you have a basic knowledge of the Ruby language. You don&#8217;t need to be a Ruby master, but some advanced sections such as Metaprogramming and DSL may require you to stop for a moment and refresh or improve your specific knowledge of Ruby on that topic.</p>
<p>Don&#8217;t expect this book to explain you the basic details of Ruby or its syntax, this is behind the scope of this publication. There are plenty of commented examples, but if you want to learn about a specific Ruby feature make sure you keep a reference like <a href="http://www.simonecarletti.com/blog/out/amzcom-programmingruby"><em>Programming Ruby</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-programmingruby">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-programmingruby">UK</a>) or <a href="http://www.simonecarletti.com/blog/out/amzcom-rubyway"><em>The Ruby Way</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-rubyway">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-rubyway">UK</a>) handy.</p>
<p>Another requirement is that you must be ready to read printed source code: this book is full or Ruby code. At least the 50% of the pages contain code, making this book a valuable practical reference.</p>
<h2>Plus</h2>
<p>Aside from being an excellent resource to help you thinking Ruby and programming in the Ruby way, this book constantly adopts a practical approach providing tons of examples to read. Every chapter ends with an <em>In the Wild</em> section containing examples extracted from real Ruby libraries, and a <em>Wrapping up</em> section that helps you to fix the concepts in mind.</p>
<p>As as long time Ruby developer I really loved the <em>Use Symbols to Stand for Something</em>, object and block chapters. Some of them didn&#8217;t necessary teach anything new, but the way used to present the concepts was very helpful and understandable.</p>
<p>I appreciate the focus on tests and the RSpec chapter. The most part of Ruby books dedicate a specific section about tests, then seem to forget them for the rest of the chapters. This is not the case of this book, where several examples are verified by tests.</p>
<p>Finally, the simple colloquial tone makes the book really friendly. It seems like a person you always worked with is now teaching you programming and thinking in the Ruby way.</p>
<h2>Minus</h2>
<p>It&#8217;s really hard to find some real negative in this book.</p>
<p>I found the Regular Expression chapter pretty boring and misplaced. In fact, it was the only chapter in the book where the main focus was teaching Regular Expression basics, instead of focusing on using Regular Expressions in the right way.</p>
<p>The same applies to the RubyGems sections. There have been several changes in the Gems community in the last years and the chapter appears to be slightly outdated. Again, I would have left this topic outside the book.</p>
<p>Finally, I would have appreciated a wider usage of Ruby 1.9 over 1.8 in order to discourage the adoption of Ruby 1.8.</p>
<h2>In Conclusion</h2>
<p>If you are a beginner to intermediate level Ruby programmer, this book is a must read that it&#8217;s likely to help you improving your Ruby skill and writing code in the Ruby way.</p>
<p>If you are completely new to Ruby, personally don&#8217;t recommend this book as a first reading. You might want to start with a Ruby reference to learn how programming in Ruby, then read Eloquent Ruby to learn how programming in the Ruby way. Also, don&#8217;t forget about <a href="http://rubyist.info/">the Rubyist</a>, you&#8217;ll find several Ruby common practices and coding conventions.</p>
<p>If you are a Ruby expert and you have been writing Ruby for the last 5 years, don&#8217;t be too self-confident. I&#8217;m quite sure the book will be able to provide you some valuable advice more than once.</p>
<h2>Where to Find Eloquent Ruby</h2>
<p>You can get a printed or Kindle copy at <a href="http://www.simonecarletti.com/blog/out/amzcom-eloquentruby">Amazon.com</a>, <a href="http://www.simonecarletti.com/blog/out/amzuk-eloquentruby">Amazon.co.uk</a>, or at your other favorite book seller.</p>
<h3>Kindle Preview</h3>
<div id="kindle-reader">.</div>
<p><script type="text/javascript" src="http://kindleweb.s3.amazonaws.com/app/KindleReader-min.js"></script><script type="text/javascript">// <![CDATA[
                            KindleReader.LoadSample({containerID: 'kindle-reader', asin: 'B004MMEJ36', width: '600', height: '599', assoctag: 'simonecarletti-20'});
// ]]&gt;</script></p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2009/03/book-review-design-patterns-in-ruby/' rel='bookmark' title='Design Patterns in Ruby Book Review'>Design Patterns in Ruby Book Review</a></li>
<li><a href='http://www.simonecarletti.com/blog/2010/03/book-review-learning-jquery/' rel='bookmark' title='Learning jQuery 1.3 Book Review'>Learning jQuery 1.3 Book Review</a></li>
<li><a href='http://www.simonecarletti.com/blog/2011/03/book-review-getting-real/' rel='bookmark' title='Getting Real Book Review'>Getting Real Book Review</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/10/eloquent-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>the Rubyist</title>
		<link>http://www.simonecarletti.com/blog/2011/09/the-rubyist/</link>
		<comments>http://www.simonecarletti.com/blog/2011/09/the-rubyist/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 14:42:39 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1479</guid>
		<description><![CDATA[the Rubyist is a collection of conventions and standards for Ruby programmers.]]></description>
			<content:encoded><![CDATA[<p>Several years ago I started keeping track of Ruby coding conventions that my team and I were using in-house. The purpose was to document the most important guidelines we employ in order to keep our libraries and source code consistent, regardless the maintainer.</p>
<p>The result of this effort is <a title="the Rubyist" href="http://rubyist.info/">the Rubyist</a>, a collection of conventions and standards for Ruby programmers. The project is now <a href="http://github.com/weppos/rubyist">open source</a>, you can fork it and contribute.</p>
<div class="wp-caption aligncenter" style="width: 510px"><img title="the Rubyist Homepage" src="http://share.weppos.net/simonecarletti/skitched-20110907-164017.png" alt="the Rubyist Homepage" width="500" height="195" /><p class="wp-caption-text">the Rubyist Homepage</p></div>
<p><span id="more-1479"></span>When I first started learning Ruby, several years ago, there was a very nice project called <strong>RubyGarden</strong>. This project was a real treasure for those learning Ruby because it was full of resources and information about the Ruby world. If you have been developing in Ruby for a long time, I&#8217;m sure you&#8217;ll remember it.</p>
<p>But most of all, the RubyGarden has been extremely helpful to me (and many others) in learning about Ruby conventions and the most notable Ruby best practices. One page in particular, the <strong>RubyStyleGuide</strong>, was a collection of Ruby coding standard and naming conventions that every Ruby developer ought to have been familiar with.</p>
<div class="wp-caption aligncenter" style="width: 510px"><img title="RubyGarde Ruby Style Guide " src="http://share.weppos.net/simonecarletti/skitched-20110907-163554.png" alt="" width="500" height="379" /><p class="wp-caption-text">RubyGarde Ruby Style Guide</p></div>
<p>A lot has changed since then.</p>
<p>The RubyGarden project was shut down a few years ago. As far I remember, the wiki was invaded by spam and the <code>www.rubygarden.org</code> domain has now been hijacked.</p>
<p>The Ruby language has evolved. Ruby 1.8 and Ruby 1.9 have introduced several new syntax changes and conventions. Moreover, the adoption of the Ruby programming language has increased over the years and several new common programming patterns have arisen.</p>
<p><a href="https://github.com/">GitHub</a> was born. If you are in the habit of <a href="http://www.simonecarletti.com/blog/2009/09/inside-ruby-on-rails-reading-source-code/">read the source code</a>, GitHub is like a chocolate factory for a child. You can find an countless number of Ruby projects, filter them, sort them and read the source code to learn from it. Just pick some random accounts owned by notable Ruby developers, explore their programming habits and learn new exciting best practices and patterns.</p>
<p>Even with these newly available resources, there are many times when an quick reference can come in most handy.</p>
<p>Whether you are a Ruby master or a newbie, I hope you&#8217;ll find <a href="rubyist.info">the Rubyist</a> to be a source of inspiration.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/09/the-rubyist/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Understanding Ruby and Rails: Lazy load hooks</title>
		<link>http://www.simonecarletti.com/blog/2011/04/understanding-ruby-and-rails-lazy-load-hooks/</link>
		<comments>http://www.simonecarletti.com/blog/2011/04/understanding-ruby-and-rails-lazy-load-hooks/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 08:22:54 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[lazy-load]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1426</guid>
		<description><![CDATA[With Rails 3 you can now register specific hooks to be lazy-executed when the corresponding library is loaded.]]></description>
			<content:encoded><![CDATA[<div class="flash-message info target-rails">
<p>This article targets <strong>Rails 3</strong>. The information contained in this page might not apply to different versions.</p>
</div>
<p><em>This is article is part of my series Understanding Ruby and Rails. Please see the <a href="http://www.simonecarletti.com/blog/2009/09/inside-ruby-on-rails-reading-source-code/">table of contents</a> for the series to view the list of all posts.</em></p>
<p>A small-but-interesting feature introduced in <strong>Rails 3</strong> is the built-in support for <strong>lazy loading</strong>.</p>
<p>Lazy loading is a very common <strong>design pattern</strong>. The concept is to defer initialization of an object until the point at which it is needed. This design pattern decreases the time required by an application to boot by distributing the computation cost during the execution. Also, if a specific feature is never used, the computation won&#8217;t be executed at all.</p>
<p>With Rails 3 you can now register specific hooks to be lazy-executed when the corresponding library is loaded.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span><br />
<br />
&nbsp; initializer <span style="color:#996600;">&quot;active_record.include_plugins&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; ActiveSupport.<span style="color:#9900CC;">on_load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active_record</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">MyApp::ActivePlugins</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>In this case we register the block to be executed when the <code>ActiveRecord</code> library is loaded. If you read the <a href="https://github.com/rails/rails/blob/v3.0.6/activerecord/lib/active_record/base.rb">ActiveRecord::Base source code</a>, the very last line is a call to</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ActiveSupport.<span style="color:#9900CC;">run_load_hooks</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active_record</span>, <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div>
<p>This line of code executes all the hooks previously registered for <code>ActiveRecord</code>.<span id="more-1426"></span></p>
<h2>lazy-load hooks in the wild</h2>
<p>Perhaps one of the most frequent usage of the lazy-load hooks is in Rails plugins.</p>
<p>For example, if your plugin needs to register some helpers, you can write a hook to include the helpers in <code>ActionView</code> only when <code>ActionView</code> is loaded. If the environment is loaded from a rake task (which doesn&#8217;t necessary need <code>ActionView</code>), then your plugin hook won&#8217;t be executed and the Rails application will boot faster.</p>
<p>Here&#8217;s an example from the <code>will_paginate</code> gem.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'will_paginate'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'will_paginate/collection'</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">module</span> WillPaginate<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">class</span> Railtie <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Rails::Railtie</span><br />
&nbsp; &nbsp; initializer <span style="color:#996600;">&quot;will_paginate.active_record&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>app<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:active_record</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'will_paginate/finders/active_record'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#6666ff; font-weight:bold;">WillPaginate::Finders::ActiveRecord</span>.<span style="color:#9900CC;">enable</span>!<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; initializer <span style="color:#996600;">&quot;will_paginate.action_dispatch&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>app<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:action_controller</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#6666ff; font-weight:bold;">ActionDispatch::ShowExceptions</span>.<span style="color:#9900CC;">rescue_responses</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'WillPaginate::InvalidPage'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#ff3333; font-weight:bold;">:not_found</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; initializer <span style="color:#996600;">&quot;will_paginate.action_view&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>app<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:action_view</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'will_paginate/view_helpers/action_view'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">WillPaginate::ViewHelpers::ActionView</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<h2>Using lazy load in your libraries</h2>
<p>So far, we only discussed about using lazy-loading to hook Rails core library. Because lazy-loading is an <code>ActiveSupport</code> feature, you can use it in your Rails applications but also in your own Ruby classes.</p>
<p>First, make sure to add a call to <code>ActiveSupport.run_load_hooks</code> at the end of your Ruby class.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> HttpClient<br />
&nbsp; <span style="color:#008000; font-style:italic;"># ...</span><br />
<br />
&nbsp; ActiveSupport.<span style="color:#9900CC;">run_load_hooks</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:http_client</span>, <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Now you can register on_load hooks everywhere passing the name of the library used in <code>run_load_hooks</code>.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Request<br />
&nbsp; <span style="color:#008000; font-style:italic;"># ...</span><br />
<br />
&nbsp; ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:http_client</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># do something</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Response<br />
&nbsp; <span style="color:#008000; font-style:italic;"># ...</span><br />
<br />
&nbsp; ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:http_client</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># do something</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<h2>Hook context</h2>
<p>The <code>run_load_hooks</code> method takes a second parameter representing the context within the hook will be executed. It&#8217;s a common pattern to pass the class/instance the hooks refer to.</p>
<p>For instance, the <code>ActiveRecord</code> library mentioned at the beginning of the article passes <code>self</code>. In that context, <code>self</code> references the <code>ActiveRecord::Base</code> class.</p>
<p>This is useful because, if you want to include some custom methods into ActiveRecord, you can use the following block</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:active_record</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">MyPlugin::Extensions</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>instead of</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:active_record</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">send</span> :<span style="color:#9966CC; font-weight:bold;">include</span>, <span style="color:#6666ff; font-weight:bold;">MyPlugin::Extensions</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>There&#8217;s also an other interesting use case for this feature. If you want to perform some kind of lazy-initialization when an instance of a class is created, just pass the instance itself.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Color<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@name</span> = name<br />
<br />
&nbsp; &nbsp; ActiveSupport.<span style="color:#9900CC;">run_load_hooks</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:instance_of_color</span>, <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
ActiveSupport.<span style="color:#9900CC;">on_load</span> <span style="color:#ff3333; font-weight:bold;">:instance_of_color</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;The color is #{@name}&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
Color.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;yellow&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; &quot;The color is yellow&quot;</span></div></td></tr></tbody></table></div>
<h2>Source Code</h2>
<p>The source code of the lazy-loading feature is available in the <a href="https://github.com/rails/rails/blob/v3.0.6/activesupport/lib/active_support/lazy_load_hooks.rb">lazy_load_hooks.rb</a> file.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2009/09/inside-ruby-on-rails-reading-source-code/' rel='bookmark' title='Understanding Ruby and Rails: Reading Source Code'>Understanding Ruby and Rails: Reading Source Code</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/12/inside-ruby-on-rails-rescuable-and-rescue_from/' rel='bookmark' title='Understanding Ruby and Rails: Rescuable and rescue_from'>Understanding Ruby and Rails: Rescuable and rescue_from</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/09/inside-ruby-on-rails-extract_options-from-arrays/' rel='bookmark' title='Understanding Ruby and Rails: extract_options! from Arrays'>Understanding Ruby and Rails: extract_options! from Arrays</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/04/understanding-ruby-and-rails-lazy-load-hooks/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Getting Real Book Review</title>
		<link>http://www.simonecarletti.com/blog/2011/03/book-review-getting-real/</link>
		<comments>http://www.simonecarletti.com/blog/2011/03/book-review-getting-real/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 10:02:27 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[37signals]]></category>
		<category><![CDATA[reviews]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1296</guid>
		<description><![CDATA[Getting Real is the first book published by 37signals, containing ideas, suggestions and examples to help you building effective software.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.simonecarletti.com/blog/out/amzcom-gettingreal"><img class="alignleft" title="Book Getting Real" src="http://share.weppos.net/simonecarletti/book-gettingreal-20110930-193119.png" alt="Book Getting Real" width="166" height="250" /></a><a href="http://www.simonecarletti.com/blog/out/amzcom-gettingreal"><em>Getting Real</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-gettingreal">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-gettingreal">UK</a>) is the first book published by <a href="http://37signals.com/">37signals</a>, the famous company behind successful web applications such as <strong>Basecamp</strong> and <strong>Highrise</strong>, other than the company that created the Ruby on Rails framework.</p>
<p><em>Getting Real</em> was published in 2006 but if you start reading it today, you will probably find it more relevant than several other programming and development books written in the last two months. This is because <em>Getting Real</em> is not about a language or technology. It isn&#8217;t about the latest cutting-edge development framework. <em>Getting Real</em> describes a way of thinking, describes an effective approach ideally suited to develop web based software.</p>
<p>To describe the book with the authors&#8217; words</p>
<blockquote><p><em>Getting Real</em> is about skipping all the stuff that <em>represents</em> real and <em>actually building the real things</em>.</p>
<p><em>Getting Real</em> is less. Less mass, less software, less of everything that&#8217;s not essential.</p>
<p><em>Getting Real</em> is about iterations and lowering the cost of change.</p></blockquote>
<p><span id="more-1296"></span></p>
<h2>Structure</h2>
<p>The book is <strong>organized in 16 chapters</strong>, <strong>each one containing several essays</strong>.</p>
<p>Here&#8217;s the table of content:</p>
<ol>
<li>Introduction</li>
<li>The Starting Line</li>
<li>Stay Lean</li>
<li>Priorities</li>
<li>Feature Selection</li>
<li>Process</li>
<li>The Organization</li>
<li>Staffing</li>
<li>Interface Design</li>
<li>Code</li>
<li>Words</li>
<li>Pricing and Signup</li>
<li>Promotion</li>
<li>Support</li>
<li>Post-Launch</li>
<li>Conclusion</li>
</ol>
<p>If you want to get a preview of the content or read it online, <a href="http://gettingreal.37signals.com/toc.php">the complete book is available online for free</a>.</p>
<h3>Essays</h3>
<p>An essay is usually no more than 2 pages long. <strong>The essay size is one of the best aspects of the book</strong>. Keeping essays short actually make it possible to read them very quickly. You can easily schedule your reading. Do you have only 15 minutes available? Open the book, pick the next essay and changes are you will finish it in 10 minutes.</p>
<p>Short chapters help you to stay focused on the content. When it takes 30 minutes or longer to read a chapter, you might lose the concentration. How many times in the middle of a chapter and did you realize you didn&#8217;t remember what was written at the beginning of it?</p>
<p>Essays contain ideas, suggestions and examples to help you <em>Getting Real</em>. <strong>The book focus is on building software and web applications</strong>, but reading the book you will immediately realize that several topics are applicable to non-software activities too.</p>
<p>As the authors say, <em>this book doesn&#8217;t have to be an all or nothing proposition. Even if you can&#8217;t embrace Getting Real fully, there are bound to be at least a few ideas in here you can sneak past the powers that be</em>.</p>
<h2>Who should read this book?</h2>
<p>So, is <em>Getting Real</em> for you? For me, <strong><em>Getting Real</em> is one of the best books I ever read about development methodologies</strong> since a long time. When I was reading the book, I had the same awesome feeling I first noticed when I read The Pragmatic Programmers book.</p>
<p>As an entrepreneur and the co-founder of RoboDomain, <em>Getting Real</em> has been a valuable resource. I discovered that several techniques and processes we applied to RoboDomain are proven to be successful and there&#8217;s still plenty of new things to be done to improve ourself and our product.</p>
<p><em>Getting Real</em> is not the kind of book you can read and say &#8220;from tomorrow we have to start following all these guidelines&#8221;. You need to be ready to acknowledge the ideas it contains.</p>
<p>If you ever developed a web application or if you&#8217;re going to develop one. If you are an entrepreneur, designer or programmer. If concepts like <strong>versioning</strong>, <strong>agile development</strong>, <strong>iterations</strong>, <strong>continuous integration</strong>, <strong>testing</strong> and <strong>customer satisfaction</strong> mean something for you, then you will probably appreciate <em>Getting Real</em>.</p>
<p>And if you love meetings, spend several time writing specs, work several months before having a working version of your product, <em>Getting Real</em> might be a good book for, you provided that you read it by trying to understand why a specific approach has proven to be successful, rather than assuming this is just hot air.</p>
<p><span style="font-size: 20px; font-weight: bold;">In conclusion</span></p>
<p>As you might understand, I really enjoyed this book and <strong>I strongly recommend it</strong>.</p>
<p>I want to close this book review by quoting a couple of sentences that, in my opinion, better represent <em><em>Getting Real</em></em>.</p>
<blockquote>
<h4>Don&#8217;t be a yes-man</h4>
<p>Each time you say yes to a feature, you&#8217;re adopting a child. You have to take your baby through a whole chain of events (e.g. design, implementation, testing, etc.). And once that feature&#8217;s out there, you&#8217;re stuck with it. Just try to take a released feature away from customers and see how pissed off they get.</p></blockquote>
<blockquote>
<h4>Pay off your code and design &#8220;bills&#8221;</h4>
<p>We usually think of debt in terms of money but it comes in other forms too. You can easily build up code and design debt. Hack together some bad code that&#8217;s functional but still a bit hairy and you&#8217;re building up debt.</p>
<p>The same way you should regularly put aside some of your income for taxes, regularly put aside some time to pay off your code and design debt. If you don&#8217;t, you&#8217;ll just be paying interest (fixing hacks) instead of paying down the principal (and moving forward).</p></blockquote>
<h2>Where to Find Getting Real</h2>
<p>You can get a printed copy at <a href="http://www.simonecarletti.com/blog/out/amzcom-gettingreal">Amazon.com</a>, <a href="http://www.simonecarletti.com/blog/out/amzuk-gettingreal">Amazon.co.uk</a>, or at your other favorite book seller. The complete book <a href="http://gettingreal.37signals.com/toc.php">is also available online for free</a>.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2009/03/book-review-design-patterns-in-ruby/' rel='bookmark' title='Design Patterns in Ruby Book Review'>Design Patterns in Ruby Book Review</a></li>
<li><a href='http://www.simonecarletti.com/blog/2010/03/book-review-learning-jquery/' rel='bookmark' title='Learning jQuery 1.3 Book Review'>Learning jQuery 1.3 Book Review</a></li>
<li><a href='http://www.simonecarletti.com/blog/2011/10/eloquent-ruby/' rel='bookmark' title='Eloquent Ruby Book Review'>Eloquent Ruby Book Review</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/03/book-review-getting-real/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New in Whois: improved caching</title>
		<link>http://www.simonecarletti.com/blog/2011/02/new-in-whois-improved-caching/</link>
		<comments>http://www.simonecarletti.com/blog/2011/02/new-in-whois-improved-caching/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 11:15:45 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubywhois]]></category>
		<category><![CDATA[whois]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1279</guid>
		<description><![CDATA[Today, I'm very happy to report that this week I introduced a completely new caching system for the Whois::Answer::Parser.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working very hard to include some of the most important features in the new version of the <a title="Ruby Whois - Ruby Whois Gem" href="http://www.ruby-whois.org/">Ruby Whois library</a>.</p>
<p>Today, I&#8217;m very happy to report that this week I closed the <a href="https://github.com/weppos/whois/issues/18">issue #18</a> which introduces a completely new caching system for the <code>Whois::Answer::Parser</code>.</p>
<p>The way <code>Whois</code> parsers currently work, is to extract a property only the very first time it is requested.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">r = Whois.<span style="color:#9900CC;">query</span> <span style="color:#996600;">&quot;weppos.it&quot;</span><br />
<br />
<span style="color:#008000; font-style:italic;"># the property has never been requested before</span><br />
<span style="color:#008000; font-style:italic;"># the value is computed, cached and returned</span><br />
r.<span style="color:#9900CC;">status</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; :ok</span><br />
<br />
<span style="color:#008000; font-style:italic;"># the property has been requested before</span><br />
<span style="color:#008000; font-style:italic;"># the value is returned without further elaborations</span><br />
r.<span style="color:#9900CC;">status</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; :ok</span></div></td></tr></tbody></table></div>
<p>So far, so good.</p>
<p>The way the system works under the hood, is to create a parser instance variable for every single requested property.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">r = Whois.<span style="color:#9900CC;">query</span> <span style="color:#996600;">&quot;weppos.it&quot;</span><br />
<br />
<span style="color:#008000; font-style:italic;"># get the first parser</span><br />
<span style="color:#008000; font-style:italic;"># because</span><br />
<span style="color:#008000; font-style:italic;"># &nbsp; r.status relies on</span><br />
<span style="color:#008000; font-style:italic;"># a</span><br />
<span style="color:#008000; font-style:italic;"># &nbsp; r.parser.parsers.first.status</span><br />
<span style="color:#CC0066; font-weight:bold;">p</span> = r.<span style="color:#9900CC;">parser</span>.<span style="color:#9900CC;">parsers</span>.<span style="color:#9900CC;">first</span><br />
<br />
<span style="color:#008000; font-style:italic;"># value is not cached</span><br />
<span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">instance_variable_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@status&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; nil</span><br />
<br />
<span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">status</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; :ok</span><br />
<br />
<span style="color:#008000; font-style:italic;"># value is cached</span><br />
<span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">instance_variable_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@status&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; :ok</span></div></td></tr></tbody></table></div>
<p>So far, quite good. This approach has a couple of drawbacks.</p>
<p><span id="more-1279"></span>First, it creates an instance variable for every single property. Because of the large (and increasing) <a href="http://www.ruby-whois.org/manual/parser-properties.html">number of properties</a>, the parser object space counts a large number of instance variables. This makes it hard, for instance, to sweep the cache because you have to loop through all instance variables and remove each one.</p>
<p>Second, there&#8217;s a small inefficiency here. Because in Ruby you don&#8217;t have to define variables, an undefined instance variable is <code>nil</code>. But <code>nil</code> is actually a value and properties can have a <code>nil</code> value. In this implementation, you don&#8217;t have a way to distinguish when a value is nil and when it hasn&#8217;t been elaborated yet, thus <code>nil</code> properties will never hit the cache.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">def</span> created_on<br />
&nbsp; <span style="color:#0066ff; font-weight:bold;">@created_on</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#9966CC; font-weight:bold;">if</span> very_expensive_scan<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>created_on<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># ... set the value</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#008000; font-style:italic;"># calling #created_on several times will continue to perform</span><br />
<span style="color:#008000; font-style:italic;"># the very_expensive_scan as long as the value != nil.</span></div></td></tr></tbody></table></div>
<p>The new approach uses a single instance variable called <code>@cached_properties</code> as cache. The variable contains a <code>Hash&lt;:key =&gt; value&gt;</code>, where the key is the property and the value the cached result.</p>
<p>If the cache doesn&#8217;t contain any key for given property, then the method hasn&#8217;t been executed yet. Cached <code>nil</code> properties will return <code>nil</code> without further elaboration.</p>
<p>The method <code>#cached_properties_fetch</code> takes care of everything.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">def</span> created_on<br />
&nbsp; cached_properties_fetch<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:created_on</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#008000; font-style:italic;"># value is cached and returned</span><br />
created_on<br />
<span style="color:#008000; font-style:italic;"># =&gt; nil</span><br />
<br />
<span style="color:#008000; font-style:italic;"># the request hits the cache</span><br />
created_on<br />
<span style="color:#008000; font-style:italic;"># =&gt; nil</span></div></td></tr></tbody></table></div>
<p>If you need to sweep the cache, reset <code>@cached_properties</code> to an empty <code>Hash</code>.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2010/01/new-in-whois-property-is-set/' rel='bookmark' title='New in Whois: property is set?'>New in Whois: property is set?</a></li>
<li><a href='http://www.simonecarletti.com/blog/2010/01/new-in-whois-principle-of-least-surprise/' rel='bookmark' title='New in Whois: Applying the Principle of Least Surprise'>New in Whois: Applying the Principle of Least Surprise</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/10/ruby-whois-0-8-1/' rel='bookmark' title='Ruby Whois 0.8.1'>Ruby Whois 0.8.1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/02/new-in-whois-improved-caching/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New in Whois: Gem Testers</title>
		<link>http://www.simonecarletti.com/blog/2011/02/new-in-whois-gem-testers/</link>
		<comments>http://www.simonecarletti.com/blog/2011/02/new-in-whois-gem-testers/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 16:18:37 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubywhois]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1283</guid>
		<description><![CDATA[The new Whois version now includes support for gem test.]]></description>
			<content:encoded><![CDATA[<p>At the beginning of February, <a title="Introducing Gem Testers | Engine Yard Ruby on Rails Blog" href="http://www.engineyard.com/blog/2011/introducing-gem-testers/">Engine Yard announced</a> a very interesting project called <a title="Gem Testers" href="http://gem-testers.org/">Gem Testers</a>.</p>
<blockquote><p>Gem Testers was built to make it easier for gem developers to write gems that run everywhere. This is split into two pieces:</p>
<ol>
<li><a title="rubygems-test | RubyGems.org | your community gem host" href="http://rubygems.org/gems/rubygems-test">rubygems-test</a> is a Rubygems plugin which automatically runs the test suite of a specified gem on install.</li>
<li><a title="Gem Testers" href="http://gem-testers.org/">Gem-testers.org</a> is a central repository for test data. Test results are organized primarily by Ruby version and operating system.</li>
</ol>
</blockquote>
<p>I believe this is a wonderful project and it can have a very good impact on the quality of the available Ruby gems.</p>
<p><a href="http://www.ruby-whois.org/">Whois</a> currently counts <strong>211 RSpec examples</strong> and <strong>1899 tests</strong> with <strong>8674 assertions</strong>. This is a very huge test suite which is able to provide an immediate feedback on existing bugs.</p>
<p><span id="more-1283"></span>The new Whois version now includes support for <code>gem test</code>. You can decide to run the tests automatically when installing the gem or run them later by executing</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>gem <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #c20cb9; font-weight: bold;">whois</span></div></td></tr></tbody></table></div>
<p>Read <a href="https://github.com/rubygems/rubygems-test/blob/master/README.txt">rubygems-test README</a> for further information.</p>
<p>Unfortunately, this feature forced me to re-include all the test fixtures and files into the packaged <code>.gem</code>. It means the size of <code>.gem</code> file will be bigger than previous releases.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2011/03/whois-2-0/' rel='bookmark' title='Whois 2.0'>Whois 2.0</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/08/ruby-has-a-new-whois-library/' rel='bookmark' title='Ruby has a new WHOIS library'>Ruby has a new WHOIS library</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/09/ruby-whois-preview-answer-and-parser/' rel='bookmark' title='Ruby Whois preview: WHOIS answer and parser'>Ruby Whois preview: WHOIS answer and parser</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/02/new-in-whois-gem-testers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TabsOnRails 2.0.0</title>
		<link>http://www.simonecarletti.com/blog/2011/02/tabsonrails-2-0-0/</link>
		<comments>http://www.simonecarletti.com/blog/2011/02/tabsonrails-2-0-0/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 10:56:46 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tabs on rails]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1270</guid>
		<description><![CDATA[TabsOnRails 2.0.0 is now available. This release targets Rails 3.0 and is no longer compatible con Rails 2.3.x.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.simonecarletti.com/code/tabs_on_rails">TabsOnRails</a> is a simple Rails plugin for creating tabs and navigation menus. It provides helpers for generating navigation menus with a flexible interface.</p>
<p>Release 2.0.0 is now available. It targets Rails 3.0 and is no longer compatible con Rails 2.3.x.</p>
<p>You can <a href="http://rubygems.org/gems/tabs_on_rails">download/install the Gem via RubyGems</a>.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>gem <span style="color: #c20cb9; font-weight: bold;">install</span> tabs_on_rails</div></td></tr></tbody></table></div>
<p>For more details please read the <a href="http://www.simonecarletti.com/code/tabs_on_rails/docs.html">documentation</a> or <a href="http://www.simonecarletti.com/code/tabs_on_rails/api/">API</a>.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2009/05/tabsonrails-030-with-concurrent-tabs-support-aka-namespaces/' rel='bookmark' title='TabsOnRails 0.3.0 with concurrent tabs support aka namespaces'>TabsOnRails 0.3.0 with concurrent tabs support aka namespaces</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/06/tabsonrails-0-8-0/' rel='bookmark' title='TabsOnRails 0.8.0'>TabsOnRails 0.8.0</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/10/tabsonrails-graduated-from-beta/' rel='bookmark' title='TabsOnRails graduated from beta'>TabsOnRails graduated from beta</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/02/tabsonrails-2-0-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to restart God when you deploy a new release via Capistrano</title>
		<link>http://www.simonecarletti.com/blog/2011/02/how-to-restart-god-when-you-deploy-a-new-release/</link>
		<comments>http://www.simonecarletti.com/blog/2011/02/how-to-restart-god-when-you-deploy-a-new-release/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 12:37:15 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[god]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1232</guid>
		<description><![CDATA[How to restart God monitored processes when you deploy a new Rails release via Capistrano.]]></description>
			<content:encoded><![CDATA[<p class="jump-to-solution">I don&#8217;t have time to read and I want to <a href="#the-solution">jump immediately to the solution →</a>.</p>
<p>Like the 90% of Rails developers, I use Capistrano to manage the deployment of my Rails applications. I also use <a title="God" href="http://god.rubyforge.org/">God</a> to monitor several application processes, such as <a title="delayed_job" href="http://rubygems.org/gems/delayed_job">DelayedJob</a> workers.</p>
<h2>The problem</h2>
<p>Each time I execute a deployment, I need the restart all the Rails instances, including rake tasks and workers, otherwise they will continue to run with the previous (old) application version.</p>
<p>I cannot simply send a command to God via Capistrano because God is omnipotent and runs with <code>root</code> privileges, while Capistrano doesn&#8217;t (for security reasons I don&#8217;t deploy with a sudoer user).</p>
<p>Here comes the problem. How to restart God when you deploy a new release? I already discussed this problem in the past and the solution I adopted since today was to <a title="Capistrano: Executing a command as root without using sudo  –  Simone Carletti's Blog" href="http://www.simonecarletti.com/blog/2010/07/capistrano-executing-a-command-as-root-without-using-sudo/">execute a command as root without using sudo</a>. The problem with this solution, is that it requires you to type the root password each time you execute a new deploy. And this is extremely awkward.</p>
<p>Last week I was configuring a new <code>Rails</code> application and I decided to investigate a new solution. I really like the way how Phusion Passenger works: whenever you touch the <code>/tmp/restart.txt</code> file, Passenger restarts all the Rails processes.</p>
<p>Why not implementing the same behavior in God? Luckily, <a href="https://groups.google.com/d/msg/god-rb/qViXZHQMhiM/cjBFo-oM6NgJ">someone else already had the same idea</a> before.<span id="more-1232"></span></p>
<h2 id="#the-solution">The solution</h2>
<p>I found <a href="https://gist.github.com/769766">the following recipe on Gist</a>.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">module</span> God<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">module</span> Conditions<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">class</span> RestartFileTouched <span style="color:#006600; font-weight:bold;">&lt;</span> PollCondition<br />
&nbsp; &nbsp; &nbsp; attr_accessor <span style="color:#ff3333; font-weight:bold;">:restart_file</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">super</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> process_start_time<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">`ps -o lstart &nbsp;-p #{self.watch.pid} --no-heading`</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> restart_file_modification_time<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">mtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">restart_file</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> valid?<br />
&nbsp; &nbsp; &nbsp; &nbsp; valid = <span style="color:#0000FF; font-weight:bold;">true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; valid <span style="color:#006600; font-weight:bold;">&amp;</span>= complain<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Attribute 'restart_file' must be specified&quot;</span>, <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">restart_file</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?<br />
&nbsp; &nbsp; &nbsp; &nbsp; valid<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> test<br />
&nbsp; &nbsp; &nbsp; &nbsp; process_start_time <span style="color:#006600; font-weight:bold;">&lt;</span> restart_file_modification_time<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Save the code in your God application or on the server and make sure God loads it. My suggestion is to add the following line in the God configuration script (e.g. <code>/etc/god/config</code>)</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># Load in all God shared configs.</span><br />
<span style="color:#008000; font-style:italic;"># Share configs apply to all instances.</span><br />
God.<span style="color:#CC0066; font-weight:bold;">load</span> <span style="color:#996600;">&quot;/etc/god/conf.d/*.god&quot;</span></div></td></tr></tbody></table></div>
<p>and save the file in the <code>/etc/god/conf.d</code> folder. In this way, when you&#8217;ll start God with</p>
<div class="codecolorer-container text default code-bash" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ god -c /etc/god/config</div></td></tr></tbody></table></div>
<p>all the shared recipes will be loaded.</p>
<p>Then, configure the monitored process to watch the restart file. The configuration is available as a <a title="Module: God::Conditions" href="http://rubydoc.info/gems/god/0.11.0/God/Conditions">state transition condition</a>.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rails_root = <span style="color:#996600;">&quot;/path/to/rails&quot;</span><br />
<br />
restart.<span style="color:#9900CC;">condition</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:restart_file_touched</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; c.<span style="color:#9900CC;">interval</span> = <span style="color:#006666;">5</span>.<span style="color:#9900CC;">seconds</span><br />
&nbsp; c.<span style="color:#9900CC;">restart_file</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>rails_root, <span style="color:#996600;">'tmp'</span>, <span style="color:#996600;">'restart.txt'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Taking the <a href="https://github.com/blog/229-dj-god">GitHub DelayedJob recipe as example</a>, you should change</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># restart if memory gets too high</span><br />
w.<span style="color:#9900CC;">transition</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:up</span>, <span style="color:#ff3333; font-weight:bold;">:restart</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>on<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; on.<span style="color:#9900CC;">condition</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:memory_usage</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; c.<span style="color:#9900CC;">above</span> = <span style="color:#006666;">300</span>.<span style="color:#9900CC;">megabytes</span><br />
&nbsp; &nbsp; c.<span style="color:#9900CC;">times</span> = <span style="color:#006666;">2</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>to</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># restart if memory gets too high</span><br />
w.<span style="color:#9900CC;">transition</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:up</span>, <span style="color:#ff3333; font-weight:bold;">:restart</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>on<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; on.<span style="color:#9900CC;">condition</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:memory_usage</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; c.<span style="color:#9900CC;">above</span> = <span style="color:#006666;">300</span>.<span style="color:#9900CC;">megabytes</span><br />
&nbsp; &nbsp; c.<span style="color:#9900CC;">times</span> = <span style="color:#006666;">2</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; on.<span style="color:#9900CC;">condition</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:restart_file_touched</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; c.<span style="color:#9900CC;">interval</span> = <span style="color:#006666;">5</span>.<span style="color:#9900CC;">seconds</span><br />
&nbsp; &nbsp; c.<span style="color:#9900CC;">restart_file</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>rails_root, <span style="color:#996600;">'tmp'</span>, <span style="color:#996600;">'restart.txt'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>The <code>restart_file</code> can be whichever file you want. I strongly encourage to use the same <code>/tmp/restart.txt</code> Passenger file, in this way your processes will restart each time you restart the application.</p>
<p>Thanks to <a href="https://github.com/amedeo">Amedeo</a> for his help and thanks to <a href="https://github.com/nathanhumbert">Nathan Humbert</a> for creating the original God configuration.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2010/07/capistrano-executing-a-command-as-root-without-using-sudo/' rel='bookmark' title='Capistrano: Executing a command as root without using sudo'>Capistrano: Executing a command as root without using sudo</a></li>
<li><a href='http://www.simonecarletti.com/blog/2008/12/capistrano-deploy-recipe-with-passenger-mod_rails-taste/' rel='bookmark' title='Running Capistrano with Passenger (mod_rails)'>Running Capistrano with Passenger (mod_rails)</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/06/capistrano-and-database-yml/' rel='bookmark' title='Capistrano and database.yml'>Capistrano and database.yml</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/02/how-to-restart-god-when-you-deploy-a-new-release/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Get the currently described class in RSpec</title>
		<link>http://www.simonecarletti.com/blog/2010/12/get-the-currently-described-class-in-rspec/</link>
		<comments>http://www.simonecarletti.com/blog/2010/12/get-the-currently-described-class-in-rspec/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 15:18:59 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1204</guid>
		<description><![CDATA[How to get the currently described class in RSpec.]]></description>
			<content:encoded><![CDATA[<p>If you <a href="https://github.com/weppos/whois/blob/9ba4f16d204ae23236c3228a8662826994722597/spec/whois/client_spec.rb">look at my RSpec</a>, you will probably notice a wide usage of a <code>klass</code> method.</p>
<div class="codecolorer-container text default code-ruby" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe Whois::Client do<br />
&nbsp; context &quot;#initialize&quot; do<br />
&nbsp; &nbsp; it &quot;accepts a zero parameters&quot; do<br />
&nbsp; &nbsp; &nbsp; lambda { klass.new }.should_not raise_error<br />
&nbsp; &nbsp; end<br />
<br />
&nbsp; &nbsp; it &quot;accepts a timeout setting with a value in seconds&quot; do<br />
&nbsp; &nbsp; &nbsp; client = klass.new(:timeout =&gt; 100)<br />
&nbsp; &nbsp; &nbsp; client.timeout.should == 100<br />
&nbsp; &nbsp; end<br />
&nbsp; end<br />
end</div></td></tr></tbody></table></div>
<p>The <code>klass</code> method references the currently described object, in the case above <code>Whois::Client</code>.</p>
<p>Here&#8217;s the method definition.</p>
<div class="codecolorer-container text default code-ruby" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># /spec/support/helpers.rb<br />
module SupportHelpers<br />
&nbsp; # Gets the currently described class.<br />
&nbsp; # Conversely to +subject+, it returns the class<br />
&nbsp; # instead of an instance.<br />
&nbsp; def klass<br />
&nbsp; &nbsp; described_class<br />
&nbsp; end<br />
end<br />
<br />
RSpec.configure do |config|<br />
&nbsp; config.include SupportHelpers<br />
end</div></td></tr></tbody></table></div>
<p>I normally save the definition in a file, and put the file into the <code>/specs/support</code> folder (which is automatically loaded in RSpec 2).</p>
<p>The <code>klass</code> method is very convenient helper to keep your specs cleaned and flexible, specially when using long namespaces.</p>
<p><span id="more-1204"></span>Before that, I was used to define a <code>@klass</code> instance variable at the beginning of my specs/tests. It worked, but wasn&#8217;t really that clean.</p>
<div class="codecolorer-container text default code-ruby" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe Whois::Client do<br />
&nbsp; before(:each) do<br />
&nbsp; &nbsp; @klass = Whois::Client<br />
&nbsp; end<br />
<br />
&nbsp; context &quot;#initialize&quot; do<br />
&nbsp; &nbsp; it &quot;accepts a zero parameters&quot; do<br />
&nbsp; &nbsp; &nbsp; lambda { @klass.new }.should_not raise_error<br />
&nbsp; &nbsp; end<br />
&nbsp; end<br />
end</div></td></tr></tbody></table></div>
<div class="codecolorer-container text default code-ruby" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">class Whois::ClientTest &lt; Test::Unit::TestCase<br />
&nbsp; def setup<br />
&nbsp; &nbsp; @klass = Whois::Client<br />
&nbsp; end<br />
<br />
&nbsp; def test_initialize_with_zero_parameters<br />
&nbsp; &nbsp; assert_nothing_raised { @klass.new }<br />
&nbsp; end<br />
end</div></td></tr></tbody></table></div>
<p>I&#8217;m very grateful to RSpec because it seems to adapt perfectly to any developers&#8217; need.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2011/04/rspec-rails-doesnt-render-rails-views-by-default/' rel='bookmark' title='RSpec Rails doesn&#8217;t render Rails views by default'>RSpec Rails doesn&#8217;t render Rails views by default</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/08/ruby-has-a-new-whois-library/' rel='bookmark' title='Ruby has a new WHOIS library'>Ruby has a new WHOIS library</a></li>
<li><a href='http://www.simonecarletti.com/blog/2010/02/new-in-whois-command-line-improvements/' rel='bookmark' title='New in Whois: command line improvements'>New in Whois: command line improvements</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2010/12/get-the-currently-described-class-in-rspec/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Capistrano: Executing a command as root without using sudo</title>
		<link>http://www.simonecarletti.com/blog/2010/07/capistrano-executing-a-command-as-root-without-using-sudo/</link>
		<comments>http://www.simonecarletti.com/blog/2010/07/capistrano-executing-a-command-as-root-without-using-sudo/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 11:20:40 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[su]]></category>
		<category><![CDATA[sudo]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1150</guid>
		<description><![CDATA[How to run a command in Capistrano as a root without using sudo.]]></description>
			<content:encoded><![CDATA[<p class="jump-to-solution">I don&#8217;t have time to read and I want to <a href="#the-solution">jump immediately to the solution →</a>.</p>
<h2>The problem</h2>
<p><img class="alignleft size-full wp-image-374" title="Capistrano Logo" src="http://www.simonecarletti.com/blog/wp-content/uploads/2009/06/capistrano-logo.png" alt="Capistrano Logo" width="150" height="57" />From the day I started using <a title="delayed_job | RubyGems.org | your community gem host" href="http://rubygems.org/gems/delayed_job">DelayedJob</a> in combination with <a title="God - A Process Monitoring Framework in Ruby" href="http://god.rubyforge.org/">God</a>, I faced the problem t<strong>o restart the <code>god</code> group every time I deployed a new application release</strong>.</p>
<p>In fact, when a rake task (God runs DelayedJob using the rake <code>jobs:work</code> task, <a title="dj.god - GitHub" href="http://github.com/blog/229-dj-god">here&#8217;s the config</a>) is started, it loads the entire Rails environment and keeps it in memory as long as the process is running. When you deploy a new version, if you don&#8217;t restart the rake task, DelayedJob continues to run with the previous Rails version environment.</p>
<p>To restart a task/group in God:</p>
<div class="codecolorer-container text default code-bash" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ god restart GROUP_NAME</div></td></tr></tbody></table></div>
<p>Speaking about my servers, the problem is that God runs as root while Capistrano deploys new releases using a not-root not-sudo user, for security purposes. In practice, it means I can&#8217;t use the <a title="Sudo - Capistrano" href="http://www.capify.org/index.php/Sudo">Capistrano <code>sudo</code></a> command because the deploy user is not in the sudoers list. Fortunately, the solution exists and is to <a title="Configure sudo to ask for root password - Server Fault" href="http://serverfault.com/questions/103932/configure-sudo-to-ask-for-root-password/103959#103959">run the command as <code>su -c &lt;command&gt;</code></a>.</p>
<p>The gotcha here is that when you use <code>su</code> you have to handle the remote server root password prompt. Thanks to Capistrano, this is as easy as pie.<span id="more-1150"></span></p>
<p>Here&#8217;s the method I created to run the command.</p>
<h2 id="the-solution">The solution</h2>
<div class="codecolorer-container text default code-ruby;" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># Runs +command+ as root invoking the command with su -c<br />
# and handling the root password prompt.<br />
#<br />
# &nbsp; surun &quot;/etc/init.d/apache reload&quot;<br />
# &nbsp; # Executes<br />
# &nbsp; # su - -c '/etc/init.d/apache reload'<br />
#<br />
def surun(command)<br />
&nbsp; password = fetch(:root_password, Capistrano::CLI.password_prompt(&quot;root password: &quot;))<br />
&nbsp; run(&quot;su - -c '#{command}'&quot;) do |channel, stream, output|<br />
&nbsp; &nbsp; channel.send_data(&quot;#{password}n&quot;) if output<br />
&nbsp; end<br />
end</div></td></tr></tbody></table></div>
<p>And here&#8217;s the final Capistrano/DelayedJob/Run code fragment.</p>
<div class="codecolorer-container text default brush: ruby;" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">namespace :god do<br />
&nbsp; desc &quot;Restart the god group 'GROUP_NAME'&quot;<br />
&nbsp; task :restart, :roles =&gt; :app do<br />
&nbsp; &nbsp; surun &quot;god restart GROUP_NAME&quot;<br />
&nbsp; end<br />
end<br />
<br />
after &quot;deploy:restart&quot;, &quot;delayed_job:restart&quot;</div></td></tr></tbody></table></div>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2011/02/how-to-restart-god-when-you-deploy-a-new-release/' rel='bookmark' title='How to restart God when you deploy a new release via Capistrano'>How to restart God when you deploy a new release via Capistrano</a></li>
<li><a href='http://www.simonecarletti.com/blog/2008/12/capistrano-deploy-recipe-with-passenger-mod_rails-taste/' rel='bookmark' title='Running Capistrano with Passenger (mod_rails)'>Running Capistrano with Passenger (mod_rails)</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/06/capistrano-and-database-yml/' rel='bookmark' title='Capistrano and database.yml'>Capistrano and database.yml</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2010/07/capistrano-executing-a-command-as-root-without-using-sudo/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

