<?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/categories/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>Heroku and Rails 3.2 asset:precompile error</title>
		<link>http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/</link>
		<comments>http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 21:34:00 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails 3.2]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1575</guid>
		<description><![CDATA[How to configure your Rails 3.2 application to successfully deploy it to Heroku.]]></description>
			<content:encoded><![CDATA[<p>I have a very simple Rails 3.1 application, deployed on Heroku. Just <strong>after upgrading it to Rails 3.2, the deploy to Heroku stopped working properly</strong>.</p>
<p>More specifically, the rake <code>asset:precompile</code> task was failing during slug compilation.</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 />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 /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp;Your bundle is <span style="color: #7a0874; font-weight: bold;">complete</span><span style="color: #000000; font-weight: bold;">!</span> It was installed into .<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>bundle<br />
&nbsp; &nbsp; &nbsp; &nbsp;Cleaning up the bundler cache.<br />
-----<span style="color: #000000; font-weight: bold;">&gt;</span> Writing config<span style="color: #000000; font-weight: bold;">/</span>database.yml to <span style="color: #c20cb9; font-weight: bold;">read</span> from DATABASE_URL<br />
-----<span style="color: #000000; font-weight: bold;">&gt;</span> Preparing app <span style="color: #000000; font-weight: bold;">for</span> Rails asset pipeline<br />
&nbsp; &nbsp; &nbsp; &nbsp;Running: rake assets:precompile<br />
&nbsp; &nbsp; &nbsp; &nbsp;rake aborted<span style="color: #000000; font-weight: bold;">!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;could not connect to server: Connection refused<br />
&nbsp; &nbsp; &nbsp; &nbsp;Is the server running on host <span style="color: #ff0000;">&quot;127.0.0.1&quot;</span> and accepting<br />
&nbsp; &nbsp; &nbsp; &nbsp;TCP<span style="color: #000000; font-weight: bold;">/</span>IP connections on port <span style="color: #000000;">5432</span>?<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;Tasks: TOP =<span style="color: #000000; font-weight: bold;">&gt;</span> environment<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #7a0874; font-weight: bold;">&#40;</span>See full trace by running task with --trace<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;Precompiling assets failed, enabling runtime asset compilation<br />
&nbsp; &nbsp; &nbsp; &nbsp;Injecting rails31_enable_runtime_asset_compilation<br />
&nbsp; &nbsp; &nbsp; &nbsp;Please see this article <span style="color: #000000; font-weight: bold;">for</span> troubleshooting help:<br />
&nbsp; &nbsp; &nbsp; &nbsp;http:<span style="color: #000000; font-weight: bold;">//</span>devcenter.heroku.com<span style="color: #000000; font-weight: bold;">/</span>articles<span style="color: #000000; font-weight: bold;">/</span>rails31_heroku_cedar<span style="color: #666666; font-style: italic;">#troubleshooting</span></div></td></tr></tbody></table></div>
<p>The Heroku <a href="http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting">assets:precompile toubleshooting page</a> contains several explanations, but none of these was my case. In fact, my application was working properly with Rails 3.1.<span id="more-1575"></span></p>
<p>It turns out there are some changes in the Rails 3.2 initialization process that conflicts with Heroku slug compilation. The solution is very simple. All you have to do is to set the Rails 3.2 <code>initialize_on_precompile</code> configuration to false in your <code>application.rb</code> file.</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">config.assets.initialize_on_precompile = false</div></td></tr></tbody></table></div>
<p>This option is new in Rails 3.2 and prevents the Rails environment to be loaded when the <code>assets:precompile</code> task is executed. Because Heroku precompile assets before setting the database configuration, you need to set this configuration to false or you Rails application will try to connect to an unexisting database.</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 />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp;Your bundle is <span style="color: #7a0874; font-weight: bold;">complete</span><span style="color: #000000; font-weight: bold;">!</span> It was installed into .<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>bundle<br />
&nbsp; &nbsp; &nbsp; &nbsp;Cleaning up the bundler cache.<br />
-----<span style="color: #000000; font-weight: bold;">&gt;</span> Writing config<span style="color: #000000; font-weight: bold;">/</span>database.yml to <span style="color: #c20cb9; font-weight: bold;">read</span> from DATABASE_URL<br />
-----<span style="color: #000000; font-weight: bold;">&gt;</span> Preparing app <span style="color: #000000; font-weight: bold;">for</span> Rails asset pipeline<br />
&nbsp; &nbsp; &nbsp; &nbsp;Running: rake assets:precompile<br />
-----<span style="color: #000000; font-weight: bold;">&gt;</span> Rails plugin injection<br />
&nbsp; &nbsp; &nbsp; &nbsp;Injecting rails_log_stdout<br />
&nbsp; &nbsp; &nbsp; &nbsp;Injecting rails3_serve_static_assets<br />
...</div></td></tr></tbody></table></div>
<p>This configuration is also documented in the <a href="http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets">Precompiling Assets section</a> of the new Rails 3.2 asset pipeline guide.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2011/09/using-sprockets-without-a-railsrack-project/' rel='bookmark' title='Using Sprockets without a Rails/Rack project'>Using Sprockets without a Rails/Rack project</a></li>
<li><a href='http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/' rel='bookmark' title='Configuring Rails 3 to use HTTPS and SSL'>Configuring Rails 3 to use HTTPS and SSL</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/01/vmware-fusion-the-network-bridge-device-on-devvmnet0-is-not-running/' rel='bookmark' title='VMware Fusion &#8220;The network bridge device on /dev/vmnet0 is not running&#8221; error'>VMware Fusion &#8220;The network bridge device on /dev/vmnet0 is not running&#8221; error</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>Using Sprockets without a Rails/Rack project</title>
		<link>http://www.simonecarletti.com/blog/2011/09/using-sprockets-without-a-railsrack-project/</link>
		<comments>http://www.simonecarletti.com/blog/2011/09/using-sprockets-without-a-railsrack-project/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 15:28:56 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sprockets]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1485</guid>
		<description><![CDATA[An example of using Sprockets to preprocess and bundle CSS and JavaScript files without a Rails application.]]></description>
			<content:encoded><![CDATA[<p><a href="http://getsprockets.org/">Sprockets</a> is a Ruby library for compiling and serving web assets. It features declarative dependency management for JavaScript and CSS assets and a vert powerful preprocessor pipeline that allows you to write assets in languages like CoffeeScript, Sass or SCSS.</p>
<p>Sprockets is now integrated in Rails 3.1 and is the core library behind the new <strong>Rails 3.1 asset pipeline</strong>. What about if you want to use Sprockets outside a Rails project?</p>
<p>Sprockets exposes a very powerful Rack interface to serve assets over HTTP. <a href="http://www.metabates.com/2011/08/31/using-sprockets-without-rails/">Integrating Sprockets in a Rack application</a>, such as a Sinatra project, turns out to be a very straightforward task.</p>
<p>But in my case, I wanted to use Sprockets preprocessing and bundling feature outside an HTTP application. And it turned out Sprockets is very good at doing this as well.</p>
<p><span id="more-1485"></span>I have a custom shared template I created several months ago called docss. I use this for several projects such as <a href="http://www.ruby-whois.org/">Ruby Whois</a> and <a href="http://www.robowhois.com/">RoboWhois</a>. The template is composed of several CSS files. I have a Ruby <code>rake</code> script that merges these files and packages them into a single asset, then compress it and publish the result to <a href="http://aws.amazon.com/cloudfront/">Amazon CloudFront</a>.</p>
<p>Instead of processing and merging the files using file system tasks I&#8217;m now delegating this task to Sprockets. The project directory structure looks like this</p>
<div class="codecolorer-container bash 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="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">.<br />
├── Gemfile<br />
├── Gemfile.lock<br />
├── Rakefile<br />
├── build<br />
│   ├── javascripts<br />
│   │   └── all.js<br />
│   └── stylesheets<br />
│   &nbsp; &nbsp; ├── <span style="color: #000000;">960</span>.css<br />
│   &nbsp; &nbsp; ├── alignments.css<br />
│   &nbsp; &nbsp; ├── all.css<br />
│   &nbsp; &nbsp; ├── reset.css<br />
│   &nbsp; &nbsp; ├── screen.css<br />
│   &nbsp; &nbsp; └── syntax.css<br />
├── lib<br />
│   └── yuicompressor-2.4.2.jar<br />
└── src<br />
&nbsp; &nbsp; ├── javascripts<br />
&nbsp; &nbsp; │   └── all.js<br />
&nbsp; &nbsp; └── stylesheets<br />
&nbsp; &nbsp; &nbsp; &nbsp; ├── <span style="color: #000000;">960</span>.css<br />
&nbsp; &nbsp; &nbsp; &nbsp; ├── alignments.css<br />
&nbsp; &nbsp; &nbsp; &nbsp; ├── all.css<br />
&nbsp; &nbsp; &nbsp; &nbsp; ├── reset.css<br />
&nbsp; &nbsp; &nbsp; &nbsp; ├── screen.css.scss<br />
&nbsp; &nbsp; &nbsp; &nbsp; └── syntax.css</div></td></tr></tbody></table></div>
<p>And here&#8217;s my <code>compile</code> task</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 />29<br />30<br />31<br />32<br />33<br />34<br />35<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;">'rubygems'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'bundler'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pathname'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'logger'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fileutils'</span><br />
<br />
Bundler.<span style="color:#CC0066; font-weight:bold;">require</span><br />
<br />
ROOT &nbsp; &nbsp; &nbsp; &nbsp;= <span style="color:#CC00FF; font-weight:bold;">Pathname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
LOGGER &nbsp; &nbsp; &nbsp;= <span style="color:#CC00FF; font-weight:bold;">Logger</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>STDOUT<span style="color:#006600; font-weight:bold;">&#41;</span><br />
BUNDLES &nbsp; &nbsp; = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span> all.<span style="color:#9900CC;">css</span> all.<span style="color:#9900CC;">js</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
BUILD_DIR &nbsp; = ROOT.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;build&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
SOURCE_DIR &nbsp;= ROOT.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;src&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
task <span style="color:#ff3333; font-weight:bold;">:compile</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:cleanup</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; sprockets = <span style="color:#6666ff; font-weight:bold;">Sprockets::Environment</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>ROOT<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>env<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; env.<span style="color:#9900CC;">logger</span> = LOGGER<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; sprockets.<span style="color:#9900CC;">append_path</span><span style="color:#006600; font-weight:bold;">&#40;</span>SOURCE_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'javascripts'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; sprockets.<span style="color:#9900CC;">append_path</span><span style="color:#006600; font-weight:bold;">&#40;</span>SOURCE_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'stylesheets'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; BUNDLES.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>bundle<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; assets = sprockets.<span style="color:#9900CC;">find_asset</span><span style="color:#006600; font-weight:bold;">&#40;</span>bundle<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; prefix, basename = assets.<span style="color:#9900CC;">pathname</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2</span>..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">mkpath</span> BUILD_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>prefix<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; assets.<span style="color:#9900CC;">write_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>BUILD_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>prefix, basename<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; assets.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>asset<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># strip filename.css.foo.bar.css multiple extensions</span><br />
&nbsp; &nbsp; &nbsp; realname = asset.<span style="color:#9900CC;">pathname</span>.<span style="color:#9900CC;">basename</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>..<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; asset.<span style="color:#9900CC;">write_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>BUILD_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>prefix, realname<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</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>First I create a new <code>Sprockets::Environment</code> instance passing some configurations, such as a custom logger.</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">sprockets = <span style="color:#6666ff; font-weight:bold;">Sprockets::Environment</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>ROOT<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>env<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; env.<span style="color:#9900CC;">logger</span> = LOGGER<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Then I append the asset paths.</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 /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sprockets.<span style="color:#9900CC;">append_path</span><span style="color:#006600; font-weight:bold;">&#40;</span>SOURCE_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'javascripts'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
sprockets.<span style="color:#9900CC;">append_path</span><span style="color:#006600; font-weight:bold;">&#40;</span>SOURCE_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'stylesheets'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div>
<p>Finally, I process and package the assets to the build directory.</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">assets.<span style="color:#9900CC;">write_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>BUILD_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>prefix, basename<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div>
<p>Because sometimes I need to reference a single CSS file (e.g. <code>alignments.css</code>) instead of the entire bundle, I also build a standalone package for each CSS source. You might not want to do that.</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">assets.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>asset<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># strip filename.css.foo.bar.css multiple extensions</span><br />
&nbsp; realname = asset.<span style="color:#9900CC;">pathname</span>.<span style="color:#9900CC;">basename</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>..<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; asset.<span style="color:#9900CC;">write_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>BUILD_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>prefix, realname<span style="color:#006600; font-weight:bold;">&#41;</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>Please keep in mind that if you want to use processors you must include the corresponding libraries. For example, if you want to support <code>SASS</code> processor you need to add the <code>sass</code> gem.</p>
<p>Here&#8217;s my <code>Gemfile</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 /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">source <span style="color:#996600;">&quot;http://rubygems.org&quot;</span><br />
<br />
gem <span style="color:#996600;">'sass'</span><br />
gem <span style="color:#996600;">'sprockets'</span></div></td></tr></tbody></table></div>
<p>That&#8217;s all.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/' rel='bookmark' title='Heroku and Rails 3.2 asset:precompile error'>Heroku and Rails 3.2 asset:precompile error</a></li>
<li><a href='http://www.simonecarletti.com/blog/2010/07/the-way-to-rails-3/' rel='bookmark' title='The Road to Rails 3: make your Rails 2.3 project more Rails 3 oriented'>The Road to Rails 3: make your Rails 2.3 project more Rails 3 oriented</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/09/using-sprockets-without-a-railsrack-project/feed/</wfw:commentRss>
		<slash:comments>4</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>Configuring Rails 3 to use HTTPS and SSL</title>
		<link>http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/</link>
		<comments>http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/#comments</comments>
		<pubDate>Thu, 05 May 2011 10:45:53 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails 3]]></category>
		<category><![CDATA[rails 3.1]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1447</guid>
		<description><![CDATA[Ways to force your Rails application to use SSL and the HTTPS protocol.]]></description>
			<content:encoded><![CDATA[<div class="flash-message info target-rails">
<p>This article targets <strong>Rails 3</strong> and <strong>Rails 3.1</strong>. The information contained in this page might not apply to different versions.</p>
</div>
<p>There are several ways to force your Rails application to use SSL and the HTTPS protocol.<span id="more-1447"></span></p>
<h2>Rails &gt;= 3.1</h2>
<p>If you are using Rails 3.1 (currently available in beta1) or greater, <a href="https://github.com/rails/rails/commit/2c0c4d754e34b13379dfc53121a970c25fab5dae">this commit</a> makes it incredibly easy to switch from HTTP/HTTPS and vice-versa.</p>
<p>Simply use <code>config.force_ssl = true</code> in your environment configuration.</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"><span style="color:#008000; font-style:italic;"># config/application.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">module</span> MyApp<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">class</span> Application <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Rails::Application</span><br />
&nbsp; &nbsp; config.<span style="color:#9900CC;">force_ssl</span> = <span style="color:#0000FF; font-weight:bold;">true</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>You can also selectively enable https depending on the current Rails environment. For example, you might want to keep HTTPS turned off on development, and enable it on staging/production.</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"><span style="color:#008000; font-style:italic;"># config/application.rb</span><br />
<span style="color:#9966CC; font-weight:bold;">module</span> MyApp<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">class</span> Application <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Rails::Application</span><br />
&nbsp; &nbsp; config.<span style="color:#9900CC;">force_ssl</span> = <span style="color:#0000FF; font-weight:bold;">false</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;"># config/environments/production.rb</span><br />
<span style="color:#6666ff; font-weight:bold;">MyApp::Application</span>.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; config.<span style="color:#9900CC;">force_ssl</span> = <span style="color:#0000FF; font-weight:bold;">true</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>Behind the scenes, Rails adds the awesome <a href="https://rubygems.org/gems/rack-ssl">Rack::SSL Rack middleware</a> to your application middleware stack. <code>Rack::SSL</code> automatically filters the request, redirects not-HTTPS requests to the corresponding HTTPS path and applies some additional improvements to make sure your HTTPS request is secure.</p>
<h2>Rails &lt; 3.1</h2>
<p>If you&#8217;re not using Rails 3.1 don&#8217;t worry. Enabling HTTPS is as easy as adding the following line to your environment configuration.</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">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_before</span> <span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, <span style="color:#996600;">&quot;Rack::SSL&quot;</span></div></td></tr></tbody></table></div>
<p>Note that I&#8217;m passing <code>Rack::SSL</code> as string to delegate the loading of the class at the end of the Rails application initialization. Also note the middleware must be inserted in a specific position in the stack, at least before <code>ActionDispatch::Static</code> and <code>ActionDispatch::Cookies</code>.</p>
<p>Don&#8217;t forget to define <code>Rack::SSL</code> dependency in your <code>Gemfile</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 /></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;"># Gemfile</span><br />
gem <span style="color:#996600;">'rack-ssl'</span>, :<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'rack/ssl'</span></div></td></tr></tbody></table></div>
<h2>Enabling HTTPS and HTTP in parallel</h2>
<p>Transitioning an HTTP-based application to HTTPS might not be as easy as you think, especially if your application embeds assets from third-party hosts.</p>
<p>There are cases where you want to make your application available using both HTTP and HTTPS. For instance, we adopted this strategy in <a title="The easier way to track your domains" href="https://www.robodomain.com/">RoboDomain</a> for a while when we <a title="Going SSL - RoboBlog" href="http://blog.robodomain.com/post/3581778301/going-ssl">moved the application to HTTPS</a>.</p>
<p><code>Rack::SSL</code> has a very interesting and undocumented feature. You can pass an <code>:exclude</code> option to determine when to enable/disable the use of HTTPS.</p>
<p>The following code enables Rack::SSL and all its filters only in case the request comes from a HTTPS connection.</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">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_before</span> <span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, <span style="color:#6666ff; font-weight:bold;">Rack::SSL</span>, <span style="color:#ff3333; font-weight:bold;">:exclude</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>env<span style="color:#006600; font-weight:bold;">|</span> env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'HTTPS'</span><span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#996600;">'on'</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>Both the following URLs will continue to work, but the first one will trigger the <code>Rack::SSL</code> filters.</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">https://example.com<br />
http://example.com</div></td></tr></tbody></table></div>
<p>This solution also works very well for cloud providers like <strong>Heroku</strong> and completely replaces home-made solutions like the one I posted in <a title="Force SSL using ssl_requirement in Rails app - Stack Overflow" href="http://stackoverflow.com/questions/3861772/force-ssl-using-ssl-requirement-in-rails-app">this StackOverflow answer</a> months ago, also featured <a href="http://clearcove.ca/blog/2010/11/how-to-secure-a-rails-app-on-heroku-with-ssl-firesheep/">here</a> and <a href="http://blog.dynamic50.com/2011/02/15/ssl-on-wildcard-domains-on-heroku-using-godaddy/">here</a>.</p>
<p>If you want to <strong>use SSL on Heroku and redirect the HTTP traffic to HTTPS</strong>, my suggestion is to go with <code>Rack::SSL</code>.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2010/08/rails-3-you-are-using-the-old-router-dsl/' rel='bookmark' title='Upgrading to Rails 3: You are using the old router DSL which will be removed in Rails 3.1'>Upgrading to Rails 3: You are using the old router DSL which will be removed in Rails 3.1</a></li>
<li><a href='http://www.simonecarletti.com/blog/2010/07/the-way-to-rails-3/' rel='bookmark' title='The Road to Rails 3: make your Rails 2.3 project more Rails 3 oriented'>The Road to Rails 3: make your Rails 2.3 project more Rails 3 oriented</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/07/configuring-git-repository-with-redmine/' rel='bookmark' title='Configuring a Git repository with Redmine'>Configuring a Git repository with Redmine</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>RSpec Rails doesn&#8217;t render Rails views by default</title>
		<link>http://www.simonecarletti.com/blog/2011/04/rspec-rails-doesnt-render-rails-views-by-default/</link>
		<comments>http://www.simonecarletti.com/blog/2011/04/rspec-rails-doesnt-render-rails-views-by-default/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 07:54:23 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1433</guid>
		<description><![CDATA[RSpec (actually the RSpec Rails gem) doesn't render the content of the action by default, like the common Rails functional tests do.]]></description>
			<content:encoded><![CDATA[<p>If you are using <strong>RSpec</strong> and <strong>RSpec Rails</strong> to test your Rails application, I strongly encourage you to keep reading this post because there&#8217;s a gotcha in the default configuration you might not expect, especially if you ever used the default Rails test framework before.</p>
<p>A couple of days ago I was notified about a but in a Rails application. The application crashed when trying to create a new record.</p>
<p>I changed the <code>new</code> action the day before, but I was sure all the tests passed and the action was covered with a pretty reasonable RSpec test suite. Moreover, the action method was one of the most simplest methods I have ever written.</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:#9966CC; font-weight:bold;">def</span> new<br />
&nbsp; <span style="color:#0066ff; font-weight:bold;">@record</span> = Model.<span style="color:#9900CC;">new</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>I run the test suite, all green. I executed the page in the browser, it crashed.</p>
<div id="attachment_1434" class="wp-caption aligncenter" style="width: 384px"><img class="size-full wp-image-1434" title="WTF Monkey" src="http://www.simonecarletti.com/blog/wp-content/uploads/2011/04/wtf.jpg" alt="" width="374" height="300" /><p class="wp-caption-text">My face when I discovered all tests passed but the action kept crashing.</p></div>
<p>After several minutes of debugging I discovered the culprit: RSpec (actually the RSpec Rails gem) doesn&#8217;t render the content of the action by default, like the common Rails functional tests do. In fact, <code>@response.body</code> always resulted in an empty string.<span id="more-1433"></span></p>
<p>The view contained a statement like the following</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"><span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">collection_select</span> Model.<span style="color:#9900CC;">all</span>, <span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></td></tr></tbody></table></div>
<p>that was misspelled and crashed when the template was rendered. But because RSpec doesn&#8217;t render the Rails views, the view was not covered by the tests at all.</p>
<p>The solution is simple and is called <a href="http://relishapp.com/rspec/rspec-rails/v/2-5/dir/controller-specs/render-views">render_views</a>.</p>
<p>To tell a controller to render the content of the view, simply call <code>render_views</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 /></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;">&quot;spec_helper&quot;</span><br />
<br />
describe RecordsController <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; render_views<br />
<br />
&nbsp; describe <span style="color:#996600;">&quot;GET index&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># ...</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>You can also enable <code>render_views</code> globally, in your <code>spec_helper.rb</code> file.</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">RSpec.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; config.<span style="color:#9900CC;">render_views</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>The reason behind this choice is because RSpec Rails actually provides a special RSpec group called &#8220;view specs&#8221;.</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">describe <span style="color:#996600;">&quot;events/index.html.erb&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; it <span style="color:#996600;">&quot;renders _event partial for each event&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; assign<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:events</span>, <span style="color:#006600; font-weight:bold;">&#91;</span>stub_model<span style="color:#006600; font-weight:bold;">&#40;</span>Event<span style="color:#006600; font-weight:bold;">&#41;</span>, stub_model<span style="color:#006600; font-weight:bold;">&#40;</span>Event<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; render<br />
&nbsp; &nbsp; view.<span style="color:#9900CC;">should</span> render_template<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;_event&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:count</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span><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>According to the documentation, this is the right way to test Rails templates in RSpec. Honestly, I never really liked this feature and I tend to keep using controller specs — the RSpec equivalent of functional tests — (or alternative test tools) to test the views.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2010/12/get-the-currently-described-class-in-rspec/' rel='bookmark' title='Get the currently described class in RSpec'>Get the currently described class in RSpec</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/08/user-profile-permalinks-with-ruby-on-rails-and-authlogic/' rel='bookmark' title='User profile permalinks with Ruby on Rails (and Authlogic)'>User profile permalinks with Ruby on Rails (and Authlogic)</a></li>
<li><a href='http://www.simonecarletti.com/blog/2011/02/new-in-whois-gem-testers/' rel='bookmark' title='New in Whois: Gem Testers'>New in Whois: Gem Testers</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/04/rspec-rails-doesnt-render-rails-views-by-default/feed/</wfw:commentRss>
		<slash:comments>5</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>Whois 2.0</title>
		<link>http://www.simonecarletti.com/blog/2011/03/whois-2-0/</link>
		<comments>http://www.simonecarletti.com/blog/2011/03/whois-2-0/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 20:58:19 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Hosting / Domains]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubywhois]]></category>
		<category><![CDATA[whois]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1417</guid>
		<description><![CDATA[I'm very proud to announce the immediate availability of Whois 2.0.]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-thumbnail wp-image-606" title="Whois" src="http://www.simonecarletti.com/blog/wp-content/uploads/2009/09/whois-150x150.png" alt="" width="150" height="150" />One year after the <a title="Ruby Whois 1.0 is here!" href="http://www.simonecarletti.com/blog/2010/02/ruby-whois-1-0-is-here/">first major release</a>, almost 2 years after the first commit, 1k commits and 59 versions after, I&#8217;m very proud to announce the immediate availability of <strong>Whois 2.0</strong>.</p>
<p><a title="Ruby Whois - Ruby Whois Gem" href="http://www.ruby-whois.org/">Whois</a> is an intelligent, pure Ruby, <strong>WHOIS client and parser</strong>. It provides a flexible and programmable API to query WHOIS servers and look up IP, TLD, and domain WHOIS information. It also offers command-line interface to run WHOIS queries from the console.</p>
<p>This second major release, the 60th since I released the gem, is a very important milestones. I started working on this project with the idea of <strong>creating a Ruby parser for each TLD available and it took me two years and countless hours to reach this goal</strong>.<span id="more-1417"></span></p>
<h2>Some stats</h2>
<p>To give you a rough idea about the complexity of this project, here&#8217;s a few numbers about Whois 2.0.</p>
<ul>
<li>866,706 different WHOIS records (~3.3 GB of data) analyzed as of March 16th, 2011</li>
<li>425 RSpec test files</li>
<li>444 fixture files extracted from real WHOIS records</li>
<li>3787 RSpec examples and an unknown number of matches (sorry, I stopped keeping track of them when they reached 15k assertions) that probably makes this project one of the gems with the largest RSpec test suite</li>
<li>~20.000 lines of code, including the test files</li>
<li>168 different WHOIS parsers</li>
</ul>
<p>Oh, man. You can&#8217;t believe the amount of work required to cover all the existing WHOIS servers, their features and especially their whims.</p>
<h2>New and Notable</h2>
<p>The increasing support for all existing TLD servers is probably one of the most important changes for this milestone. But the CHANGELOG is very huge and there are a few other changes that are worth a mention.</p>
<p>I already blogged about a few of them.</p>
<ul>
<li>The <a href="http://www.simonecarletti.com/blog/2011/03/new-in-whois-nameservers-are-now-nameserver/"><code>#nameservers</code> property now returns a Nameserver object</a>. This change allows the parser to also extract name server IPv4 and IPv6 addresses.</li>
<li>A <a href="http://www.simonecarletti.com/blog/2011/02/new-in-whois-improved-caching/">new caching mechanism</a> reduces the number of instance variables created.</li>
<li>The library now supports the <a href="http://www.simonecarletti.com/blog/2011/02/new-in-whois-gem-testers/">Gem Testers project</a>.</li>
<li>You can now bind a different address or port to the WHOIS socket request.</li>
</ul>
<p>Last but not least, the <code>Whois::Answer</code> class has been renamed to <code>Whois::Record</code>.</p>
<p>There are a couple of backwards-incompatible changes to the API. These changes were necessary to evolve the API to better reflect the representation of the existing WHOIS responses.</p>
<h2>What&#8217;s Next?</h2>
<p>The Whois library is pretty stable. It is now used in production for several big projects.</p>
<p>The API is mature. A few changes have been made in this major release to support additional WHOIS properties, however I don&#8217;t expect big changes in the future.</p>
<p>The following releases will focus on increasing the list of supported properties for each parser, as well closing some of the <a href="https://github.com/weppos/whois/issues">existing issues</a>.</p>
<p>The biggest change in the API in the near future is likely to be the <a href="https://github.com/weppos/whois/issues#issue/2">standardization of the status property</a>.</p>
<p>In the last months I received some great feedback from people using my library with other platforms, such as Java and .NET. This is amazing.</p>
<p>If you have any case study, case history or feedback, please let me know. I would love to learn more about how you use the library.</p>
<h2>How to Upgrade</h2>
<p>To upgrade to Whois 2.0 please follow the instructions in the <a href="http://www.ruby-whois.org/manual/upgrading.html">documentation page</a>.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2009/12/whois-0-9-0/' rel='bookmark' title='Whois 0.9.0: WHOIS parsers, CLI and performances'>Whois 0.9.0: WHOIS parsers, CLI and performances</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/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>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/03/whois-2-0/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>HTML 5 Up and Running Book Review</title>
		<link>http://www.simonecarletti.com/blog/2011/03/book-review-html-5-up-and-running/</link>
		<comments>http://www.simonecarletti.com/blog/2011/03/book-review-html-5-up-and-running/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 11:22:17 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[html 5]]></category>
		<category><![CDATA[reviews]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1393</guid>
		<description><![CDATA[HTML5 Up and Running provides a practical overview of the new features available in HTML 5.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.simonecarletti.com/blog/out/amzcom-divehtml5"><img class="alignleft" title="Book HTML5 Up and Running" src="http://share.weppos.net/simonecarletti/book-oreilly-divehtml5-20110930-193508.png" alt="Book HTML5 Up and Running" width="190" height="250" /></a><a href="http://www.simonecarletti.com/blog/out/amzcom-divehtml5"><em>HTML5 Up and Running</em></a> (<a href="http://www.simonecarletti.com/blog/out/amzcom-divehtml5">US</a> | <a href="http://www.simonecarletti.com/blog/out/amzuk-divehtml5">UK</a>) is one of the first books — if not the first — <strong>about HTML 5</strong>. The author of the book is Mark Pilgrim, developer advocate for Google well know for its effort in the open source community and web standards.</p>
<p>Mark is the author of several other publications including <a href="http://example.com/">Dive Into Accessibility</a>, <a title="Dive Into Python" href="http://diveintopython.org/">Dive into Python</a>, Dive Into Greasemonkey. Not surprisingly, his site is called <a title="dive into mark" href="http://diveintomark.org/">Dive Into Mark</a>.</p>
<p>But hey. Why isn&#8217;t this book called Dive Into HTML 5? I&#8217;m glad you asked. Actually, this appears to be the original title. The book is entirely available for free on the web at the <a title="Dive Into HTML5" href="http://diveintohtml5.org/">Dive Into HTML 5 website</a>. Honestly, I don&#8217;t know the reason why the title was changed for the printed version.</p>
<p><em>HTML5 Up and Running</em> provides a <strong>practical overview of the new features already available in HTML 5</strong>. HTML 5 is not a standard (yet), but browsers such as Safari, Firefox, Opera and Chrome already support many if its feature.</p>
<p>One of the first things you&#8217;ll learn reading this book, is that HTML 5 is not one big thing. Here&#8217;s what Mark said in the introduction.</p>
<blockquote><p>You may well ask: &#8220;How can I start using HTML5 if older browsers don&#8217;t support it?&#8221; But the question itself is misleading. HTML5 is not one big thing; it is a collection of individual features. So you can&#8217;t detect &#8220;HTML5 support,&#8221; because that doesn&#8217;t make any sense. But you <em>can</em> detect support for individual features, like canvas, video, or geolocation.</p></blockquote>
<p><strong>HTML 5 is closer than you think</strong>.</p>
<p><span id="more-1393"></span>For example, by reading this book you will learn that the iPhone and the iPad already support the new HTML 5 input features and attempt to adapt the keyboard layout according to the <code>input</code> type.</p>
<p>An email input type, like this following one</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;input type=&quot;email&quot; /&gt;</div></td></tr></tbody></table></div>
<p>on the iPhone and iPad will trigger the email-keyboard that contains a smaller-than-usual space bar, plus dedicated keys for the <code>@</code> and <code>.</code> characters.</p>
<p>Isn&#8217;t this amazing?</p>
<h2>Structure</h2>
<p><em>HTML5 Up and Running</em> is divided into an introduction, 11 chapters, and 2 appendix.</p>
<p>Here&#8217;s the table of content:</p>
<ul>
<li>Introduction: Five Things You Should Know About HTML</li>
<li>A Quite Biased History of HTML5</li>
<li>Detecting HTML5 Features: It&#8217;s Elementary, My Dear Watson</li>
<li>What Does It All Mean?</li>
<li>Let’s Call It a Draw(ing Surface)</li>
<li>Video in a Flash (Without That Other Thing)</li>
<li>You Are Here (And So Is Everybody Else)</li>
<li>A Place To Put Your Stuff</li>
<li>Let’s Take This Offline</li>
<li>A Form of Madness</li>
<li>&#8220;Distributed&#8221;, &#8220;Extensibility&#8221;, And Other Fancy Words</li>
<li>Manipulating History for Fun &amp; Profit</li>
<li>The All-In-One Almost-Alphabetical No-Bullshit Guide to Detecting Everything</li>
<li>HTML5 Peeks, Pokes and Pointers</li>
</ul>
<p>The book is very easy to read. It contains <strong>tons of examples</strong>, a very simple tone and several references. However, <strong>previous programming experience is recommended</strong>, especially if you plan to use advanced JavaScript-oriented features.</p>
<h2>Examples, Examples and Examples</h2>
<p>As I already mentioned, the book is full of contextual hints and examples. This is really cool because examples help you to remember an information.</p>
<p>You don&#8217;t need to trust me, you can check with your own eyes. Go for example to the <a title="Canvas - Dive Into HTML5" href="http://diveintohtml5.org/canvas.html">Canvas chapter</a> and see how well it is explained. I honestly can&#8217;t imagine a better way to do this.</p>
<h2>This is not a book to learn HTML</h2>
<p>Remember, even if <em>HTML5 Up and Running</em> is easy to read and full of examples, <strong>it isn&#8217;t the right book to start learning HTML</strong>. This book assumes you already know about HTML, CSS and JavaScript.</p>
<p>Nevertheless, the <em><a title="How Did We Get Here? - Dive Into HTML5" href="http://diveintohtml5.org/past.html">How did we get there</a></em> chapter is one of the best article I ever read about the history of HTML.</p>
<p>And if you happened to migrate all your websites to XHTML (like the 90% of developers did) in the last 5 years, you might probably want to read the <em>everything you know about xhtml is wrong</em> paragraph — <em>*sigh*</em>.</p>
<h2>Remember, JavaScript is the future</h2>
<p>Since I mentioned JavaScript, let me take the time to highlight a very important concept: <strong>JavaScript is the future</strong>.</p>
<p>It doesn&#8217;t matter if you are developing a mobile web site, an iPad product, a Rails application, an interactive HTML 5 interface. JavaScript is the future.</p>
<p><strong>JavaScript stands to development like parsley stands to kitchen</strong>. You find it almost everywhere. Think about jQuery Mobile, Sencha Touch, <a title="Unobtrusive JavaScript in Rails 3  –  Simone Carletti's Blog" href="http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/">unobtrusive Rails 3 helpers</a>, <a title="Cappuccino Web Framework - Build Desktop Class Applications in Objective-J and JavaScript" href="http://cappuccino.org/">Cappucino</a>, HTML 5 Modernizr library, etc. these are just a few examples of how JavaScript is evolving and entering our life.</p>
<p>If you are a developer or designer, you can&#8217;t continue to ignore JavaScript. And JavaScript appears to be even more important in HTML 5 because several features are designed to work with JavaScript. I&#8217;m talking about Geo Localization, Canvas and Local Storage, just to name a few.</p>
<p>So do yourself a favor: <strong>take the time to start learning and experimenting with JavaScript</strong>.</p>
<h2>In conclusion</h2>
<p>I enjoyed this book. I appreciated the HTML 5 introduction, the history of HTML and the tutorial about detecting HTML 5 features. But the chapter I enjoyed the most is the one about the new HTML 5 form inputs, probably because I have been able to start experimenting with them immediately.</p>
<h2>Where to Find HTML 5 Up and Running</h2>
<p>You can get a printed or Kindle copy at <a href="http://www.simonecarletti.com/blog/out/amzcom-divehtml5">Amazon.com</a>, <a href="http://www.simonecarletti.com/blog/out/amzuk-divehtml5">Amazon.co.uk</a>, or at your other favorite book seller.</p>
<div id="kindle-reader">
<h3>Kindle Preview</h3>
</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: 'B0043D2E0E', width: '600', height: '635', assoctag: 'simonecarletti-20'});
// ]]&gt;</script></p>
<p>Related posts<ol>
<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/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/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-html-5-up-and-running/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby 4 Rails &#8211; Learning Ruby, with Rails in mind</title>
		<link>http://www.simonecarletti.com/blog/2011/03/ruby-4-rails/</link>
		<comments>http://www.simonecarletti.com/blog/2011/03/ruby-4-rails/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 09:47:30 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[showoff]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=1353</guid>
		<description><![CDATA[Ruby 4 Rails is a presentation about the most essential features of the Ruby programming language, created to provide the necessary Ruby knowledge to start with Rails.]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-thumbnail wp-image-1363" title="Ruby Logo" src="http://www.simonecarletti.com/blog/wp-content/uploads/2011/03/ruby-icon-150x150.png" alt="" width="100" height="100" />A few weeks ago, a friend of mine told me he wanted to learn Rails. He asked me to prepare a private course about Rails and to teach him the basics of the framework.</p>
<p>After a brief discussion, we ultimately agreed that he couldn&#8217;t effectively use Rails without <em>being a Rubyist</em>. For this reason, I offered him to start with a Ruby concentrate, carefully prepared keeping an eye on Rails.</p>
<p>The presentation is called <strong>Ruby 4 Rails</strong> and <a href="https://github.com/weppos/ruby-4-rails.showoff">is now available on GitHub</a>. It is based on <a href="https://github.com/schacon/showoff">ShowOff</a>, a Markdown-to-HTML presentation tool created by Scott Chacon. Make sure to read the <code>README</code> to learn how it works.</p>
<p>The initial part of the README is largely inspired by <a href="http://www.engineyard.com/blog/2011/ruote-and-flow/">John&#8217;s post about Ruote</a>.</p>
<p><a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img class="alignleft" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" alt="Creative Commons License" /></a> Ruby 4 Rails is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>. Based on a work at <a rel="dct:source" href="https://github.com/weppos/ruby-4-rails.showoff">github.com</a>.</p>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2009/08/ruby-on-rails-and-canonical-link-tag/' rel='bookmark' title='Ruby on Rails and Canonical link tag'>Ruby on Rails and Canonical link tag</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/04/tabsonrails/' rel='bookmark' title='TabsOnRails: creating and managing Tabs with Ruby on Rails'>TabsOnRails: creating and managing Tabs with Ruby on Rails</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/08/user-profile-permalinks-with-ruby-on-rails-and-authlogic/' rel='bookmark' title='User profile permalinks with Ruby on Rails (and Authlogic)'>User profile permalinks with Ruby on Rails (and Authlogic)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2011/03/ruby-4-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

