<?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>tomrochette.com &#187; ruby</title>
	<atom:link href="http://www.tomrochette.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tomrochette.com</link>
	<description>What&#039;s up with my life</description>
	<lastBuildDate>Sat, 17 Dec 2011 23:30:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Marshal un hash avec proc</title>
		<link>http://www.tomrochette.com/marshal-un-hash-avec-proc/</link>
		<comments>http://www.tomrochette.com/marshal-un-hash-avec-proc/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 06:15:15 +0000</pubDate>
		<dc:creator>tomzx</dc:creator>
				<category><![CDATA[Programmation]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.tomrochette.com/?p=503</guid>
		<description><![CDATA[Il arrive souvent que l&#8217;on veut écrire un hash imbriqué, tel que objet[:unevaleur][:unesecondevaleur] accédant à la valeur :unesecondevaleur du hash contenu dans objet[:unevaleur]. Voici une implémentation simpliste de la chose. 1 X = Hash.new &#123;&#124;h, k&#124; h&#91;k&#93; = Hash.new&#40;0&#41;&#125; Avec ce code, à chaque fois qu&#8217;on crée un hash non-existant dans X, on ajoute un [...]]]></description>
			<content:encoded><![CDATA[<p>Il arrive souvent que l&#8217;on veut écrire un hash imbriqué, tel que <strong>objet[:unevaleur][:unesecondevaleur]</strong> accédant à la valeur <strong>:unesecondevaleur</strong> du hash contenu dans <strong>objet[:unevaleur]</strong>. Voici une implémentation simpliste de la chose.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">X = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>h, k<span style="color:#006600; font-weight:bold;">|</span> h<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>Avec ce code, à chaque fois qu&#8217;on crée un hash non-existant dans X, on ajoute un nouveau hash avec 0 comme valeur par défaut. Si maintenant on veut marshaller X, on obtient l&#8217;erreur suivante:</p>
<blockquote><p>Marshal.dump(X)<br />
TypeError: can&#8217;t dump hash with default proc</p></blockquote>
<p>On obtient cette erreur puisque, lorsqu&#8217;on crée X, on a passé un bloc à new ce qui fait que lorsqu&#8217;on veut le marshaller, Ruby n&#8217;est pas mesure de marshaller le proc associé.<br />
Afin d&#8217;être en mesure de marshaller X, il suffit simplement d&#8217;éliminer le proc en effectuant une copie vers un nouveau hash sans proc.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Y = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span>
X.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>h, k<span style="color:#006600; font-weight:bold;">|</span> Y<span style="color:#006600; font-weight:bold;">&#91;</span>h<span style="color:#006600; font-weight:bold;">&#93;</span> = X<span style="color:#006600; font-weight:bold;">&#91;</span>h<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC00FF; font-weight:bold;">Marshal</span>.<span style="color:#9900CC;">dump</span><span style="color:#006600; font-weight:bold;">&#40;</span>Y<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tomrochette.com/marshal-un-hash-avec-proc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

