Accessing Hash Value problems


#1

This is my code

pets = Hash.new
pets = {
"Stevie" => "cat",
"Bowser" => "hamster",
"Kevin Sorbo" => "fish"
}
puts pets ["Stevie"]

Despite doing what the computer says and getting the desired name on screen, I get a wrong notation.

I get the following message: "Oops, try again. It looks like you didn't print out the value of each of the keys stored in your hash." Despite clearly getting the name typed.

Can anyone help?

Grtz


#2

It says to print each key doesn't it?


#3

Well, with this code it doesn't help either

puts pets ["Stevie", "Bowser", "Kevin Sorbo"]


#4

Wouldn't it be better to tell ruby to print each one than to list them yourself? What if there was a million of them, would you really do it that way? Or what if it changed every few milliseconds?


#5

And how do you do that?


#6

The second exercise of the category has an example of that:
https://www.codecademy.com/en/courses/ruby-beginner-en-Qn7Qw/0/2
But that doesn't look like where you are, no idea where you are or what the instructions ask for. The error message says to print each one, I suspect they explain how they want that done in that exercise?


#7

I got it, thanks for helping me out!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.