Data Structures

Accessing Hash Values

You can access values in a hash just like an array.

pets = {
  "Stevie" => "cat",
  "Bowser" => "hamster",
  "Kevin Sorbo" => "fish"

puts pets["Stevie"]
# will print "cat"
  1. In the example above, we create a hash called pets.
  2. Then we print cat by accessing the key "Stevie" in the pets hash.
Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.