<?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: An interesting Ruby hash semantics gotcha</title>
	<atom:link href="http://blog.playlouder.com/2008/05/07/an-interesting-ruby-hash-semantics-gotcha/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.playlouder.com/2008/05/07/an-interesting-ruby-hash-semantics-gotcha/</link>
	<description></description>
	<pubDate>Tue, 18 Nov 2008 09:47:35 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: matthew</title>
		<link>http://blog.playlouder.com/2008/05/07/an-interesting-ruby-hash-semantics-gotcha/#comment-3447</link>
		<dc:creator>matthew</dc:creator>
		<pubDate>Sat, 10 May 2008 12:15:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.playlouder.com/?p=89#comment-3447</guid>
		<description>[quote comment=""]
I thought {} was synonymous with Hash.new

Therefore

{{} =&#62; true}[{}]

generates 2 different hashes with different object_ids and so will return nil
[/quote]

It is Hash.new yes - {{} =&gt; true} is a Hash which has one key/value pair - the key is {} (an empty Hash) and the value true.

[] is then the lookup method for that Hash - so we're looking up the value corresponding to the key {}. We expect to get true, but it doesn't find the value, due to {}.hash not being fixed for different instances of an empty hash.

h = Hash.new
empty_hash_as_key = {}
another_empty_hash = {}
h[empty_hash_as_key] = true

h[another_empty_hash]
=&gt; nil

-Matt</description>
		<content:encoded><![CDATA[<blockquote cite="http://blog.playlouder.com/2008/05/07/an-interesting-ruby-hash-semantics-gotcha/#comment-">
<p>I thought {} was synonymous with Hash.new</p>
<p>Therefore</p>
<p>{{} =&gt; true}[{}]</p>
<p>generates 2 different hashes with different object_ids and so will return nil
</p>
</blockquote>
<p>It is Hash.new yes - {{} => true} is a Hash which has one key/value pair - the key is {} (an empty Hash) and the value true.</p>
<p>[] is then the lookup method for that Hash - so we&#8217;re looking up the value corresponding to the key {}. We expect to get true, but it doesn&#8217;t find the value, due to {}.hash not being fixed for different instances of an empty hash.</p>
<p>h = Hash.new<br />
empty_hash_as_key = {}<br />
another_empty_hash = {}<br />
h[empty_hash_as_key] = true</p>
<p>h[another_empty_hash]<br />
=> nil</p>
<p>-Matt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Green</title>
		<link>http://blog.playlouder.com/2008/05/07/an-interesting-ruby-hash-semantics-gotcha/#comment-3379</link>
		<dc:creator>Anthony Green</dc:creator>
		<pubDate>Thu, 08 May 2008 07:48:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.playlouder.com/?p=89#comment-3379</guid>
		<description>I love Ruby code examples. I like the whooshing sound they make as they fly by over my head. You lost me at Object#hash. 

From my naive non-software enginneering perspective the first three code examples behave exactly as I would expect:

I thought {} was synonymous with Hash.new

Therefore

{{} =&#62; true}[{}]

generates 2 different hashes with different object_ids and so will return nil

and its the same with

{{}=&#62;true, {}=&#62;true}

the two hashes have two different object_ids so are different keys

where as

 {} == {}

compares just the contents of both hashes and returns true.

There is no === operator for Hashes which I would expect to assert that both objects have the same object_id and the same contents.</description>
		<content:encoded><![CDATA[<p>I love Ruby code examples. I like the whooshing sound they make as they fly by over my head. You lost me at Object#hash. </p>
<p>From my naive non-software enginneering perspective the first three code examples behave exactly as I would expect:</p>
<p>I thought {} was synonymous with Hash.new</p>
<p>Therefore</p>
<p>{{} =&gt; true}[{}]</p>
<p>generates 2 different hashes with different object_ids and so will return nil</p>
<p>and its the same with</p>
<p>{{}=&gt;true, {}=&gt;true}</p>
<p>the two hashes have two different object_ids so are different keys</p>
<p>where as</p>
<p> {} == {}</p>
<p>compares just the contents of both hashes and returns true.</p>
<p>There is no === operator for Hashes which I would expect to assert that both objects have the same object_id and the same contents.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
