<?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>Cloud Connected &#187; ruby</title>
	<atom:link href="http://www.cloudconnected.fr/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cloudconnected.fr</link>
	<description>Thoughts of a french web developer</description>
	<lastBuildDate>Wed, 01 Feb 2012 08:53:57 +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>Afficher un hash dans l&#8217;ordre en Ruby</title>
		<link>http://www.cloudconnected.fr/2007/08/10/afficher-un-hash-dans-lordre-en-ruby/</link>
		<comments>http://www.cloudconnected.fr/2007/08/10/afficher-un-hash-dans-lordre-en-ruby/#comments</comments>
		<pubDate>Fri, 10 Aug 2007 15:23:12 +0000</pubDate>
		<dc:creator>Rémi</dc:creator>
				<category><![CDATA[Non classé]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.the-asw.com/?p=290</guid>
		<description><![CDATA[Ruby, contrairement à PHP (mais comme beaucoup d&#8217;autres langages), fait la différence entre un tableau (avec des index numériques) et un hash (avec n&#8217;importe quel type d&#8217;index, mais sans notion d&#8217;ordre). Etant habitué à PHP, je me suis heurté au problème suivant : comment afficher un hash dans l&#8217;ordre ? Considerons le code suivant, où [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby, contrairement à PHP (mais comme beaucoup d&#8217;autres langages), fait la différence entre un tableau (avec des index numériques) et un hash (avec n&#8217;importe quel type d&#8217;index, mais sans notion d&#8217;ordre). Etant habitué à PHP, je me suis heurté au problème suivant : comment afficher un hash dans l&#8217;ordre ?</p>
<p>Considerons le code suivant, où le hash contient les éléments indexés par leur id numérique (je n&#8217;utilise pas un tableau volontairement, car j&#8217;ai besoin de ces id).</p>
<pre>
hash = {
	1 => "toto",
	2 => "tata",
	3 => "titi",
	4 => "tutu",
	5 => "tete",
	6 => "tyty"
}

hash.each_pair do |key, value|
	puts "#{key} = #{value}"
end
</pre>
<p>Il va afficher :</p>
<pre>
5 = tete
6 = tyty
1 = toto
2 = tata
3 = titi
4 = tutu
</pre>
<p>Ce résultat correspond à la documentation de la classe Hash :</p>
<blockquote><p>
A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. <strong>The order in which you traverse a hash by either key or value may seem arbitrary, and will generally not be in the insertion order</strong>.
</p></blockquote>
<p>Bref, c&#8217;est tout à fait normal, mais pas du tout ce que je recherche. Pour l&#8217;afficher dans l&#8217;ordre, il suffit en fait de trier et parcourir les clefs du hash :</p>
<pre>
hash.keys.sort!.each do |key|
	puts "#{key} = " + hash[key]
end
</pre>
<p>Et on obtient le bon ordre :</p>
<pre>
1 = toto
2 = tata
3 = titi
4 = tutu
5 = tete
6 = tyty
</pre>
<p>Youpi ! Bon, on peut toujours critiquer PHP, mais son concept de tableau/hash mélangé et ordonné, c&#8217;est quand même bien pratique :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cloudconnected.fr/2007/08/10/afficher-un-hash-dans-lordre-en-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

