简体   繁体   中英

Ruby on Rails hash not working

I have a hash that I want to extract the values from, but when I try its empty.

When I display the variable with the hash this is what shows.

{"raspado"=>{"id"=>28520497, "name"=>"Raspado", "profileIconId"=>508, "summonerLevel"=>30, "revisionDate"=>1394570420000}}

When I debug the variable with the hash this is what I get.

---
raspado:
  id: 28520497
  name: Raspado
  profileIconId: 508
  summonerLevel: 30
  revisionDate: 1394570420000

When I try to access a key via

debug @data[:id]

I get

--- 
...

what I'm I doing wrong?

id is the key in the hash which is the value of key raspado of @data .

You should do:

debug @data['raspado']['id']

If you have multiple keys like ' raspado in the hash', then you could get the values with the code below:

@data.each do |index, values|
  debug (values['x'])
end

ref: How do I get data from a hash?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM