<?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"
	>
<channel>
	<title>Comments on: Ruby Hashes of Arbitrary Depth</title>
	<atom:link href="http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/</link>
	<description>Politics, Programming and Possibilities</description>
	<pubDate>Fri, 12 Mar 2010 07:10:40 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: BryanDonovan</title>
		<link>http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-587</link>
		<dc:creator>BryanDonovan</dc:creator>
		<pubDate>Wed, 27 Sep 2006 19:59:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-587</guid>
		<description>I've always used the HashMD class I found in the RubyTalk mailing list:

class HashMD   {"a" =&#62; {"b" =&#62; {"c" =&#62; "xxx"}}}

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/13408</description>
		<content:encoded><![CDATA[<p>I&#8217;ve always used the HashMD class I found in the RubyTalk mailing list:</p>
<p>class HashMD   {&#8221;a&#8221; =&gt; {&#8221;b&#8221; =&gt; {&#8221;c&#8221; =&gt; &#8220;xxx&#8221;}}}</p>
<p><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/13408" rel="nofollow">http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/13408</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Adams</title>
		<link>http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-585</link>
		<dc:creator>Josh Adams</dc:creator>
		<pubDate>Sun, 24 Sep 2006 14:13:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-585</guid>
		<description>Thanks a lot.  This is the LISPy kind of thing I've been hoping to grasp as a potential solution to my own problems  :)  Too much .NET hosed my brain...

Excellent writeup.</description>
		<content:encoded><![CDATA[<p>Thanks a lot.  This is the LISPy kind of thing I&#8217;ve been hoping to grasp as a potential solution to my own problems  <img src='http://blog.inquirylabs.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Too much .NET hosed my brain&#8230;</p>
<p>Excellent writeup.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Duane Johnson</title>
		<link>http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-584</link>
		<dc:creator>Duane Johnson</dc:creator>
		<pubDate>Sun, 24 Sep 2006 00:22:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-584</guid>
		<description>Sure, Josh, I can take a gander:

  hash = Hash.new(&#038;(p=lambda{&#124;h,k&#124; h[k] = Hash.new(&#038;p)}))

First you have to understand how a Hash allows you to set the default value of an empty key.  Normally, the default is simply "nil", meaning that if you access any old key, such as my_hash[:duane], without having first set :duane to something, then my_hash will return nil.

This default can be customized, like this:
  my_hash = Hash.new { &#124;h,k&#124; h[k] = 0 }

In this case, my_hash[:brother] will return zero (0).

We could have just as well done this with a Proc object (instead of a block, as above) by doing something like this:

  p = Proc.new { &#124;h,k&#124; h[k] = 0 }
  my_hash = Hash.new(&#038;p)

In this case, we're using the Ruby notation '&#038;' to denote that the proc we're passing in is to take the place of the optional block.

"lambda" is a synonym for "Proc.new".  Thus, Kent is creating a new Proc object assigned to the variable "p".  Because blocks (and thus, Proc objects, aka lambdas) are aware of the variable scope outside of themselves, Kent is able to pass the "p" he just barely assigned right back in to the Proc itself--essentially creating a recursive lambda function.

Thus, in English, this single line:

hash = Hash.new(&#038;(p=lambda{&#124;h,k&#124; h[k] = Hash.new(&#038;p)}))

says, "Create a hash whose default value is a hash whose default value is a hash whose default value is a hash whose ..." etc.</description>
		<content:encoded><![CDATA[<p>Sure, Josh, I can take a gander:</p>
<p>  hash = Hash.new(&#038;(p=lambda{|h,k| h[k] = Hash.new(&#038;p)}))</p>
<p>First you have to understand how a Hash allows you to set the default value of an empty key.  Normally, the default is simply &#8220;nil&#8221;, meaning that if you access any old key, such as my_hash[:duane], without having first set :duane to something, then my_hash will return nil.</p>
<p>This default can be customized, like this:<br />
  my_hash = Hash.new { |h,k| h[k] = 0 }</p>
<p>In this case, my_hash[:brother] will return zero (0).</p>
<p>We could have just as well done this with a Proc object (instead of a block, as above) by doing something like this:</p>
<p>  p = Proc.new { |h,k| h[k] = 0 }<br />
  my_hash = Hash.new(&#038;p)</p>
<p>In this case, we&#8217;re using the Ruby notation &#8216;&#038;&#8217; to denote that the proc we&#8217;re passing in is to take the place of the optional block.</p>
<p>&#8220;lambda&#8221; is a synonym for &#8220;Proc.new&#8221;.  Thus, Kent is creating a new Proc object assigned to the variable &#8220;p&#8221;.  Because blocks (and thus, Proc objects, aka lambdas) are aware of the variable scope outside of themselves, Kent is able to pass the &#8220;p&#8221; he just barely assigned right back in to the Proc itself&#8211;essentially creating a recursive lambda function.</p>
<p>Thus, in English, this single line:</p>
<p>hash = Hash.new(&#038;(p=lambda{|h,k| h[k] = Hash.new(&#038;p)}))</p>
<p>says, &#8220;Create a hash whose default value is a hash whose default value is a hash whose default value is a hash whose &#8230;&#8221; etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Adams</title>
		<link>http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-583</link>
		<dc:creator>Josh Adams</dc:creator>
		<pubDate>Fri, 22 Sep 2006 19:43:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-583</guid>
		<description>Any chance I could get an explanation of what Kent's solution does?

--mortal</description>
		<content:encoded><![CDATA[<p>Any chance I could get an explanation of what Kent&#8217;s solution does?</p>
<p>&#8211;mortal</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Duane Johnson</title>
		<link>http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-581</link>
		<dc:creator>Duane Johnson</dc:creator>
		<pubDate>Thu, 21 Sep 2006 15:17:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-581</guid>
		<description>Thanks, Kent!

Nice solution--I wish I'd thought of it!</description>
		<content:encoded><![CDATA[<p>Thanks, Kent!</p>
<p>Nice solution&#8211;I wish I&#8217;d thought of it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kent</title>
		<link>http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-580</link>
		<dc:creator>Kent</dc:creator>
		<pubDate>Wed, 20 Sep 2006 23:53:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/#comment-580</guid>
		<description>Or

    hash = Hash.new(&#38;(p=lambda{&#124;h,k&#124; h[k] = Hash.new(&#38;p)}))</description>
		<content:encoded><![CDATA[<p>Or</p>
<p>    hash = Hash.new(&amp;(p=lambda{|h,k| h[k] = Hash.new(&amp;p)}))</p>
]]></content:encoded>
	</item>
</channel>
</rss>
