<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://cygal.info/blog/index.php/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Dissidents - hachage</title>
  <link>http://cygal.info/blog/index.php/</link>
  <description></description>
  <language>fr</language>
  <pubDate>Sat, 20 Sep 2008 15:04:06 +0200</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Du sucre en ruby</title>
    <link>http://cygal.info/blog/index.php/post/Du-sucre-en-ruby</link>
    <guid isPermaLink="false">urn:md5:dfe4b1861f1f4ffdfacc4e4ef8f797af</guid>
    <pubDate>Sat, 21 Jul 2007 14:57:00 +0200</pubDate>
    <dc:creator>ArtMoonik</dc:creator>
        <category>Script</category>
        <category>hachage</category><category>ruby</category>    
    <description>&lt;p&gt;Les Hash faciles et pratiques.&lt;/p&gt;    &lt;p&gt;Quand on est amené à faire du &lt;a href=&quot;http://fr.wikipedia.org/wiki/Fonction_de_hachage&quot; hreflang=&quot;fr&quot;&gt;hachage&lt;/a&gt; en ruby, on fait des trucs genre:&lt;/p&gt;
&lt;pre&gt;
tableau[cle] &amp;lt;&amp;lt; donnee
&lt;/pre&gt;


&lt;p&gt;Bien sur si c'est la première fois qu'on croise cette clé alors on aura une erreur genre:&lt;/p&gt;
&lt;pre&gt;
NoMethodError: undefined method `&amp;lt;&amp;lt;' for nil:NilClass
&lt;/pre&gt;

&lt;p&gt;Donc à la place on fait:&lt;/p&gt;
&lt;pre&gt;
tableau[cle] = [donnee]
&lt;/pre&gt;

&lt;p&gt;Au final ca donne:&lt;/p&gt;
&lt;pre&gt;
if tableau[cle].nil? then
   tableau[cle] = [donnee]
else
    tableau[cle] &amp;lt;&amp;lt; donnee
end
&lt;/pre&gt;


&lt;p&gt;Bien sûr c'est horriblement laid et ca prend de la place et ca fait perdre du temps, bref ca pue. Donc, alternative, donnons à notre Hash 'tableau' une valeur par défaut autre qu'un nil, soit un Array ici:&lt;/p&gt;
&lt;pre&gt;
tableau = Hash.new { |h,k| h[k] = [] }
tableau[cle] &amp;lt;&amp;lt; donnee
&lt;/pre&gt;


&lt;p&gt;Et puis le hachage, c'est bien :)
Bonsoir les loutres.&lt;/p&gt;</description>
    
    
    
          <comments>http://cygal.info/blog/index.php/post/Du-sucre-en-ruby#comment-form</comments>
      <wfw:comment>http://cygal.info/blog/index.php/post/Du-sucre-en-ruby#comment-form</wfw:comment>
      <wfw:commentRss>http://cygal.info/blog/index.php/feed/rss2/comments/30</wfw:commentRss>
      </item>
    
</channel>
</rss>