Forum: Ruby on Rails String keys in hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Konrad I. (Guest)
on 2006-03-19 16:09
(Received via mailing list)
Hi all,

My app is going to count sth based on poll id in the url. The url
looks like:
http://address.com?id=dwNKiItvcyrQ5sycnIhmJablDfXs...

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"
unknown (Guest)
on 2006-03-19 16:59
(Received via mailing list)
Hi --

On Sun, 19 Mar 2006, Konlin wrote:

>         {:partner_id => 0,
>
> 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?

They're not the same; one of them has double quotation marks and the
other doesn't.  The string:

    abc

and the string

   "abc"

do not make for the same hash key.


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
KN (Guest)
on 2006-03-19 17:09
(Received via mailing list)
On Sun, Mar 19, 2006 at 03:08:28PM +0100, Konlin wrote:

> poll_id = '"' + params['id'] + '"'
What are those '"' + ... + '"' for? You are already passing a
string when you do params['id'] so this would be enough:

  poll_id = params['id']
  @codes[poll_id]

or just: @codes[params['id']]
Konrad I. (Guest)
on 2006-03-19 17:19
(Received via mailing list)
Hi KN,

W Twoim li¶cie datowanym 19 marca 2006 (16:08:46) mo¿na
przeczytaæ:
K> What are those '"' + ... + '"' for? You are already passing a
K> string when you do params['id'] so this would be enough:
K>
K>   poll_id = params['id']
K>   @codes[poll_id]

K> or just: @codes[params['id']]

You're right. Thanks for answer, I'm a very beginning programmer :)

--
K.O.N.L.I.N.            removed_email_address@domain.invalid
   Kinetic Organism Normally for Logical Infiltration and Nullification,

          "Is this not what you expected to see?"
This topic is locked and can not be replied to.