<?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; activesupport</title>
	<atom:link href="http://my.rails-royce.org/tag/activesupport/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-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<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"><table><tr><td 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></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td 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></td></tr></table></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>
	</channel>
</rss>
