<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>La rolls des blogs &#187; tips</title>
	<atom:link href="http://my.rails-royce.org/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://my.rails-royce.org</link>
	<description>Another Ruby and Rails blog</description>
	<lastBuildDate>Fri, 13 Jan 2012 23:47:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>HTTPClient and SSL verify certificate</title>
		<link>http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/</link>
		<comments>http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 11:58:51 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[gem]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[certificate]]></category>
		<category><![CDATA[httpclient]]></category>
		<category><![CDATA[openssl]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=255</guid>
		<description><![CDATA[If you as us in my company use self signed certificate and ran into an OpenSSL::SSL::SSLError when using httpclient gem. Here is how to bypass ssl certificate verification : The issue : require 'rubygems' require 'httpclient' &#160; client = HTTPClient.new url = &#34;https://www.server1.com&#34; client.get&#40;url&#41; at depth 1 - 19: self signed certificate in certificate chain &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>If you as us in my company use self signed certificate and ran into an OpenSSL::SSL::SSLError when using httpclient gem.</p>
<p>Here is how to bypass ssl certificate verification : </p>
<p>The issue :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'httpclient'</span>
&nbsp;
client = HTTPClient.<span style="color:#9900CC;">new</span>
url = <span style="color:#996600;">&quot;https://www.server1.com&quot;</span>
client.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span>
at depth <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">19</span>: <span style="color:#0000FF; font-weight:bold;">self</span> signed certificate <span style="color:#9966CC; font-weight:bold;">in</span> certificate chain
<span style="color:#6666ff; font-weight:bold;">OpenSSL::SSL::SSLError</span>: SSL_connect returned=<span style="color:#006666;">1</span> errno=<span style="color:#006666;">0</span> state=SSLv3 read server certificate B: certificate verify failed
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">/</span>session.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">247</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`connect'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:247:in `</span>ssl_connect<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:639:in `connect'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">/</span>timeout.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">128</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`timeout'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:631:in `</span>connect<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient/session.rb:522:in `query'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">/</span>session.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">147</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`query'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:953:in `</span>do_get_block<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:765:in `do_request'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">848</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`protect_keep_alive_disconnected'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:764:in `</span>do_request<span style="color:#996600;">'
	from /home/hery/.gem/ruby/1.8/gems/httpclient-2.1.5.2/lib/httpclient.rb:666:in `request'</span>
	from <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>hery<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">gem</span><span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>httpclient<span style="color:#006600; font-weight:bold;">-</span>2.1.5.2<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>httpclient.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">591</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`get'
	from (irb):5</span></pre></div></div>

<p>Solution :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">client.<span style="color:#9900CC;">ssl_config</span>.<span style="color:#9900CC;">verify_mode</span> = <span style="color:#6666ff; font-weight:bold;">OpenSSL::SSL::VERIFY_NONE</span>
client.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2011/01/12/httpclient-and-ssl-verify-certificate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enumerable grep method in Ruby</title>
		<link>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/</link>
		<comments>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 13:43:08 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[best practices]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=201</guid>
		<description><![CDATA[There is some methods that are not very known among ruby developpers. One of them is Enumerable#grep method. I would like to share one of the use case I have to face with, and how I implement grep. Purpose, I have a Hash representing a simple network I&#8217;d like to select all the people related &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>There is some methods that are not very known among ruby developpers.<br />
One of them is <strong>Enumerable#grep</strong> method.<br />
I would like to share one of the use case I have to face with, and how I implement grep.</p>
<p>Purpose, I have a Hash representing a simple network<br />
I&#8217;d like to select all the people related to :patrick with network label as key.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Say I have this hash</span>
h = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:patrick</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:patrick</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span> , <span style="color:#ff3333; font-weight:bold;">:john</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:paul</span>, <span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:friends</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:paul</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elisabeth</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># I want this result :</span>
<span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>,:elvis<span style="color:#006600; font-weight:bold;">&#93;</span>,
  <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>A solution is to iterate throug each values then select :patrick</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  h.<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:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>memo,<span style="color:#006600; font-weight:bold;">&#40;</span>k,v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">|</span>
   <span style="color:#9966CC; font-weight:bold;">if</span> v.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    memo.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>k <span style="color:#006600; font-weight:bold;">=&gt;</span> v<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">else</span> 
    memo
   <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Though it works, I think this piece code is not &#8220;rubyesque&#8221;.</p>
<p>Well, this is a basic case but imagine you have to select X number of variables or more  ( :patrick,:paul &#8230; etc ) or worse select with a more complex criteria.</p>
<p>After digging into the rdoc documentation, it seems that <strong>Enumerable#grep</strong> matches our requirements :</p>
<ul>
<li> It takes a parameter to compare with each elements of the <strong>Enumerable</strong> </li>
<li> It can take a block that returns the value of matching elements </li>
</ul>
<p>Well, how can we implement that ?<br />
This code below demonstrate why I love Ruby !</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> NetworkPattern
  <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;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@matches</span> = args
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> ===<span style="color:#006600; font-weight:bold;">&#40;</span>pair<span style="color:#006600; font-weight:bold;">&#41;</span>
     key, value = pair
     value.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>@matches<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">any</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> result<span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    hash.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">compact</span>.<span style="color:#9900CC;">flatten</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>hash<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span> hash.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</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>k,v<span style="color:#006600; font-weight:bold;">|</span>  <span style="color:#006600; font-weight:bold;">&#91;</span>k,<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_a
     <span style="color:#0066ff; font-weight:bold;">@matches</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
pattern = NetworkPattern.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span> h.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span>pattern<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>k,v<span style="color:#006600; font-weight:bold;">|</span>  <span style="color:#006600; font-weight:bold;">&#91;</span>k,pattern.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>v<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</span> 
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># With somer refactoring, we implement NetworPattern#exec and use it </span>
pattern.<span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Well it may seem more complicated but if you want to select both :patrick and :paul  relationships, the code does not change <strong>howmany arguments you pass in NetworkPattern.new</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">pattern = NetworkPattern.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:patrick</span>, <span style="color:#ff3333; font-weight:bold;">:paul</span><span style="color:#006600; font-weight:bold;">&#41;</span>
pattern.<span style="color:#CC0066; font-weight:bold;">exec</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:work</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elvis</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:football</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:john</span>, <span style="color:#ff3333; font-weight:bold;">:fred</span>, <span style="color:#ff3333; font-weight:bold;">:marcus</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:friends</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:joana</span>, <span style="color:#ff3333; font-weight:bold;">:elisabeth</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>All the logic belongs to the class <strong>NetworkPattern</strong> and it uses <strong>Enumerable#grep</strong> with block return.<br />
In your project you can use something like that since it can be easily tested and more readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/08/19/enumerable-grep-method-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bundler default Gemfile and custom Gemfile, example of wirble</title>
		<link>http://my.rails-royce.org/2010/06/29/bundler-default-gemfile-and-custom-gemfile-example-of-wirble/</link>
		<comments>http://my.rails-royce.org/2010/06/29/bundler-default-gemfile-and-custom-gemfile-example-of-wirble/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 13:38:34 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[bundler]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[gemfile]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[wirble]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=152</guid>
		<description><![CDATA[Hi all, A simple trick I want to share with you if you are using Bundler (or rails 3) It matters if you want to install wirble gem in your workstation but not in your server Sometimes, you want to include some gem that are not mandatory for your application to run but you want &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2010/06/29/bundler-default-gemfile-and-custom-gemfile-example-of-wirble/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Hi all,<br />
A simple trick I want to share with you if you are using Bundler (or rails 3)<br />
It matters if you want to install wirble gem in your workstation but not in your server</p>
<p>Sometimes, you want to include some gem that are not mandatory for your application to run but you want to use in your workstation. E.g wirble, awesome_print etc &#8230; or other debugging tools</p>
<p>Just write something like this in your Gemfile</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Gemfile</span>
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exist</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'../myGemfile'</span>,<span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  instance_eval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>file<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now in your myGemfile</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># myGemfile</span>
gem <span style="color:#996600;">'wirble'</span>
gem <span style="color:#996600;">'awesome_print'</span></pre></div></div>

<p>That&#8217;s all! You can now require &#8216;wirble&#8217; or &#8216;awesome_print&#8217; in your rails console</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2010/06/29/bundler-default-gemfile-and-custom-gemfile-example-of-wirble/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ordered Hash by keys</title>
		<link>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/</link>
		<comments>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 09:15:50 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[activesupport]]></category>
		<category><![CDATA[active_support]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=111</guid>
		<description><![CDATA[Recently, I have to implement my own Hash sorting method. Not really with Hash but with ActiveSupport::OrderedHash I don&#8217;t like the way ActiveSupport::OrderedHash behaves with sorting functionality. It just behaves like a simple Hash I would like that sort return an ActiveSupport::OrderedHash not an Arrray!!! Why is it called OrderedHash in this case ? I &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Recently, I have to implement my own Hash sorting method. Not really with Hash but with <strong><em>ActiveSupport::OrderedHash</em></strong></p>
<ul>
<ol>I don&#8217;t like the way <strong><em>ActiveSupport::OrderedHash</em></strong> behaves with sorting functionality.</ol>
<ol>  It just behaves like a simple <strong><em>Hash</em></strong></ol>
<ol>I would like that sort return an  <strong><em>ActiveSupport::OrderedHash</em></strong> not an <strong><em>Arrray</em></strong>!!! Why is it called OrderedHash in this case ?</ol>
<ol>I would like to sort with keys only, not with an array [key, value]</ol>
</ul>
<p>Ok no matter, I implemented my own new <strong><em>SimpleOrderedHash</em></strong> :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/ordered_hash'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> SimpleOrderedHash <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::OrderedHash</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Lets reimplement sort method</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sort<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    h = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">dup</span>
    h.<span style="color:#9900CC;">sort</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    h
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Lets implement sort! method</span>
  <span style="color:#008000; font-style:italic;"># It uses @keys variable to do the sorting</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sort!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@keys</span>.<span style="color:#9900CC;">sort</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    sync_keys!
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># A fancy inspect   </span>
  <span style="color:#9966CC; font-weight:bold;">def</span> inspect
    <span style="color:#996600;">&quot;#&lt;#{self.class.name} #{Hash.instance_method(:inspect).bind(self).call}&gt;&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can now use it like that :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">h = SimpleOrderedHash.<span style="color:#9900CC;">new</span>
h.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">'second'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#996600;">'first'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">5</span>, <span style="color:#996600;">'third'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2.5</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;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;SimpleOrderedHash {&quot;third&quot;=&gt;2.5, &quot;second&quot;=&gt;1, &quot;first&quot;=&gt;5}&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># sort return a SimpleOrderedHash</span>
result = h.<span style="color:#9900CC;">sort</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  h<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;SimpleOrderedHash {&quot;third&quot;=&gt;2.5, &quot;second&quot;=&gt;1, &quot;first&quot;=&gt;5}&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># and order as I wanted</span>
result.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> v <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006666;">1</span>
<span style="color:#006666;">2.5</span>
<span style="color:#006666;">5</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;second&quot;</span>, <span style="color:#996600;">&quot;third&quot;</span>, <span style="color:#996600;">&quot;first&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># sort! change internally the order of a SimpleOrderedHash</span>
h.<span style="color:#9900CC;">sort</span>! <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  h<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
h.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> v <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006666;">5</span>
<span style="color:#006666;">2.5</span>
<span style="color:#006666;">1</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;first&quot;</span>, <span style="color:#996600;">&quot;third&quot;</span>, <span style="color:#996600;">&quot;second&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/10/02/ordered-hash-by-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inspectable accessors</title>
		<link>http://my.rails-royce.org/2009/09/23/inspectable-accessors/</link>
		<comments>http://my.rails-royce.org/2009/09/23/inspectable-accessors/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 15:49:46 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[accessor]]></category>
		<category><![CDATA[inspect]]></category>
		<category><![CDATA[méta-programmation]]></category>
		<category><![CDATA[meta-programming]]></category>
		<category><![CDATA[metaprograming]]></category>
		<category><![CDATA[métaprogrammation]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=65</guid>
		<description><![CDATA[Hey, it&#8217;s been a while I posted here. So what&#8217;s the purpose ? I commonly use script/console to debug my rails applications. Today I wanted to see the values of my custom attributes model without typing model.custom_attribute. So I took a look at ActiveRecord::Base#inspect method. and the default inspect method shows me database fields. Well, &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2009/09/23/inspectable-accessors/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Hey, it&#8217;s been a while I posted here.</p>
<p>So what&#8217;s the purpose ? I commonly use script/console  to debug my rails applications. Today I wanted to see the values of my custom attributes  model without typing <em><strong>model.custom_attribute</strong></em>. So I took a look at <em><strong>ActiveRecord::Base#inspect method.</strong></em> and the default inspect method shows me database fields. Well, let&#8217;s do some work to inspect custom attributes.<br />
Say you have a <em>users</em> table with some fields : login,name,encrypted_password.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:heroic_action</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">superman = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
superman.<span style="color:#9900CC;">heroic_action</span> = <span style="color:#996600;">&quot;Save the widow and the orphan&quot;</span>
pp superman.<span style="color:#9900CC;">inspect</span>
<span style="color:#996600;">&quot;#&lt;User login: &quot;</span>Superman<span style="color:#996600;">&quot;,  name: &quot;</span>Clark Kent<span style="color:#996600;">&quot;,
          encrypted_password: &quot;</span>da39a3ee5e6b4b0d3255bfef95601890afd80709<span style="color:#996600;">&quot;&gt;&quot;</span></pre></div></div>

<p>Well I also want to know what heroic_action is superman doing. So i have to overwrite inspect method but I would like a DRY solution. That&#8217;s why I wrote this code :</p>
<p>EDIT: Just a small change to have inheritance work.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support/core_ext/class'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> InspectableAttributes
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">class_inheritable_array</span> <span style="color:#ff3333; font-weight:bold;">:inspectable_attributes</span> , <span style="color:#ff3333; font-weight:bold;">:instance_writer</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">inspectable_attributes</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    base.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">def</span> inspect
        inspected_values = respond_to?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:attributes_before_type_cast</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? attributes_before_type_cast.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{k.to_s}:  #{v.inspect}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> : <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        inspected_values.<span style="color:#9900CC;">concat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">inspectable_attributes</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>attr<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;#{attr}: #{send(attr).inspect}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#996600;">&quot;#&lt;#{self.class.name} #{inspected_values.join(', ')}&gt;&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> inspectable_attr_accessor<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
      attr_accessor<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inspectable_attributes</span> <span style="color:#006600; font-weight:bold;">|</span>= args.<span style="color:#9900CC;">map</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>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> InspectableAttributes
  inspectable_attr_accessor <span style="color:#ff3333; font-weight:bold;">:heroic_action</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That&#8217;s what I was looking for !!!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">superman = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inspect</span>
superman.<span style="color:#9900CC;">heroic_action</span> = <span style="color:#996600;">&quot;Save the widow and the orphan&quot;</span>
pp superman.<span style="color:#9900CC;">inspect</span>
<span style="color:#996600;">&quot;#&lt;User login: &quot;</span>Superman<span style="color:#996600;">&quot;,  name: &quot;</span>Clark Kent<span style="color:#996600;">&quot;,
             encrypted_password: &quot;</span>da39a3ee5e6b4b0d3255bfef95601890afd80709<span style="color:#996600;">&quot;,
             heroic_action: &quot;</span>Save the widow <span style="color:#9966CC; font-weight:bold;">and</span> the orphan<span style="color:#996600;">&quot;&gt;&quot;</span></pre></div></div>

<p>Note that this code can be used in any ruby code requiring &#8216;active_support/core_ext/class&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/09/23/inspectable-accessors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class : to_s or not to_s ? That is the name !</title>
		<link>http://my.rails-royce.org/2009/07/10/class-to_s-or-not-to_s-that-is-the-name/</link>
		<comments>http://my.rails-royce.org/2009/07/10/class-to_s-or-not-to_s-that-is-the-name/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 21:22:45 +0000</pubDate>
		<dc:creator>Hallelujah</dc:creator>
				<category><![CDATA[best practices]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[programmation]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://my.rails-royce.org/?p=6</guid>
		<description><![CDATA[Many rubyist use the method Class#to_s instead of Class#name My purpose (shared by some programmers) is that to_s is just a String representation of the Class not its name!! Imagine you want to store an ordered list of inherited subclasses as a String so you can retrieve the inheritance order of each subclass. class Parent &#8230; </p><p><a class="more-link block-button" href="http://my.rails-royce.org/2009/07/10/class-to_s-or-not-to_s-that-is-the-name/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Many rubyist use the method <strong>Class#to_s</strong> instead of <strong>Class#name<br />
</strong><br />
My purpose (shared by some programmers) is that <strong>to_s</strong> is just a String representation of the Class not its name!!</p>
<p>Imagine you want to store an ordered list of inherited subclasses as a String so you can retrieve the inheritance order of each subclass.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Parent
  @@subclasses = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inherited</span><span style="color:#006600; font-weight:bold;">&#40;</span>subclass<span style="color:#006600; font-weight:bold;">&#41;</span>
    @@subclasses <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;#{subclass}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inheritance_order</span>
    @@subclasses.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{self}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Child1 <span style="color:#006600; font-weight:bold;">&lt;</span> Parent
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Child2 <span style="color:#006600; font-weight:bold;">&lt;</span> Parent
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># etc ...</span></pre></div></div>

<p>And now you want to know, the inheritance order of Child2</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Child2.<span style="color:#9900CC;">inheritance_order</span>
<span style="color:#008000; font-style:italic;">#=&gt; 1</span></pre></div></div>

<p>As long as you don&#8217;t override Child2.to_s method, this works.</p>
<p>But if you decide to override it &#8230;.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> Child2.<span style="color:#9900CC;">to_s</span>
  <span style="color:#996600;">&quot;The second child of Parent&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Child2.<span style="color:#9900CC;">inheritance_order</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span></pre></div></div>

<p>Do you understand what I mean ? My purpose is that <strong>Class#to_s</strong> is a String representation, so you can do what you want with this method (as long as it returns a String instance)</p>
<p>If you want to write something more reliable, just change <strong>Class#to_s</strong> by <strong>Class#name</strong> :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Parent
  @@subclasses = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inherited</span><span style="color:#006600; font-weight:bold;">&#40;</span>subclass<span style="color:#006600; font-weight:bold;">&#41;</span>
    @@subclasses <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> subclass.<span style="color:#9900CC;">name</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">inheritance_order</span>
    @@subclasses.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">name</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>What is your opinion ?</p>
]]></content:encoded>
			<wfw:commentRss>http://my.rails-royce.org/2009/07/10/class-to_s-or-not-to_s-that-is-the-name/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

