From: Mike Stok Date: 2006-03-19T23:26:03+09:00 Subject: Re: String keys in hash On 19-Mar-06, at 9:13 AM, Konlin wrote: > 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_parameter > s > > 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. 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 Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.