<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Whatever to succeed in rails' world</title>
	
	<link>http://www.tricksonrails.com</link>
	<description>Ruby and Ruby on Rails tricks and tips from the edge</description>
	<lastBuildDate>Fri, 01 Jul 2011 06:59:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/WhateverToSucceedInRailsWorld" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="whatevertosucceedinrailsworld" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>The 7 main actions we took to improve the Rails stack performance at Justin.tv</title>
		<link>http://www.tricksonrails.com/2011/07/the-7-main-actions-we-took-to-improve-the-rails-stack-performance-at-justin-tv/</link>
		<comments>http://www.tricksonrails.com/2011/07/the-7-main-actions-we-took-to-improve-the-rails-stack-performance-at-justin-tv/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 06:59:22 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[justin.tv]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=418</guid>
		<description><![CDATA[Here are the slides of the talk I gave at the San Francisco Rails meetup group yesterday about the work we have done on improving Rails performance at Justin.tv Enjoy! Rails performance at Justin.tv &#8211; Guillaume Luccisano View more presentations from kwi]]></description>
			<content:encoded><![CDATA[<p>Here are the slides of the talk I gave at the San Francisco Rails meetup group yesterday about the work we have done on improving Rails performance at Justin.tv</p>
<p>Enjoy!</p>
<div style="width:550px; margin: auto" id="__ss_8476299"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/kwi/rails-performance-at-justintv-guillaume-luccisano" title="Rails performance at Justin.tv - Guillaume Luccisano" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.slideshare.net/kwi/rails-performance-at-justintv-guillaume-luccisano?referer=');">Rails performance at Justin.tv &#8211; Guillaume Luccisano</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/8476299" width="550" height="420" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.slideshare.net/?referer=');">presentations</a> from <a href="http://www.slideshare.net/kwi" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.slideshare.net/kwi?referer=');">kwi</a> </div>
</p></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2011%2F07%2Fthe-7-main-actions-we-took-to-improve-the-rails-stack-performance-at-justin-tv%2F&amp;linkname=The%207%20main%20actions%20we%20took%20to%20improve%20the%20Rails%20stack%20performance%20at%20Justin.tv" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2011_2F07_2Fthe-7-main-actions-we-took-to-improve-the-rails-stack-performance-at-justin-tv_2F_amp_linkname=The_207_20main_20actions_20we_20took_20to_20improve_20the_20Rails_20stack_20performance_20at_20Justin.tv&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2011/07/the-7-main-actions-we-took-to-improve-the-rails-stack-performance-at-justin-tv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails3 ActiveSupport Notification subscription – Rails3 Tricks #03</title>
		<link>http://www.tricksonrails.com/2011/03/rails3-active-support-notification-subscription/</link>
		<comments>http://www.tricksonrails.com/2011/03/rails3-active-support-notification-subscription/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 02:17:29 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[active support]]></category>
		<category><![CDATA[notifications]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=391</guid>
		<description><![CDATA[Hello there! Almost 10 months without any post due to a very busy period: I moved from Paris to San Francisco. Ok, so now facts has been said, this will hopefully change! I plan to write a new series of Rails3 posts, starting with a quick one on the new way Rails handles notifications. The [...]]]></description>
			<content:encoded><![CDATA[<p>Hello there! Almost 10 months without any post due to a very busy period: I moved from Paris to San Francisco.<br/> Ok, so now facts has been said, this will hopefully change! I plan to write a new series of Rails3 posts, starting with a quick one on the new way Rails handles notifications.</p>
<p>The new version of ActiveSupport shipped with Rails brings along a new notification system which is heavily used by Rails3 internarlly. Rails doesn&#8217;t write directly to logs anymore, instead of that, it publishes a notification which can be caught by any observers.</p>
<p>In production, Rails will by default publish deprecation warnings through this notification system. Last week I was looking for a way to play with that and didn&#8217;t find a clear example on the web, so here is a small snippet of code if you are also looking for a nice way to log your deprecation warnings:</p>
<p><br/></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;"># In config/initializers/deprecations_logger.rb</span><br />
DeprecationLogger = <span style="color:#CC00FF; font-weight:bold;">Logger</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'log/deprecations.log'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<span style="color:#6666ff; font-weight:bold;">ActiveSupport::Notifications</span>.<span style="color:#9900CC;">subscribe</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>deprecation<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>type, date,b,c, event<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; DeprecationLogger.<span style="color:#9900CC;">info</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{date} - #{event[:message]}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2011%2F03%2Frails3-active-support-notification-subscription%2F&amp;linkname=Rails3%20ActiveSupport%20Notification%20subscription%20%26%238211%3B%20Rails3%20Tricks%20%2303" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2011_2F03_2Frails3-active-support-notification-subscription_2F_amp_linkname=Rails3_20ActiveSupport_20Notification_20subscription_20_26_238211_3B_20Rails3_20Tricks_20_2303&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2011/03/rails3-active-support-notification-subscription/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Avoid memory leaks in your ruby/rails code and protect you against denial of service</title>
		<link>http://www.tricksonrails.com/2010/06/avoid-memory-leaks-in-ruby-rails-code-and-protect-against-denial-of-service/</link>
		<comments>http://www.tricksonrails.com/2010/06/avoid-memory-leaks-in-ruby-rails-code-and-protect-against-denial-of-service/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 10:45:37 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[ruby1.9]]></category>
		<category><![CDATA[symbol]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=345</guid>
		<description><![CDATA[We heard a lot about that Ruby is cool cause we do not have to care about memory, the garbage collector does it for us. Well, that&#8217;s kind of true, but this does not mean we can write code without keeping in mind on what&#8217;s is going on under our ruby code. Ruby symbol memory [...]]]></description>
			<content:encoded><![CDATA[<p>We heard a lot about that Ruby is cool cause we do not have to care about memory, <strong>the garbage collector</strong> does it for us. Well, that&#8217;s kind of true, but this does not mean we can write code without keeping in mind on <strong>what&#8217;s is going on under our ruby code</strong>.</p>
<h3>Ruby symbol memory leak</h3>
<p>We all know that using <strong>symbols</strong> instead of <strong>strings</strong> is a good practice to have, it&#8217;s <strong>faster</strong> and it <strong>saves your memory</strong>. Yes but at what price ? Symbols are faster in part cause they are created just one time in memory, that&#8217;s great ! But then ? they <strong>will stay forever in memory</strong> &#8230;</p>
<p>That means, <strong>do not</strong> convert everything in symbol ! Be sure to well know what you are converting in symbol.</p>
<p>Example: Somewhere in your app, you apply a <em>to_sym</em> on an user&#8217;s name like :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">hash<span style="color:#006600; font-weight:bold;">&#91;</span>current_user.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">to_sym</span><span style="color:#006600; font-weight:bold;">&#93;</span> = something</div></div>
<p>When you have hundreds of users, that&#8217;s could be ok, but what is happening if you have one million of users ? Here are the numbers :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">kwi:~$ irb<br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>head <span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#008000; font-style:italic;"># Current memory usage : 6608K</span><br />
<span style="color:#008000; font-style:italic;"># Now, add one million randomly generated short symbols</span><br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>head <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1000000</span>.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">to_f</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_sym</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
<span style="color:#008000; font-style:italic;"># Current memory usage : 153M, even after a Garbage collector run.</span><br />
<span style="color:#008000; font-style:italic;"># Surprisingly, on Ruby 1.8.7-p249,</span><br />
<span style="color:#008000; font-style:italic;"># the VM only grow up to 33M, but that's still a lot !</span><br />
<br />
<span style="color:#008000; font-style:italic;"># Now, imagine if symbols are just 20x longer than that ?</span><br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>head <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">1000000</span>.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">to_f</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_sym</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#008000; font-style:italic;"># Current memory usage : 501M</span></div></div>
<p>Furthermore, <strong>NEVER</strong> convert non controlled arguments in symbol or check arguments before, this can easily lead to a <strong>denial of service</strong>.</p>
<p>Example: You have a website with a locale parameter in order to localize your content and you have something like this in your application controller:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">before_filter <span style="color:#ff3333; font-weight:bold;">:set_locale</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">def</span> set_locale<br />
&nbsp; I18n.<span style="color:#9900CC;">locale</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:locale</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_sym</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>It&#8217;s really simple to call thousand of times your website with a long params[:locale] and make your <strong>application bloat</strong> !</p>
<p>By the way, it looks like the <strong>I18n gem</strong> converts automatically the locale in symbol, so be sure to check if the locale is valid before assigning it !<br />
Here is the link: <a href="http://github.com/svenfuchs/i18n/blob/master/lib/i18n/config.rb#LID8" onclick="pageTracker._trackPageview('/outgoing/github.com/svenfuchs/i18n/blob/master/lib/i18n/config.rb_LID8?referer=');">http://github.com/svenfuchs/i18n/blob/master/lib/i18n/config.rb#LID8</a></p>
<p>If you need to control your number of allocted symbols in your app, you can use <em>Symbol.all_symbols.size</em>. Add this to your log to see if you are <strong>leaking symbols over time</strong> ! (This can be a good measure to add in <strong>Newrelic</strong>; Newrelic guys, are you reading ? <img src='http://www.tricksonrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Reference to objects leak</h3>
<p>This leak is a fake one but can grow rapidly in your app.<br />
It happens when you keep a variable in your code referering objects, and these objects are also referencing objects, and etc&#8230;</p>
<p>This often happen when using <em>$variable</em> or <em>@@variable</em> as they stay <strong>forever</strong> in memory.<br />
Here is a little example :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;"># Memory usage at irb launch: 6320K</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> HelloIamLeaking<br />
&nbsp; @@an_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Put something big in the array</span><br />
&nbsp; &nbsp; @@an_array <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;hello world&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">**</span><span style="color:#006666;">10</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 />
x = HelloIamLeaking.<span style="color:#9900CC;">new</span><br />
x = <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#008000; font-style:italic;"># So no more HelloIamLeaking instance in our code</span><br />
<span style="color:#CC00FF; font-weight:bold;">GC</span>.<span style="color:#9900CC;">start</span> <span style="color:#008000; font-style:italic;"># Run the garbage collector to be sure this is real !</span><br />
<span style="color:#008000; font-style:italic;"># Memory usage after : 17M</span><br />
<br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.9.2<span style="color:#006600; font-weight:bold;">-</span>head <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">ObjectSpace</span>.<span style="color:#9900CC;">each_object</span><span style="color:#006600; font-weight:bold;">&#40;</span>HelloIamLeaking<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> x <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span><br />
<span style="color:#008000; font-style:italic;"># So we have no more instance of HelloIamLeaking</span><br />
<span style="color:#008000; font-style:italic;"># but the class variable remains in memory.</span></div></div>
<p>Ok, this is a completely logical and dumb example but this show you the principle.</p>
<p>And this can grow <strong>exponentially</strong> if you have objects linking to <strong>huge array or datasets</strong>, they will <strong>never be garbage collected if just one object in your code is still referencing the source object</strong>.</p>
<p>This will consume your memory, but not only, this will also consume your cpu time as when the garbage collector runs, it looks on every single object, and the more objects you have, the more it spent time looking at them &#8230;</p>
<p>To resume <strong>reference leak</strong> : with time, it&#8217;s <strong>grow in memory</strong> and <strong>slow down dramatically the garbage collector running time</strong>.</p>
<p>If you want to read more about reference leaks, read the awsome post <a href="http://timetobleed.com/descent-into-darkness-understanding-your-systems-binary-interface-is-the-only-way-out/" onclick="pageTracker._trackPageview('/outgoing/timetobleed.com/descent-into-darkness-understanding-your-systems-binary-interface-is-the-only-way-out/?referer=');">descent into darkness</a> on the <a href="http://timetobleed.com/" onclick="pageTracker._trackPageview('/outgoing/timetobleed.com/?referer=');">blog</a> of Joe Damato.</p>
<p><strong>Update:</strong> Find this leaks easily using the <a href="http://github.com/ice799/memprof" onclick="pageTracker._trackPageview('/outgoing/github.com/ice799/memprof?referer=');">memprof</a> gem and by using <a href="http://memprof.com/" class="broken_link" onclick="pageTracker._trackPageview('/outgoing/memprof.com/?referer=');">memprof.com</a> (awsome stuff again by Joe Damato)</p>
<h3>My app is still bloating !</h3>
<p>After that, if you have still <strong>ruby/rails process bloating</strong>, be sure to use the latest version of gem that are using <strong>C code</strong>, they can be an easy source of memory leak.</p>
<p>And, this is obvious, but be sure to <strong>not load huge dataset in memory at one time</strong> ! (use <a href="http://apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_in_batches" onclick="pageTracker._trackPageview('/outgoing/apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_in_batches?referer=');">find_in_batch</a> instead for example)</p>
<p>Then, If you want more control over the memory allocation, here is a good link for <strong><a href="http://www.coffeepowered.net/2009/06/13/fine-tuning-your-garbage-collector/" onclick="pageTracker._trackPageview('/outgoing/www.coffeepowered.net/2009/06/13/fine-tuning-your-garbage-collector/?referer=');">tune up the heap</a></strong> easily and control your ruby process growth.</p>
<p><br/><br />
Thanks for taking the time to read and I hope this article will help you to reduce your memory consumption !</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F06%2Favoid-memory-leaks-in-ruby-rails-code-and-protect-against-denial-of-service%2F&amp;linkname=Avoid%20memory%20leaks%20in%20your%20ruby%2Frails%20code%20and%20protect%20you%20against%20denial%20of%20service" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F06_2Favoid-memory-leaks-in-ruby-rails-code-and-protect-against-denial-of-service_2F_amp_linkname=Avoid_20memory_20leaks_20in_20your_20ruby_2Frails_20code_20and_20protect_20you_20against_20denial_20of_20service&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/06/avoid-memory-leaks-in-ruby-rails-code-and-protect-against-denial-of-service/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>I18n_routing: Translate your Rails2/3 routes with ease</title>
		<link>http://www.tricksonrails.com/2010/05/i18n_routing-translate-your-rails2-rails3-routes-with-ease/</link>
		<comments>http://www.tricksonrails.com/2010/05/i18n_routing-translate-your-rails2-rails3-routes-with-ease/#comments</comments>
		<pubDate>Mon, 31 May 2010 07:27:49 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[rails2]]></category>
		<category><![CDATA[rais3]]></category>
		<category><![CDATA[routes]]></category>
		<category><![CDATA[routing]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=300</guid>
		<description><![CDATA[Nowadays, more and more of our Rails applications have to be localized in order to support some languages and handle lots more of customers. Since Rails 2.2 we can do this easily through the awsome i18n gem. That&#8217;s great, but with Rails, we are stuck with basics url in just one main language, and that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Nowadays, more and more of our Rails applications have to be localized in order to support some languages and handle lots more of customers. Since Rails 2.2 we can do this easily through the awsome i18n gem.</p>
<p>That&#8217;s great, but with Rails, we are stuck with basics url in just one main language, and that&#8217;s really sucks in term of <strong>SEO performance</strong>.</p>
<p>So why not <strong>translate your Rails routes</strong> too ? You will gain more customers through <strong>SEO optimization</strong> and by the way you will make your clients happy !</p>
<p>Yesterday I have released a new version <a href="http://github.com/kwi/i18n_routing" onclick="pageTracker._trackPageview('/outgoing/github.com/kwi/i18n_routing?referer=');">i18n_routing</a>. It&#8217;s now fully compatible with both <strong>Rails2</strong> and <strong>Rail3</strong>.</p>
<p>This gem give you the capability to translates easily all your routes through the shipped i18n gem :<br />
<strong>There is nothing to change in your code: just add a few translations in your locale files !</strong></p>
<h3>Usage and examples</h3>
<p>First of all, install the i18n_routing gem and add it to your Rails project. (<a href="http://wiki.github.com/kwi/i18n_routing/installation" onclick="pageTracker._trackPageview('/outgoing/wiki.github.com/kwi/i18n_routing/installation?referer=');">Help if needed in the wiki</a>)<br />
Then, in your <strong>routes.rb</strong> file, just set wich routes you want to translate with the <em>localized</em> method:<br />
<br/><br />
<strong>Rails2 examples :</strong></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">map.<span style="color:#9900CC;">localized</span><span style="color:#006600; font-weight:bold;">&#40;</span>I18n.<span style="color:#9900CC;">available_locales</span>, <span style="color:#ff3333; font-weight:bold;">:verbose</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; map.<span style="color:#9900CC;">about</span>, <span style="color:#996600;">'about'</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:controller <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'contents'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:about</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
&nbsp; map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:users</span><br />
&nbsp; map.<span style="color:#9900CC;">resource</span> &nbsp;<span style="color:#ff3333; font-weight:bold;">:contact</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p><strong>Rails3 examples :</strong></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">localized<span style="color:#006600; font-weight:bold;">&#40;</span>I18n.<span style="color:#9900CC;">available_locales</span>, <span style="color:#ff3333; font-weight:bold;">:verbose</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; match <span style="color:#996600;">'about'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'contents#about'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> &nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:about</span><br />
<br />
&nbsp; resources <span style="color:#ff3333; font-weight:bold;">:users</span><br />
&nbsp; resource &nbsp;<span style="color:#ff3333; font-weight:bold;">:contact</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>(<em>I18n.available_locales</em> is an array of all locales available given by the i18n gem (version > 0.3.5), you can pass a custom table like this if you prefer : ['en', 'fr', 'es'])<br />
<br/></p>
<p>So, now our routes have been declared, just edit your locale files with translations :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">fr:<br />
&nbsp; resource:<br />
&nbsp; &nbsp; contact: <span style="color:#996600;">'contactez-nous'</span><br />
&nbsp; named_routes_path:<br />
&nbsp; &nbsp; about: <span style="color:#996600;">'a-propos'</span><br />
&nbsp; routes:<br />
&nbsp; &nbsp; users:<br />
&nbsp; &nbsp; &nbsp; as: utilisateurs<br />
&nbsp; &nbsp; &nbsp; path_names:<br />
&nbsp; &nbsp; &nbsp; &nbsp; new: <span style="color:#996600;">'nouvel_utilisateur'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; edit: <span style="color:#996600;">'edition_utilisateur'</span></div></div>
<p><em>(This works with any i18n backend, just choose your favorite one and go ahead.)</em><br />
<br/><br />
Now it&#8217;s done, just watch the result in your irb console :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ rails console<br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> I18n.<span style="color:#9900CC;">locale</span> = <span style="color:#ff3333; font-weight:bold;">:en</span><br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:en</span> <br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> app.<span style="color:#9900CC;">users_path</span><br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/users&quot;</span> <br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> I18n.<span style="color:#9900CC;">locale</span> = <span style="color:#ff3333; font-weight:bold;">:fr</span><br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:fr</span> <br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> app.<span style="color:#9900CC;">users_path</span><br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/utilisateurs&quot;</span> <br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> app.<span style="color:#9900CC;">contact_path</span><br />
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/contactez-nous&quot;</span></div></div>
<p><br/></p>
<p>Here we are ! You can now translate all your routes <strong>without any modification in your actual code</strong> !</p>
<p>All your routes are going to be automatically generated and recognized in the correct language depending on the current I18n.locale.<br />
<br/></p>
<h3>Keypoints and Features overview</h3>
<ul>
<li>No translations are made during runtime, all is <strong>precompiled</strong> when building routes at startup.</li>
<li>Works with Rails 2.x series (> 2.2) and with <strong>Rails 3</strong></li>
<li>Built on top of I18n api => translating your routes has never been as <strong>simple</strong> as now</li>
<li>Works with simple resource(s) to <strong>deep nested resource(s)</strong></li>
<li>Can translates path names like new/edit and custome ones</li>
</ul>
<p>If you want to know more about i18n_routing, go to the <a href="http://github.com/kwi/i18n_routing" onclick="pageTracker._trackPageview('/outgoing/github.com/kwi/i18n_routing?referer=');">i18n_routing github</a> and have a look at the wiki.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F05%2Fi18n_routing-translate-your-rails2-rails3-routes-with-ease%2F&amp;linkname=I18n_routing%3A%20Translate%20your%20Rails2%2F3%20routes%20with%20ease" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F05_2Fi18n_routing-translate-your-rails2-rails3-routes-with-ease_2F_amp_linkname=I18n_routing_3A_20Translate_20your_20Rails2_2F3_20routes_20with_20ease&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/05/i18n_routing-translate-your-rails2-rails3-routes-with-ease/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Introducing BrB, extremely fast interface for doing distributed ruby</title>
		<link>http://www.tricksonrails.com/2010/04/introducing-brb-extremely-fast-interface-for-doing-distributed-ruby/</link>
		<comments>http://www.tricksonrails.com/2010/04/introducing-brb-extremely-fast-interface-for-doing-distributed-ruby/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 19:48:15 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[brb]]></category>
		<category><![CDATA[distributed ruby]]></category>
		<category><![CDATA[eventmachine]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[message passing]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=271</guid>
		<description><![CDATA[BrB is a simple, transparent and extremely fast interface for doing distributed ruby easily. It&#8217;s inspired from the original Ruby Drb library (Distributed Ruby) but it is build on top of EventMachine for performance. The concept BrB use a simple concept : Create an object instance and expose it to the world. Any other ruby [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/kwi/BrB" onclick="pageTracker._trackPageview('/outgoing/github.com/kwi/BrB?referer=');">BrB</a> is a <strong>simple</strong>, <strong>transparent</strong> and <strong>extremely fast</strong> interface for doing <strong>distributed ruby</strong> easily.<br />
It&#8217;s inspired from the original Ruby <strong>Drb</strong> library (Distributed Ruby) but it is build on top of <strong>EventMachine</strong> for performance.</p>
<h3>The concept</h3>
<p><a href="http://github.com/kwi/BrB" onclick="pageTracker._trackPageview('/outgoing/github.com/kwi/BrB?referer=');">BrB</a> use a simple concept : Create an object instance and expose it to the world.<br />
Any other ruby process will be able to call method on that object after having created a communication tunnel.</p>
<ul>
<li>It&#8217;s <strong>simple</strong> as a <strong>method call</strong></li>
<li>It&#8217;s efficient, by default BrB do simple <strong>message passing</strong> (no return value)</li>
<li>You can pass over network every object that is dumpable through Marshal</li>
</ul>
<h3>Example 1 &#8211; Simple communication</h3>
<p>Start communicating between your different Ruby processes in two easy steps :</p>
<p>Start accepting connections :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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> ExposedCoreObject<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> simple_api_method<span style="color:#006600; font-weight:bold;">&#40;</span>parameter<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; &gt; Receive #{parameter} in the main ruby process&quot;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
EM::run <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#008000; font-style:italic;"># Start event machine</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># Start BrB Service, expose an instance of core object to the outside world</span><br />
&nbsp; <span style="color:#6666ff; font-weight:bold;">BrB::Service</span>.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">start_service</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:object</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> ExposedCoreObject.<span style="color:#9900CC;">new</span>, <span style="color:#ff3333; font-weight:bold;">:host</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'localhost'</span>, <span style="color:#ff3333; font-weight:bold;">:port</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">5555</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>In any other ruby process, start communicating :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;"># Create a communication tunnel to the core process</span><br />
<span style="color:#008000; font-style:italic;"># nil as first parameter as we do not expose any object in exchange</span><br />
core = <span style="color:#6666ff; font-weight:bold;">BrB::Tunnel</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#996600;">&quot;brb://localhost:5555&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
core.<span style="color:#9900CC;">simple_api_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'a message'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;"># Results :</span><br />
<span style="color:#008000; font-style:italic;"># On core process : &nbsp;&quot;&gt; Receive a message in the main ruby process&quot;</span></div></div>
<p>At the current point, the client call the simple_api_method on our core process.<br />
All the ruby magic is happening again, and number of processes communicating that way is unlimited !</p>
<h3>Example 2 &#8211; Both side communication</h3>
<p>Our previous example was great, but clients can receive method&#8217;s call too.</p>
<p><strong>Core code :</strong></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">EM::run <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#008000; font-style:italic;"># Start event machine</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># Start BrB Service, expose an instance of core object to the outside world</span><br />
&nbsp; <span style="color:#6666ff; font-weight:bold;">BrB::Service</span>.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">start_service</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:object</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> ExposedCoreObject.<span style="color:#9900CC;">new</span>, <span style="color:#ff3333; font-weight:bold;">:host</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'localhost'</span>, <span style="color:#ff3333; font-weight:bold;">:port</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">5555</span><span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp;<span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>type, tunnel<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Get alerted that a new connection has been made :</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> type == <span style="color:#ff3333; font-weight:bold;">:register</span><br />
&nbsp; &nbsp; &nbsp; tunnel.<span style="color:#9900CC;">say_hi_in_return</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'I am the core saying Hi'</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></div>
<p><strong>Client code :</strong></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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> ExposedClientObject<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> say_hi_in_return<span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; &gt; Core says : #{parameter}&quot;</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;"># That time, we are exposing an object.</span><br />
core = <span style="color:#6666ff; font-weight:bold;">BrB::Tunnel</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span>ExposedClientObject.<span style="color:#9900CC;">new</span>, <span style="color:#996600;">&quot;brb://localhost:5555&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
core.<span style="color:#9900CC;">simple_api_method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'a message'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;"># Results :</span><br />
<span style="color:#008000; font-style:italic;"># On client process : &nbsp;&quot;&gt; Core says : I am the core saying Hi&quot;</span><br />
<span style="color:#008000; font-style:italic;"># On core process : &nbsp;&quot;&gt; Receive a message in the main ruby process&quot;</span></div></div>
<p>That&#8217;s it, our both processes are now communicating each others, it&#8217;s completely transparent as it&#8217;s just work like normal ruby method calls.</p>
<h3>Example 3 &#8211; Waiting for a return value</h3>
<p>By default, calling a method on a distant object is not blocking. That means that it do not wait for any return value. But sometimes, it&#8217;s useful to get a return, in order to do this, just add <em>_block</em> at the end of the method name.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">core = <span style="color:#6666ff; font-weight:bold;">BrB::Tunnel</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#996600;">&quot;brb://localhost:5555&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
ret = core.<span style="color:#9900CC;">simple_api_method_block</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'a message'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># Wait for the return</span></div></div>
<h3>What BrB is usable for ?</h3>
<ul>
<li>Doing Simple <strong>message passing</strong> between ruby processes.</li>
<li>Connecting <strong>hundred</strong> of ruby processes <strong>transparently</strong>.</li>
<li>Building a <strong>real-time scalable</strong> (game) <strong>server</strong></li>
<li>Taking important load on a server easily just by <strong>distributing the load</strong> on multiple BrB instance.</li>
<li>Taking advantage of <strong>multi-core</strong> and <strong>multi-threaded</strong> systems.</li>
</ul>
<p>If you want to know more about BrB, go to the <a href="http://github.com/kwi/BrB" onclick="pageTracker._trackPageview('/outgoing/github.com/kwi/BrB?referer=');">BrB github</a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F04%2Fintroducing-brb-extremely-fast-interface-for-doing-distributed-ruby%2F&amp;linkname=Introducing%20BrB%2C%20extremely%20fast%20interface%20for%20doing%20distributed%20ruby" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F04_2Fintroducing-brb-extremely-fast-interface-for-doing-distributed-ruby_2F_amp_linkname=Introducing_20BrB_2C_20extremely_20fast_20interface_20for_20doing_20distributed_20ruby&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/04/introducing-brb-extremely-fast-interface-for-doing-distributed-ruby/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Rails3 and will_paginate, Doing easy remote links – Rails3 Tricks #02</title>
		<link>http://www.tricksonrails.com/2010/04/rails3-and-will_paginate-doing-easy-remote-links-rails3-tricks-02/</link>
		<comments>http://www.tricksonrails.com/2010/04/rails3-and-will_paginate-doing-easy-remote-links-rails3-tricks-02/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 09:27:23 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[remote]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[will_paginate]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=256</guid>
		<description><![CDATA[As you know, Rails3 use only UJS (unobtrusive javascript), so for every remote link, Rails3 just add the data-remote attribute to links : &#60;a href=&#34;ajax_page.html&#34; data-remote=&#34;true&#34;&#62;A remote link !&#60;/a&#62; If you want to do ajaxed pagination, there is no easy way with will_paginate to do remote link (or I didn&#8217;t find any one), but with [...]]]></description>
			<content:encoded><![CDATA[<p>As you know, <strong>Rails3</strong> use only UJS (unobtrusive javascript), so for every remote link, Rails3 just add the <strong>data-remote</strong> attribute to links :</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/a.html" onclick="pageTracker._trackPageview('/outgoing/december.com/html/4/element/a.html?referer=');"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax_page.html&quot;</span> data-remote<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;true&quot;</span>&gt;</span>A remote link !<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html" onclick="pageTracker._trackPageview('/outgoing/december.com/html/4/element/a.html?referer=');"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;</span></div></div>
<p>If you want to do ajaxed pagination, there is no easy way with <strong>will_paginate</strong> to do remote link (or I didn&#8217;t find any one), but with Rails3 UJS, there is a little tricks do to this easily !</p>
<p>Here is the haml and jquery code :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">= will_paginate<span style="color:#006600; font-weight:bold;">&#40;</span>@users<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#ff3333; font-weight:bold;">:javascript</span><br />
&nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'.pagination a'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attr</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'data-remote'</span>, <span style="color:#996600;">'true'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></div>
<p>This snippet of code just add the attribute data-remote to pagination links. And that&#8217;s it ! Our pagination will now be ajaxed <img src='http://www.tricksonrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<ul>
<li><a href="http://github.com/rails/jquery-ujs" target="_blank" onclick="pageTracker._trackPageview('/outgoing/github.com/rails/jquery-ujs?referer=');">official jQuery UJS for rails3</a></li>
<li><a href="http://github.com/mislav/will_paginate/tree/rails3" target="_blank" onclick="pageTracker._trackPageview('/outgoing/github.com/mislav/will_paginate/tree/rails3?referer=');">will_paginate for rails3</a></li>
</ul>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F04%2Frails3-and-will_paginate-doing-easy-remote-links-rails3-tricks-02%2F&amp;linkname=Rails3%20and%20will_paginate%2C%20Doing%20easy%20remote%20links%20%26%238211%3B%20Rails3%20Tricks%20%2302" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F04_2Frails3-and-will_paginate-doing-easy-remote-links-rails3-tricks-02_2F_amp_linkname=Rails3_20and_20will_paginate_2C_20Doing_20easy_20remote_20links_20_26_238211_3B_20Rails3_20Tricks_20_2302&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/04/rails3-and-will_paginate-doing-easy-remote-links-rails3-tricks-02/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using Rspec with multiple version of Rails – Rspec Tricks #01</title>
		<link>http://www.tricksonrails.com/2010/03/using-rspec-with-multiple-version-of-rails-rspec-tricks-01/</link>
		<comments>http://www.tricksonrails.com/2010/03/using-rspec-with-multiple-version-of-rails-rspec-tricks-01/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 16:37:50 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rspec]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[rubygem]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=211</guid>
		<description><![CDATA[With the upcoming release of Rails3, it&#8217;s important to maintain gem/plugins compatibility with both Rails 2.x series and the new Rails 3.x series. That&#8217;s why I wanted to implement specs supporting both versions for one of my plugin. With a simple rake task, I wanted to run spec for a specific Rails version. By default, [...]]]></description>
			<content:encoded><![CDATA[<p>With the upcoming release of <strong>Rails3</strong>, it&#8217;s important to maintain gem/plugins compatibility with both Rails 2.x series and the new Rails 3.x series.</p>
<p>That&#8217;s why I wanted to implement specs supporting both versions for one of my plugin.</p>
<p>With a simple rake task, I wanted to run spec for a specific Rails version.</p>
<p>By default, <strong>Rspec</strong> create this kind of Rakefile :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">'rake'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'spec/rake/spectask'</span><br />
&nbsp;<br />
spec_files = <span style="color:#6666ff; font-weight:bold;">Rake::FileList</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;spec/**/*_spec.rb&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp;<br />
desc <span style="color:#996600;">&quot;Run specs&quot;</span><br />
<span style="color:#6666ff; font-weight:bold;">Spec::Rake::SpecTask</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; t.<span style="color:#9900CC;">spec_files</span> = spec_files<br />
&nbsp; t.<span style="color:#9900CC;">spec_opts</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;-c&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp;<br />
task <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:spec</span></div></div>
<p>Here we have the default Rake task : &#8220;spec&#8221; wich will run specs for our code.</p>
<h3>Custom our Rakefile to specify the Rails version</h3>
<p>At this point, we need to pass an extra option to spec script in order to specify wich rails version we want to use in our tests.</p>
<p>The way I found to do this is to tricks <em>spec_opts</em> (which is command line options for spec) by assigning to it, a <strong>Proc</strong>, like this :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">t.<span style="color:#9900CC;">spec_opts</span> = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#0066ff; font-weight:bold;">@rails_version</span> ? <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;-c -- rails_version=#{@rails_version}&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> : <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;-c&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Then add two tasks to your Rakefile :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">desc <span style="color:#996600;">&quot;Run Rails 2.x specs&quot;</span><br />
task <span style="color:#ff3333; font-weight:bold;">:rails2_spec</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#0066ff; font-weight:bold;">@rails_version</span> = <span style="color:#006666;">2</span><br />
&nbsp; <span style="color:#6666ff; font-weight:bold;">Rake::Task</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'spec'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">invoke</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
desc <span style="color:#996600;">&quot;Run Rails 3.x specs&quot;</span><br />
task <span style="color:#ff3333; font-weight:bold;">:rails3_spec</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#0066ff; font-weight:bold;">@rails_version</span> = <span style="color:#006666;">3</span><br />
&nbsp; <span style="color:#6666ff; font-weight:bold;">Rake::Task</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'spec'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">invoke</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Ok, now we have our three tasks passing Rails version to our spec tests !</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">kwi<span style="color: #000000; font-weight: bold;">@</span> ~<span style="color: #000000; font-weight: bold;">/</span>Projects<span style="color: #000000; font-weight: bold;">/</span>i18n_routing$ rake <span style="color: #660033;">-T</span><br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>kwi<span style="color: #000000; font-weight: bold;">/</span>Projects<span style="color: #000000; font-weight: bold;">/</span>i18n_routing<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
rake rails2_spec &nbsp;<span style="color: #666666; font-style: italic;"># Run Rails 2.x specs</span><br />
rake rails3_spec &nbsp;<span style="color: #666666; font-style: italic;"># Run Rails 3.x specs</span><br />
rake spec &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Run specs for current Rails version</span></div></div>
<h3>Retrieve the Rails version parameter</h3>
<p>Then in your <em>spec_helper.rb</em> file, where you include your dependencies, you just need to retrieve the rails version with this (<em>dirty</em>) piece of code :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rails_version = ARGV.<span style="color:#9900CC;">find</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> e =~ <span style="color:#006600; font-weight:bold;">/</span>rails_version=.<span style="color:#006600; font-weight:bold;">*/</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
rails_version = rails_version.<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:#9900CC;">last</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006666;">2</span></div></div>
<p>Now we have the correct rails version we want for running our tests, so we need to include our dependencies depending on the Rails&#8217; version we want to load.</p>
<p>For this, there is a tricky things with <em>gem</em> method for specify wich version of <strong>gem</strong> you want to use :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">gem <span style="color:#996600;">'actionpack'</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>rails_version <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">3</span> ? <span style="color:#996600;">'&lt; 2.9'</span> : <span style="color:#996600;">'&gt; 2.9'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'action_controller'</span></div></div>
<p><em>(Example for loading action_controller only, here I use 2.9 version number in order to support Rails3beta)</em></p>
<p>Here we are, now we can run our <strong>tests</strong> on <strong>both Rails version</strong> just with a simple rake task :</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rake rails2_spec<br />
<span style="color: #666666; font-style: italic;"># Or</span><br />
rake rails3_spec</div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F03%2Fusing-rspec-with-multiple-version-of-rails-rspec-tricks-01%2F&amp;linkname=Using%20Rspec%20with%20multiple%20version%20of%20Rails%20%26%238211%3B%20Rspec%20Tricks%20%2301" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F03_2Fusing-rspec-with-multiple-version-of-rails-rspec-tricks-01_2F_amp_linkname=Using_20Rspec_20with_20multiple_20version_20of_20Rails_20_26_238211_3B_20Rspec_20Tricks_20_2301&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/03/using-rspec-with-multiple-version-of-rails-rspec-tricks-01/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails 3 edge routing – Rails3 Tricks #01</title>
		<link>http://www.tricksonrails.com/2010/03/rails-3-edge-routing-rails3-tricks-01/</link>
		<comments>http://www.tricksonrails.com/2010/03/rails-3-edge-routing-rails3-tricks-01/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 16:40:01 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[routes]]></category>
		<category><![CDATA[routing]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=193</guid>
		<description><![CDATA[Here are just two little issues I encoutered today when I worked on Rails 3 edge routes. I think it can be useful to share them. I assume you already know changes in routes for Rails3. (if not, watch: Ryan Bates Screencast about routing in Rails3) Declare your root path Matching empty route string are [...]]]></description>
			<content:encoded><![CDATA[<p>Here are just two little issues I encoutered today when I worked on <strong>Rails 3 edge routes</strong>. I think it can be useful to share them.</p>
<p>I assume you already know changes in routes for Rails3.<br />
(if not, watch: <a href="http://railscasts.com/episodes/203-routing-in-rails-3" onclick="pageTracker._trackPageview('/outgoing/railscasts.com/episodes/203-routing-in-rails-3?referer=');">Ryan Bates Screencast about routing in Rails3</a>)</p>
<h3>Declare your root path</h3>
<p>Matching empty route string are now impossible in <strong>Rails 3 edge</strong> (not in beta yet), so you can not do anymore :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#6666ff; font-weight:bold;">MkdBaseApp::Application</span>.<span style="color:#9900CC;">routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; match <span style="color:#996600;">''</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;home#show&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:home</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Instead, you need to use <em><strong>root</strong></em> :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#6666ff; font-weight:bold;">MkdBaseApp::Application</span>.<span style="color:#9900CC;">routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; root <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'home#show'</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> &nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:home</span> <br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<h3>Route Globbing</h3>
<p>Route globbing is a way to catch any action matching a part of an url. It&#8217;s useful for catching errors for example.</p>
<p>In Rails 2, it was with <em>connect</em></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#6666ff; font-weight:bold;">ActionController::Routing::Routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>map<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">&quot;*path&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'error'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'handle404'</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>In Rails 3, it&#8217;s pretty the same, but with <em>match</em> :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#6666ff; font-weight:bold;">MkdBaseApp::Application</span>.<span style="color:#9900CC;">routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; match <span style="color:#996600;">&quot;*path&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'error#handle404'</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Put this snippet at the end of your routes declaration and the ErrorController will be called if not routes match the requested uri.</p>
<h3>Remove the map variable</h3>
<p>Remember that you do not need anymore to get the <em>map</em> variable in your route block : </p>
<p>Rails 2:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#6666ff; font-weight:bold;">ActionController::Routing::Routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>map<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:users</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Rails3, remove the <em>|map|</em> :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#6666ff; font-weight:bold;">MkdBaseApp::Application</span>.<span style="color:#9900CC;">routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; resources <span style="color:#ff3333; font-weight:bold;">:users</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F03%2Frails-3-edge-routing-rails3-tricks-01%2F&amp;linkname=Rails%203%20edge%20routing%20%26%238211%3B%20Rails3%20Tricks%20%2301" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F03_2Frails-3-edge-routing-rails3-tricks-01_2F_amp_linkname=Rails_203_20edge_20routing_20_26_238211_3B_20Rails3_20Tricks_20_2301&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/03/rails-3-edge-routing-rails3-tricks-01/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding an external repository with git using submodule</title>
		<link>http://www.tricksonrails.com/2010/03/adding-an-external-repository-with-git-using-submodule/</link>
		<comments>http://www.tricksonrails.com/2010/03/adding-an-external-repository-with-git-using-submodule/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 11:28:15 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[external]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[submodule]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=180</guid>
		<description><![CDATA[We often need to include externals git repositories in our own one in order to keep links to our favorite plugins. With git, there is an easy way to do this, it&#8217;s called submodule. The usage is trivial : git submodule add git://github.com/kwi/i18n_routing.git vendor/plugins/i18n_routing git submodule init git submodule update And there we go, we [...]]]></description>
			<content:encoded><![CDATA[<p>We often need to include <strong>externals</strong> <strong>git</strong> repositories in our own one in order to keep links to our favorite plugins.<br />
With git, there is an easy way to do this, it&#8217;s called <strong>submodule</strong>.</p>
<p>The usage is trivial :</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">git</span> submodule add <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>kwi<span style="color: #000000; font-weight: bold;">/</span>i18n_routing.git vendor<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>i18n_routing<br />
<span style="color: #c20cb9; font-weight: bold;">git</span> submodule init<br />
<span style="color: #c20cb9; font-weight: bold;">git</span> submodule update</div></div>
<p>And there we go, we have installed that nice <a href="http://github.com/kwi/i18n_routing" onclick="pageTracker._trackPageview('/outgoing/github.com/kwi/i18n_routing?referer=');">i18n rails routing</a> plugin in order to translate our rails routes.</p>
<h3>Update your modules</h3>
<p>If you want to update a particular submodule, go to its folder and do a :</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">git</span> pull</div></div>
<p>For pulling all your submodules do a :</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">git</span> submodule foreach <span style="color: #ff0000;">'git pull'</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F03%2Fadding-an-external-repository-with-git-using-submodule%2F&amp;linkname=Adding%20an%20external%20repository%20with%20git%20using%20submodule" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F03_2Fadding-an-external-repository-with-git-using-submodule_2F_amp_linkname=Adding_20an_20external_20repository_20with_20git_20using_20submodule&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/03/adding-an-external-repository-with-git-using-submodule/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx rewriting and redirection tips</title>
		<link>http://www.tricksonrails.com/2010/03/nginx-rewriting-and-redirection-tips/</link>
		<comments>http://www.tricksonrails.com/2010/03/nginx-rewriting-and-redirection-tips/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 18:52:52 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Nginx]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[conf]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=151</guid>
		<description><![CDATA[Rewriting url with nginx Last week, I needed to rename a folder in a rails&#8217; public directory served by nginx. But, doing that, I wanted to keep some links already indexed by search engines and especially jpg images still accessible. And only jpg images, so symbolic links were not appropriate. &#160; # Ensure JPEG are [...]]]></description>
			<content:encoded><![CDATA[<h3>Rewriting url with nginx</h3>
<p>Last week, I needed to rename a folder in a rails&#8217; public directory served by <strong>nginx</strong>. But, doing that, I wanted to keep some links already indexed by search engines and especially jpg images still accessible. And only jpg images, so symbolic links were not appropriate.
</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#008000; font-style:italic;"># Ensure JPEG are still accessible after</span><br />
&nbsp; <span style="color:#008000; font-style:italic;"># renaming my folder videos to thumbs</span><br />
&nbsp; location ~<span style="color:#006600; font-weight:bold;">*</span> ^<span style="color:#006600; font-weight:bold;">/</span>videos<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">+</span>\.<span style="color:#006600; font-weight:bold;">&#40;</span>jpg<span style="color:#006600; font-weight:bold;">&#41;</span>$ <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; rewrite ^<span style="color:#006600; font-weight:bold;">/</span>videos<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>$ <span style="color:#006600; font-weight:bold;">/</span>thumbs<span style="color:#006600; font-weight:bold;">/</span>$<span style="color:#006666;">1</span> last;<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></div>
<p>This snippet of code solve my problem. For every .jpg asked in the videos folder, it <strong>rewrites</strong> <em>videos</em> by <em>thumbs</em> and then continue serving the file.</p>
<h3>Redirection with nginx</h3>
<p>Another things can be useful, here is a little piece of code that check if a file is present on the file system and if not, <strong>redirect</strong> the request to another server :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">location ~<span style="color:#006600; font-weight:bold;">*</span> ^<span style="color:#006600; font-weight:bold;">/</span>thumbs<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">+</span>\.<span style="color:#006600; font-weight:bold;">&#40;</span>jpg<span style="color:#006600; font-weight:bold;">&#41;</span>$ <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">-</span>f <span style="color:#ff6633; font-weight:bold;">$request_filename</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># The file is present, so serve the file with caching header</span><br />
&nbsp; &nbsp; expires 1y;<br />
&nbsp; &nbsp; add_header Cache<span style="color:#006600; font-weight:bold;">-</span>Control public;<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">break</span>;<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># the file is not present, redirect to another server</span><br />
&nbsp; rewrite ^<span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span> http:<span style="color:#006600; font-weight:bold;">//</span>another.<span style="color:#9900CC;">thumbs</span><span style="color:#006600; font-weight:bold;">-</span>server.<span style="color:#9900CC;">com</span>$<span style="color:#006666;">1</span> permanent;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F03%2Fnginx-rewriting-and-redirection-tips%2F&amp;linkname=Nginx%20rewriting%20and%20redirection%20tips" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F03_2Fnginx-rewriting-and-redirection-tips_2F_amp_linkname=Nginx_20rewriting_20and_20redirection_20tips&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/03/nginx-rewriting-and-redirection-tips/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RVM, The ruby version manager</title>
		<link>http://www.tricksonrails.com/2010/03/rvm-the-ruby-version-manager-multiple-ruby-on-one-syste/</link>
		<comments>http://www.tricksonrails.com/2010/03/rvm-the-ruby-version-manager-multiple-ruby-on-one-syste/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 16:36:13 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[ruby1.9]]></category>
		<category><![CDATA[Rubyspec]]></category>
		<category><![CDATA[rvm]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=110</guid>
		<description><![CDATA[Thanks to our greatest friend, Ryan, I discovered last month, RVM, a Ruby version manager. Starting from now, stop the pain of getting multiple versions of ruby running on the same system! With RVM, you can manage like with a packet manager every version of ruby you want. (including, ree, jruby, ironruby, etc&#8230; !!) Follow [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to our greatest friend, <a href="http://railscasts.com/episodes/200-rails-3-beta-and-rvm" target="_blank" onclick="pageTracker._trackPageview('/outgoing/railscasts.com/episodes/200-rails-3-beta-and-rvm?referer=');">Ryan</a>, I discovered last month, <strong>RVM</strong>, a Ruby version manager.</p>
<p>Starting from now, stop the pain of getting multiple versions of ruby running on the same system!<br />
With <strong>RVM</strong>, you can manage like with a packet manager every version of ruby you want. (including, <strong>ree</strong>, <strong>jruby</strong>, <strong>ironruby</strong>, etc&#8230; !!)</p>
<p style="text-align: center;"><img src="http://www.tricksonrails.com/wp-content/uploads/2010/03/20090904-fyqnyu8pcr36ahe91er9x28bhi-300x139.png" alt="" title="RVM, no more pain for using multiple Ruby version" width="300" height="139" class="aligncenter size-medium wp-image-131" /></p>
<p>Follow the simple guide for the trivial install on the official <a href="http://rvm.beginrescueend.com/rvm/install/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/rvm.beginrescueend.com/rvm/install/?referer=');">website of RVM</a>.</p>
<p>Then install versions of ruby you want simply :</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rvm <span style="color: #c20cb9; font-weight: bold;">install</span> 1.8.7,1.9.2<br />
<br />
rvm list<br />
<span style="color: #666666; font-style: italic;">#rvm Rubies</span><br />
<span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;">#=&gt; ruby-1.8.7-p249 [ x86_64 ]</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; ruby-1.9.2-preview1 [ x86_64 ]</span><br />
<span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;">#Default Ruby (for new shells)</span><br />
<span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; ruby-1.8.7-p249 [ x86_64 ]</span><br />
<span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;">#System Ruby</span><br />
<span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; system [ x86_64 i386 ppc ]</span></div></div>
<p>After that, just choose the ruby version you want to use by telling RVM :</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rvm 1.9.2<br />
ruby <span style="color: #660033;">-v</span><br />
<span style="color: #666666; font-style: italic;"># ruby 1.9.2dev (2009-07-18 trunk 24186) [i386-darwin10.2.0]</span><br />
<br />
rvm 1.8.7<br />
ruby <span style="color: #660033;">-v</span><br />
<span style="color: #666666; font-style: italic;"># ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10.2.0]</span></div></div>
<h3>More infos about RVM :</h3>
<p>Each ruby version with RVM has its own ruby gems directory, that&#8217;s why you can install separate gems for every version of Ruby you run.<br />
And please note, for installing a gem, you do not need to use <em>sudo</em>.</p>
<p>By default, RVM, will keep the ruby system version on each new term session, if you want to change this behaviour, just tell RVM.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rvm 1.9.2 <span style="color: #660033;">--default</span></div></div>
<h3>Start enjoying Ruby again !</h3>
<p>So, now, no more excuses, time to go ahead and make the jump, start using <strong>Ruby 1.9</strong> as it now <a href="http://www.ruby-forum.com/topic/204850" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.ruby-forum.com/topic/204850?referer=');">pass RubySpec tests</a> !</p>
<p>And by the way test your application and your plugins over multiple version of ruby with ease !</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F03%2Frvm-the-ruby-version-manager-multiple-ruby-on-one-syste%2F&amp;linkname=RVM%2C%20The%20ruby%20version%20manager" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F03_2Frvm-the-ruby-version-manager-multiple-ruby-on-one-syste_2F_amp_linkname=RVM_2C_20The_20ruby_20version_20manager&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/03/rvm-the-ruby-version-manager-multiple-ruby-on-one-syste/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Rails 2.3 marks your string safe</title>
		<link>http://www.tricksonrails.com/2010/02/how-rails-2-3-marks-your-string-safe-xss-protection/</link>
		<comments>http://www.tricksonrails.com/2010/02/how-rails-2-3-marks-your-string-safe-xss-protection/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 18:55:13 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[active support]]></category>
		<category><![CDATA[good to know]]></category>
		<category><![CDATA[safe]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=162</guid>
		<description><![CDATA[Here is a little thing I find good to know ! Maybe you are curious too and you want to know how rails 2.3 know if your strings are html safe or not. In order to prevent XSS injections, Rails 2.3 implement a way to determine if a string is safe : It&#8217;s just a [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a little thing I find good to know ! Maybe you are curious too and you want to know how rails 2.3 know if your strings are html safe or not.</p>
<p>In order to prevent <strong>XSS injections</strong>, Rails 2.3 implement a way to determine if a string is safe : It&#8217;s just a little extension of the core string class given by active support (output_safety.rb).</p>
<p>This extension add a <em>@_rails_html_safe</em> instance variable to the majority of <strong>each string</strong> created by rails helper in your application !! (I find this way kind of brutal personally)</p>
<p>This variable is a boolean, and if it&#8217;s true, your string is HTML safe. So on each concatenation your string will be marked as html safe or not.</p>
<p>If you want to mark yourself a string as safe, use the method : <strong>html_safe!</strong><br />
And if you want to know if a string is safe, use : <strong>htm_safe?</strong></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> helper.<span style="color:#9900CC;">tag</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'br'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">html_safe</span>?<br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#996600;">&quot;&lt;script&gt;Not safe&lt;/script&gt;&quot;</span>.<span style="color:#9900CC;">html_safe</span>?<br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#996600;">&quot;&lt;script&gt;Not safe&lt;/script&gt;&quot;</span>.<span style="color:#9900CC;">html_safe</span>!.<span style="color:#9900CC;">html_safe</span>?<br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><br />
ruby<span style="color:#006600; font-weight:bold;">-</span>1.8.7<span style="color:#006600; font-weight:bold;">-</span>p249 <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Marshal</span>::dump<span style="color:#006600; font-weight:bold;">&#40;</span>helper.<span style="color:#9900CC;">tag</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'br'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp;<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\0</span>04<span style="color:#000099;">\b</span>I<span style="color:#000099;">\&quot;</span><span style="color:#000099;">\v</span>&lt;br /&gt;<span style="color:#000099;">\0</span>06:<span style="color:#000099;">\0</span>26@_rails_html_safeT&quot;</span><br />
<span style="color:#008000; font-style:italic;"># Here you can see the @_rails_html_safe variable with the string.</span></div></div>
<p>Be careful, this code completely change in <strong>Rails3</strong> and the little I seen, it&#8217;s a lot cleaner <img src='http://www.tricksonrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F02%2Fhow-rails-2-3-marks-your-string-safe-xss-protection%2F&amp;linkname=How%20Rails%202.3%20marks%20your%20string%20safe" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F02_2Fhow-rails-2-3-marks-your-string-safe-xss-protection_2F_amp_linkname=How_20Rails_202.3_20marks_20your_20string_20safe&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/02/how-rails-2-3-marks-your-string-safe-xss-protection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.4.2 is Out, it’s again blazing fast !</title>
		<link>http://www.tricksonrails.com/2010/02/jquery-1-4-2-is-out-its-again-blazing-fast/</link>
		<comments>http://www.tricksonrails.com/2010/02/jquery-1-4-2-is-out-its-again-blazing-fast/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 14:17:57 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[ujs]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=66</guid>
		<description><![CDATA[Yesterday, the jQuery team has released a new version of its famous javascript library, and again, it&#8217;s rock competitors Source: Taskspeed Results jQuery 1.4.2 release note A good start for succeed your projects, it&#8217;s using good tools. And jQuery is one of them. So, do not wait, and start using the today best javascript library [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, the jQuery team has released a new version of its famous javascript library, and again, it&#8217;s rock competitors <img src='http://www.tricksonrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img src="http://farm5.static.flickr.com/4052/4366089661_57f3dd6b18.jpg" alt="jQuery benchmarks vs competitors" /></p>
<ul>
<li><a href="http://www.flickr.com/photos/jeresig/4366089661/" onclick="pageTracker._trackPageview('/outgoing/www.flickr.com/photos/jeresig/4366089661/?referer=');">Source: Taskspeed Results</a></li>
<li><a href="http://blog.jquery.com/2010/02/19/jquery-142-released/" onclick="pageTracker._trackPageview('/outgoing/blog.jquery.com/2010/02/19/jquery-142-released/?referer=');">jQuery 1.4.2 release note</a></li>
</ul>
<p>A good start for succeed your projects, it&#8217;s using good tools. And <strong>jQuery</strong> is one of them.<br />
So, do not wait, and start using the today best javascript library !</p>
<h3>For <strong>Rails 3</strong>, use <strong>jQuery UJS</strong> :</h3>
<p>Use the jQuery official <strong>UJS</strong> plugin : <a href="http://github.com/rails/jquery-ujs" target="_blank" onclick="pageTracker._trackPageview('/outgoing/github.com/rails/jquery-ujs?referer=');">http://github.com/rails/jquery-ujs</a>, explanations for usage on another blog : <a href="http://blog.datagraph.org/2010/02/jquery-with-rails-3" target="_blank" onclick="pageTracker._trackPageview('/outgoing/blog.datagraph.org/2010/02/jquery-with-rails-3?referer=');">http://blog.datagraph.org/2010/02/jquery-with-rails-3</a></p>
<p>
And do not forget to use the helper <em>csrf_meta_tag</em> in your header in order to output metas for the authenticity token.
</p>
<h3>For Rails 2.x, use Jrails :</h3>
<p>
Install the drop-in remplacement <strong>jrails</strong> plugin: <a href="http://github.com/aaronchi/jrails" target="_blank" onclick="pageTracker._trackPageview('/outgoing/github.com/aaronchi/jrails?referer=');">http://github.com/aaronchi/jrails</a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2010%2F02%2Fjquery-1-4-2-is-out-its-again-blazing-fast%2F&amp;linkname=jQuery%201.4.2%20is%20Out%2C%20it%26%238217%3Bs%20again%20blazing%20fast%20%21" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2010_2F02_2Fjquery-1-4-2-is-out-its-again-blazing-fast_2F_amp_linkname=jQuery_201.4.2_20is_20Out_2C_20it_26_238217_3Bs_20again_20blazing_20fast_20_21&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2010/02/jquery-1-4-2-is-out-its-again-blazing-fast/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Another way to compare Class – Ruby Tricks #04</title>
		<link>http://www.tricksonrails.com/2009/12/another-way-to-compare-ruby-class-%e2%80%93-ruby-tricks-04/</link>
		<comments>http://www.tricksonrails.com/2009/12/another-way-to-compare-ruby-class-%e2%80%93-ruby-tricks-04/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 15:25:01 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[comparaison]]></category>
		<category><![CDATA[novice]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=96</guid>
		<description><![CDATA[Today, just a really small tricks but I find it kind of cool. (But useless ) When we want to check an object class, we often use .is_a?(ClassName). But you can do this with the === operator too : Hash === &#123;&#125; # =&#62; true But be careful, put the Class before, as it&#8217;s not [...]]]></description>
			<content:encoded><![CDATA[<p>Today, just a really small tricks but I find it kind of cool. (But useless <img src='http://www.tricksonrails.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
<p>When we want to check an object class, we often use <em>.is_a?(ClassName)</em>.</p>
<p>But you can do this with the <em>===</em> operator too :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC00FF; font-weight:bold;">Hash</span> === <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; true</span></div></div>
<p>But be careful, put the Class before, as it&#8217;s not the same :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> === <span style="color:#CC00FF; font-weight:bold;">Hash</span><br />
<span style="color:#008000; font-style:italic;"># =&gt; false</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2009%2F12%2Fanother-way-to-compare-ruby-class-%25e2%2580%2593-ruby-tricks-04%2F&amp;linkname=Another%20way%20to%20compare%20Class%20%E2%80%93%20Ruby%20Tricks%20%2304" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2009_2F12_2Fanother-way-to-compare-ruby-class-_25e2_2580_2593-ruby-tricks-04_2F_amp_linkname=Another_20way_20to_20compare_20Class_20_E2_80_93_20Ruby_20Tricks_20_2304&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2009/12/another-way-to-compare-ruby-class-%e2%80%93-ruby-tricks-04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hash creation on the fly – Ruby Tricks #03</title>
		<link>http://www.tricksonrails.com/2009/12/rails-hash-creation-on-the-fly-ruby-tricks-03/</link>
		<comments>http://www.tricksonrails.com/2009/12/rails-hash-creation-on-the-fly-ruby-tricks-03/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 16:51:08 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[advanced]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=85</guid>
		<description><![CDATA[So many times, we want to extract from an Array an Hash in order to access more easily some values. For example, your are in your Rails environment and you want to extract from your Articles tables an Hash with hash[article_id] => Article. Here is the tricks do to this with just one line of [...]]]></description>
			<content:encoded><![CDATA[<p>So many times, we want to extract from an Array an Hash in order to access more easily some values.</p>
<p>For example, your are in your Rails environment and you want to extract from your Articles tables an Hash with hash[article_id] => Article.</p>
<p>Here is the tricks do to this with just <strong>one line of code</strong> :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Article.<span style="color:#9900CC;">all</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>h, article<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>article.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#93;</span> = article; h <span style="color:#006600; font-weight:bold;">&#125;</span></div></div>
<p>A real world example I use for caching data in an active record model with a constant :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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> Locale <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><br />
&nbsp; ...<br />
&nbsp; <span style="color:#9900CC;">LocaleCached</span> = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>h, l<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>l.<span style="color:#9900CC;">short</span>.<span style="color:#9900CC;">to_sym</span><span style="color:#006600; font-weight:bold;">&#93;</span> = l; h <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; ...<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2009%2F12%2Frails-hash-creation-on-the-fly-ruby-tricks-03%2F&amp;linkname=Hash%20creation%20on%20the%20fly%20%26%238211%3B%20Ruby%20Tricks%20%2303" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2009_2F12_2Frails-hash-creation-on-the-fly-ruby-tricks-03_2F_amp_linkname=Hash_20creation_20on_20the_20fly_20_26_238211_3B_20Ruby_20Tricks_20_2303&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2009/12/rails-hash-creation-on-the-fly-ruby-tricks-03/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String concatenation performance – Ruby Tricks #02</title>
		<link>http://www.tricksonrails.com/2009/12/string-concatenation-performance-ruby-tricks-02/</link>
		<comments>http://www.tricksonrails.com/2009/12/string-concatenation-performance-ruby-tricks-02/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 13:56:28 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[concatenation]]></category>
		<category><![CDATA[novice]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=38</guid>
		<description><![CDATA[When it&#8217;s come to make string concatenation that you use hundred time in your every day projects, you have the choice in Ruby ! Most common cases : &#34;Hi #{login}&#34; 'Hi ' + login s = 'Hi ' s += login s = 'Hi ' s &#60;&#60; login But, all these methods for concatening strings [...]]]></description>
			<content:encoded><![CDATA[<p>When it&#8217;s come to make string concatenation that you use hundred time in your every day projects, you have the choice in Ruby !</p>
<p><strong>Most common cases :</strong></p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#996600;">&quot;Hi #{login}&quot;</span><br />
<br />
<span style="color:#996600;">'Hi '</span> <span style="color:#006600; font-weight:bold;">+</span> login<br />
<br />
s = <span style="color:#996600;">'Hi '</span><br />
s <span style="color:#006600; font-weight:bold;">+</span>= login<br />
<br />
s = <span style="color:#996600;">'Hi '</span><br />
s <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> login</div></div>
<p>But, all these methods for concatening strings does not really behave the same :</p>
<h3>First case, += VS &lt;&lt; :</h3>
<p><div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">s = <span style="color:#996600;">'Hi '</span><br />
s <span style="color:#006600; font-weight:bold;">+</span>= login</div></div>
<p>The <strong>+</strong> operator for strings create a new string object by concatening two strings, here &#8216;Hi &#8216; and login. So we have instanciated two strings in order to just get one.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">s = <span style="color:#996600;">'Hi '</span><br />
s <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> login</div></div>
<p>On the other hand, the <strong>&lt;&lt;</strong> append directly the content of the second string in the first string, so you do not re-instantiate a new string. But you modify your first object, so be careful especially when it comes from a variable.
</p>
<h3>Second case, + VS #{} :</h3>
<p><div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#996600;">'Hello '</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'ruby '</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'world'</span></div></div>
<p>Create the &#8216;Hello ruby &#8216; string then re-create the last string : &#8216;Hello ruby world&#8217;<br />
=> So create unecessary strings.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#996600;">&quot;Hello #{'ruby '}#{'world'}&quot;</span></div></div>
<p>Directly create the full string &#8216;Hello ruby world&#8217; without an intermediate state like seen before
</p>
<h3>Conclusion</h3>
<ul>
<li>Privilegiate <strong>&lt;&lt;</strong> when you can !</li>
<li>Use the <strong>&#8220;#{}&#8221;</strong> concatenation manner when you concatenate more than 2 strings together.</li>
</ul>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2009%2F12%2Fstring-concatenation-performance-ruby-tricks-02%2F&amp;linkname=String%20concatenation%20performance%20%26%238211%3B%20Ruby%20Tricks%20%2302" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2009_2F12_2Fstring-concatenation-performance-ruby-tricks-02_2F_amp_linkname=String_20concatenation_20performance_20_26_238211_3B_20Ruby_20Tricks_20_2302&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2009/12/string-concatenation-performance-ruby-tricks-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Symbol#to_proc – Ruby Tricks #01</title>
		<link>http://www.tricksonrails.com/2009/12/symbol_to_proc-ruby-tricks-01/</link>
		<comments>http://www.tricksonrails.com/2009/12/symbol_to_proc-ruby-tricks-01/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 00:11:18 +0000</pubDate>
		<dc:creator>kwi</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[active support]]></category>
		<category><![CDATA[novice]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[symbol]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://www.tricksonrails.com/?p=5</guid>
		<description><![CDATA[First tricks today, here is an easy one : If you are using Active Support (shipped with Rails), or a ruby version superior or equal to 1.8.7, you can use the symbol proc shortcut : Here is the standard way declaring a block : &#62;&#62; &#91;'a', 'b', 'c'&#93;.collect &#123;&#124;letter&#124; letter.capitalize&#125; =&#62; &#91;&#34;A&#34;, &#34;B&#34;, &#34;C&#34;&#93; Here [...]]]></description>
			<content:encoded><![CDATA[<p>First tricks today, here is an easy one :</p>
<p>If you are using Active Support (shipped with Rails), or a ruby version superior or equal to 1.8.7, you can use the symbol proc shortcut :</p>
<p>Here is the standard way declaring a block :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">&gt;&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'a'</span>, <span style="color:#996600;">'b'</span>, <span style="color:#996600;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>letter<span style="color:#006600; font-weight:bold;">|</span> letter.<span style="color:#9900CC;">capitalize</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;A&quot;</span>, <span style="color:#996600;">&quot;B&quot;</span>, <span style="color:#996600;">&quot;C&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></div></div>
<p>Here is the handy method :</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">&gt;&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'a'</span>, <span style="color:#996600;">'b'</span>, <span style="color:#996600;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:capitalize<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;A&quot;</span>, <span style="color:#996600;">&quot;B&quot;</span>, <span style="color:#996600;">&quot;C&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></div></div>
<p>But, keep in mind that the shortcut method is a little bit slower in term of performance than the normal way cause it creates a new Proc on each call !</p>
<h3>Benchmark :</h3>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">t = <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">realtime</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'a'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:to_s<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Time using to_proc: #{t}&quot;</span><br />
<br />
t = <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">realtime</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'a'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; e.<span style="color:#9900CC;">to_s</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Time using normal block: #{t}&quot;</span><br />
<br />
<span style="color:#008000; font-style:italic;"># Time using to_proc: 0.631899118423462</span><br />
<span style="color:#008000; font-style:italic;"># Time using normal block: 0.246822834014893</span><br />
<span style="color:#008000; font-style:italic;"># Results are the same if you test the normal block first</span></div></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tricksonrails.com%2F2009%2F12%2Fsymbol_to_proc-ruby-tricks-01%2F&amp;linkname=Symbol%23to_proc%20%26%238211%3B%20Ruby%20Tricks%20%2301" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fwww.tricksonrails.com_2F2009_2F12_2Fsymbol_to_proc-ruby-tricks-01_2F_amp_linkname=Symbol_23to_proc_20_26_238211_3B_20Ruby_20Tricks_20_2301&amp;referer=');"><img src="http://www.tricksonrails.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.tricksonrails.com/2009/12/symbol_to_proc-ruby-tricks-01/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 2.279 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-16 22:43:09 -->

