<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Hash key rewrite</title>
	<atom:link href="http://www.dweebd.com/ruby/hash-key-rewrite/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dweebd.com/ruby/hash-key-rewrite/</link>
	<description>Web Freshness</description>
	<lastBuildDate>Tue, 03 Jan 2012 10:08:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Rodent of Unusual Size</title>
		<link>http://www.dweebd.com/ruby/hash-key-rewrite/comment-page-1/#comment-626</link>
		<dc:creator>Rodent of Unusual Size</dc:creator>
		<pubDate>Wed, 21 Dec 2011 15:41:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/?p=56#comment-626</guid>
		<description>Unfortunately, I think this technique has a basic drawback, which leads to others.  It returns a *new* hash rather than updating the existing one.  Which means the return value lacks any special characteristics of the original hash (such as default value, singleton methods, etc.).

Obviously the modify-origin-hash would be addressed by using a #rewrite! method, but the other aspect remains.

To rewrite the hash in place without losing any specifics, try replacing the #inject block with this:

&lt;code lang=&quot;ruby&quot;&gt;
mapping.inject(self) do &#124;memo,(oldkey,newkey)&#124;
  memo[newkey] = memo[oldkey]
  memo.delete(oldkey) 
  memo
end
self
&lt;/code&gt;

This will retain instance-local attributes, modify the hash in place, and only iterates over the changed pairs rather than the entire hash (which might be humongous).</description>
		<content:encoded><![CDATA[<p>Unfortunately, I think this technique has a basic drawback, which leads to others.  It returns a *new* hash rather than updating the existing one.  Which means the return value lacks any special characteristics of the original hash (such as default value, singleton methods, etc.).</p>
<p>Obviously the modify-origin-hash would be addressed by using a #rewrite! method, but the other aspect remains.</p>
<p>To rewrite the hash in place without losing any specifics, try replacing the #inject block with this:</p>
<p><pre class="ruby">mapping.<span class="me1">inject</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>memo,<span class="br0">&#40;</span>oldkey,newkey<span class="br0">&#41;</span><span class="sy0">|</span>
  memo<span class="br0">&#91;</span>newkey<span class="br0">&#93;</span> = memo<span class="br0">&#91;</span>oldkey<span class="br0">&#93;</span>
  memo.<span class="me1">delete</span><span class="br0">&#40;</span>oldkey<span class="br0">&#41;</span> 
  memo
<span class="kw1">end</span>
<span class="kw2">self</span></pre></p>
<p>This will retain instance-local attributes, modify the hash in place, and only iterates over the changed pairs rather than the entire hash (which might be humongous).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Burcham</title>
		<link>http://www.dweebd.com/ruby/hash-key-rewrite/comment-page-1/#comment-209</link>
		<dc:creator>Bill Burcham</dc:creator>
		<pubDate>Fri, 17 Oct 2008 15:03:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/?p=56#comment-209</guid>
		<description>Oh yeah. For folks who want to find more of these in the wild, here&#039;s a TextMate regexp that will find all injects that use argument decomposition:

inject.*[&#124;][^&#124;(\n]*[(][^)&#124;\n]+[)][^&#124;\n]*[&#124;]

That makes sense when injecting on anything whose each method yields an array and Hash is a real common one (yielding [key,value]).</description>
		<content:encoded><![CDATA[<p>Oh yeah. For folks who want to find more of these in the wild, here&#8217;s a TextMate regexp that will find all injects that use argument decomposition:</p>
<p>inject.*[|][^|(\n]*[(][^)|\n]+[)][^|\n]*[|]</p>
<p>That makes sense when injecting on anything whose each method yields an array and Hash is a real common one (yielding [key,value]).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: duncanbeevers</title>
		<link>http://www.dweebd.com/ruby/hash-key-rewrite/comment-page-1/#comment-207</link>
		<dc:creator>duncanbeevers</dc:creator>
		<pubDate>Wed, 15 Oct 2008 16:02:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/?p=56#comment-207</guid>
		<description>A block passed to inject takes two arguments.  The first is the accumulator, and the second is the element of the enumerable being injected (or &lt;a href=&quot;http://en.wikipedia.org/wiki/Foldl&quot; rel=&quot;nofollow&quot;&gt;folded&lt;/a&gt;.)

The decomposition in this case occurs in the second argument of the inject block, which instead of assigning the enumerable element to a single block-local variable, instead decomposes the Hash tuple (key, value) to two discrete locals.  Neat!</description>
		<content:encoded><![CDATA[<p>A block passed to inject takes two arguments.  The first is the accumulator, and the second is the element of the enumerable being injected (or <a href="http://en.wikipedia.org/wiki/Foldl" rel="nofollow">folded</a>.)</p>
<p>The decomposition in this case occurs in the second argument of the inject block, which instead of assigning the enumerable element to a single block-local variable, instead decomposes the Hash tuple (key, value) to two discrete locals.  Neat!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Burcham</title>
		<link>http://www.dweebd.com/ruby/hash-key-rewrite/comment-page-1/#comment-206</link>
		<dc:creator>Bill Burcham</dc:creator>
		<pubDate>Wed, 15 Oct 2008 14:26:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/?p=56#comment-206</guid>
		<description>That&#039;s a beautiful method. Which part is the &quot;argument decomposition&quot; part? I Googled for &quot;ruby argument decomposition&quot; about all I get is this very post!</description>
		<content:encoded><![CDATA[<p>That&#8217;s a beautiful method. Which part is the &#8220;argument decomposition&#8221; part? I Googled for &#8220;ruby argument decomposition&#8221; about all I get is this very post!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

