# Ruby Hash Querying

**URL:** <https://www.ruby-forum.com/t/ruby-hash-querying/106753>\
**Category:** Ruby\
**Created:** [June 13, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/ruby-hash-querying/106753 "2007-06-13T20:36:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@kwest](https://www.ruby-forum.com/u/kwest)\
**Post date:** [June 13, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/ruby-hash-querying/106753/1 "2007-06-13T20:36:08Z")

</div>

Hello,  
I am new to Ruby and have run into a small snag. I have a hash like  
so:

@documentnames = {  
:finance\_whitepaper =\> ‘whitepaper one’,  
:ecommerce\_whitepaper =\> ‘whitepaper two’,  
:integratedsvs\_datasheet =\> ‘whitepaper three’  
}

So I am trying to then go like so:

@documentnames[k]

where k is a variable in a loop with the key from the has minus the  
“:” in front

But if I try to add the “:” in front

\<%key = ‘:’ + k%\>

And then try to reference the element

\<%= @documentnames[key] %\>

I get a null value…

---

<div class="post-metadata">

**Author:** ![kwest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@kwest](https://www.ruby-forum.com/u/kwest)\
**Post date:** [June 13, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/ruby-hash-querying/106753/2 "2007-06-13T20:41:08Z")

</div>

OK I answered my own question…sorry. But here is the answer in case  
someone else asks the same silly question:

Use a string as a key (doh!)

@documentnames = {  
‘finance\_whitepaper’ =\> ‘whitepaper one’,  
‘ecommerce\_whitepaper’ =\> ‘whitepaper two’,  
‘integratedsvs\_datasheet’ =\> ‘whitepaper three’  
}

Problem solved!

---

<div class="post-metadata">

**Author:** ![kwest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@kwest](https://www.ruby-forum.com/u/kwest)\
**Post date:** [June 13, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/ruby-hash-querying/106753/3 "2007-06-13T20:45:10Z")

</div>

On 6/13/07, kwest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%key = ‘:’ + k%\>
> 
> And then try to reference the element
> 
> \<%= @documentnames[key] %\>
> 
> I get a null value…

That happens because the keys are symbols, and not strings starting  
with “:”. Try either using strings instead of symbols as hash keys  
(i.e., ‘finance\_whitepaper’ rather than :finance\_whitepaper - note que  
quotes) or using k.to\_sym inside the loop.

---

<div class="post-metadata">

**Author:** ![kwest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@kwest](https://www.ruby-forum.com/u/kwest)\
**Post date:** [June 13, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/ruby-hash-querying/106753/4 "2007-06-13T21:01:16Z")

</div>

Great thank you. That is too easy. I am liking this.

On Jun 13, 11:36 am, Daniel K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kwest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@kwest](https://www.ruby-forum.com/u/kwest)\
**Post date:** [June 13, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/ruby-hash-querying/106753/5 "2007-06-13T20:47:10Z")

</div>

kwest schrieb:

> \<%key = ‘:’ + k%\>
> 
> And then try to reference the element
> 
> \<%= @documentnames[key] %\>
> 
> I get a null value…

The “:whatever” is a Ruby-Symbol, and you’re trying to kinda “fake” it  
with a String. However, the answer is really easy, just use:  
@documentnames[k.to\_sym]
