String keys in hash


#1

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

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"

#2

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
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.


#3

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