How to get a value out of a hash in Ruby? -


i have following hash (@myhash) in ruby:

[   {     "id"        => "123456789",     "name"      =>"random name",      "list_type" =>"random type of list"   } ]  

how can take value of id out of hash? (basically result should be: 123456789)

  • when try doing @myhash[:id] following error "can't convert symbol integer"
  • when try doing @myhash['id'] following error "can't convert string integer"

i've tried adding .to_i, .to_s , etc. nothing helps.

what have there not hash. it's array of hashes (well, array of 1 hash, precise).

first have address proper element in array (first one), address value key.

@myhash[0]['id'] # => '123456789' # or @myhash.first['id'] # => '123456789'  

i following error "can't convert symbol integer"

you think you're working hash, in reality, it's array. arrays don't accept string or symbols keys. want integers index. hence error.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -