# String keys in hash

**URL:** <https://www.ruby-forum.com/t/string-keys-in-hash/83935>\
**Category:** Ruby\
**Created:** [March 19, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/string-keys-in-hash/83935 "2006-03-19T15:15:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![konlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@konlin](https://www.ruby-forum.com/u/konlin)\
**Post date:** [March 19, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/string-keys-in-hash/83935/1 "2006-03-19T15:15:40Z")

</div>

Hi all,

My rails app is going to count sth based on poll id in the url. The url  
looks like:  
[http://address.com?id=dwNKiItvcyrQ5sycnIhmJablDfXsc9tshaGIVyNIei7.e7&some\_other\_parameters](http://address.com?id=dwNKiItvcyrQ5sycnIhmJablDfXsc9tshaGIVyNIei7.e7&some_other_parameters)

In a controller, I’ve created a hash where the keys are the ids:

@codes = {  
‘dwNKiItvcyrQ5sycnIhmJablDfXsc9tshaGIVyNIei7.e7’ =\>  
{:partner\_id =\> 0,  
:partner\_name =\> ‘sth’,  
:page\_number =\> 1},  
…  
}

Then i do:

poll\_id = ‘"’ + params[‘id’] + ‘"’

The problem is, @codes[poll\_id] returns nil.

Otherwise, @codes[“dwNKiItvcyrQ5sycnIhmJablDfXsc9tshaGIVyNIei7.e7”]  
works ok. What am I doing wrong if the keys are the same and explicite  
string works while variable doesn’t?

Can you explain me that and write how to solve that problem?

Thanks in advance, greetz

–  
K.O.N.L.I.N. removed\_email\_address@domain.invalid  
Kinetic Organism Normally for Logical Infiltration and Nullification,

```
      "Day after day, love turns grey
       Like the skin of a dying man"

```

---

<div class="post-metadata">

**Author:** ![konlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@konlin](https://www.ruby-forum.com/u/konlin)\
**Post date:** [March 19, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/string-keys-in-hash/83935/2 "2006-03-19T15:26:12Z")

</div>

On 19-Mar-06, at 9:13 AM, Konlin wrote:

> poll\_id = ‘"’ + params[‘id’] + ‘"’
> 
> The problem is, @codes[poll\_id] returns nil.

You should be able to say

@codes[params[‘id’]]

to access the hash.

> Otherwise, @codes[“dwNKiItvcyrQ5sycnIhmJablDfXsc9tshaGIVyNIei7.e7”]  
> works ok. What am I doing wrong if the keys are the same and explicite  
> string works while variable doesn’t?
> 
> Can you explain me that and write how to solve that problem?
> 
> Thanks in advance, greetz

The quotes in @codes  
[“dwNKiItvcyrQ5sycnIhmJablDfXsc9tshaGIVyNIei7.e7”] are part of Ruby’s  
syntax not the string. For example in this irb session:

ratdog:~ mike$ irb  
irb(main):001:0\> puts “hello”  
hello  
=\> nil

The quotes are needed to indicate that the characters h e l l o are a  
string. The string which ruby uses doesn’t contain the quotes.

Does that help?

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![konlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@konlin](https://www.ruby-forum.com/u/konlin)\
**Post date:** [March 19, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/string-keys-in-hash/83935/3 "2006-03-19T16:13:54Z")

</div>

Why are you wrapping params[‘id’] in double quotes? If params[‘id’] is  
already equal to your key in @codes, you can just use:

@codes[params[‘id’]]

-Scott
