# Weird hash key: what am I asking Ruby to do?

**URL:** <https://www.ruby-forum.com/t/weird-hash-key-what-am-i-asking-ruby-to-do/186119>\
**Category:** Ruby\
**Created:** [March 31, 2010, 8:32pm UTC](https://www.ruby-forum.com/t/weird-hash-key-what-am-i-asking-ruby-to-do/186119 "2010-03-31T20:32:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [March 31, 2010, 8:32pm UTC](https://www.ruby-forum.com/t/weird-hash-key-what-am-i-asking-ruby-to-do/186119/1 "2010-03-31T20:32:02Z")

</div>

I decided to type the following in irb:  
irb(main):010:0\> a = {}  
=\> {}  
irb(main):015:0\> a[lambda { x } =\> 5] = 5  
=\> 5  
irb(main):016:0\> a  
=\> {{#Proc:0x028cb304@:15(irb)=\>5}=\>5}

irb(main):017:0\> a = {}  
=\> {}  
irb(main):018:0\> a[lambda { x }] = 5  
=\> 5  
irb(main):019:0\> a  
=\> {#Proc:0x028c2a4c@:18(irb)=\>5}

So… In the first example, the hash key is ‘lambda { x } =\> 5’ .  
What on earth does that even mean?

In the second one, the hash key is a lambda. Does that even serve any  
purpose beyond just being the key for whatever value I choose to  
associate with it?

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [March 31, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/weird-hash-key-what-am-i-asking-ruby-to-do/186119/2 "2010-03-31T21:55:15Z")

</div>

On Wed, Mar 31, 2010 at 12:32 PM, Aldric G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> irb(main):018:0\> a[lambda { x }] = 5  
> =\> 5  
> irb(main):019:0\> a  
> =\> {#Proc:0x028c2a4c@:18(irb)=\>5}
> 
> So… In the first example, the hash key is ‘lambda { x } =\> 5’ .  
> What on earth does that even mean?

It means that the key is a hash table which contains a key of the  
lambda,  
that maps to the value of 5. Notice all three sets of code are the same,  
the  
keys just get progressively more complicated looking. But if you didn’t  
know  
what they were, and just considered them to be an object (which they  
are),  
then they are no longer so complicated, just complicated looking.

key = ‘simple’  
hash = Hash.new  
hash[key] = 5  
hash # =\> {“simple”=\>5}  
key[key] # =\> “simple”

##########

key = lambda {x}  
hash = Hash.new  
hash[key] = 5  
hash # =\> {#Proc:0x000332e8@-:9=\>5}  
hash[key] # =\> 5

key = hash # the hash from the previous code block is now the key  
hash = Hash.new  
hash[key] = 5  
hash # =\> {{#Proc:0x000332e8@-:9=\>5}=\>5}  
hash[key] # =\> 5

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [April 1, 2010, 2:08pm UTC](https://www.ruby-forum.com/t/weird-hash-key-what-am-i-asking-ruby-to-do/186119/3 "2010-04-01T14:08:46Z")

</div>

Josh C. wrote:

> On Wed, Mar 31, 2010 at 12:32 PM, Aldric G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > So… In the first example, the hash key is ‘lambda { x } =\> 5’ .  
> > What on earth does that even mean?
> 
> It means that the key is a hash table which contains a key of the  
> lambda,  
> that maps to the value of 5. Notice all three sets of code are the same,  
> the  
> keys just get progressively more complicated looking. But if you didn’t  
> know  
> what they were, and just considered them to be an object (which they  
> are),  
> then they are no longer so complicated, just complicated looking.

Hah! That makes perfect sense. I know what I’m doing next time I see a  
code obfuscation contest.

Thanks!

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [April 1, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/weird-hash-key-what-am-i-asking-ruby-to-do/186119/4 "2010-04-01T16:10:55Z")

</div>

Aldric G. wrote:

> So… In the first example, the hash key is ‘lambda { x } =\> 5’ .  
> What on earth does that even mean?

You can omit the hash braces in the last arg of a method call, i.e.

fn(a=\>b, c=\>d) is short for fn({a=\>b, c=\>d})

foo[bar] is also a method call. It’s short for foo.

irb(main):001:0\> h = {}  
=\> {}  
irb(main):002:0\> h[1=\>2] = 3  
=\> 3  
irb(main):003:0\> h  
=\> {{1=\>2}=\>3}
