Iterating Over Hashes - wrong example


#1

The example given is wrong

my_hash.each do |key, value|
puts key, my_hash[key]
end


#2

Really?

Did you try it out?

Because if you do, you'll realize that the code is doing what they say it does: it puts keys and values from a hash.


#3

Thanks arjofocolovi Yeah I didn't notice that the hash wasn't created while I was trying this in IRB.


#4

@cssace48385 can you help me with this i put the exact thing you put

creatures.each do |key, value|
puts key, creatures[key]
end


#5

This code should work, what error are you getting?


#6

Did you first create creatures hash? What error are you getting?


#7

thats what it says:
Did you try to access a key in the creatures hash that doesn't exist?


#8

@cssace48385
creatures = { "weasels" => 0,
"puppies" => 6,
"platypuses" => 3,
"canaries" => 1,
"Heffalumps" => 7,
"Tiggers" => 1
}
creatures.each do |key, value|
puts key, creatures[key]
end


#9

When I run your code in the lab, I get no error at all. It may come from the exercise you’re at. Each exercise checks for different things and it may give you an error only because it doesn’t solve the exercise, even though your code could be correct.

Try it on this page: labs repl.it


#10

how do i get it to pass it then cause it does work on the labs @arjofocolovi


#11

Which error are you getting?


#12

thats what it says:
Did you try to access a key in the creatures hash that doesn't exist?
@arjofocolovi


#13

Yeah sorry you already gave it above, I forgot.

Is it the 3/15 exercise?


#14

yes 3/15 @arjofocolovi


#15

Then your problem is that the instructions ask you to try accessing a non-existing key, and instead you're listing all the existing keys and values ^^.


#16

which keys don't exist? @arjofocolovi


#17

Well, any key which isn't entered in the hash. For example "yolo", or "tailor". anything you like pretty much.

The hint gives you a key you can try as an example.


#18

@arjofocolovi What am i doing wrong here?
creatures = { "weasels" => 0,
"puppies" => 6,
"platypuses" => 3,
"canaries" => 1,
"Heffalumps" => 7,
"Tiggers" => 1
}
creatures.each do |key, value|
puts key, creatures_yolo[key]
end


#19

I told you, you're displaying the content of the hash, which is not what is asked of you in this exercise.

Instead, they're asking you to try to access a key which does NOT exist in the hash creatures.

So for example, as the hint says:

                Why not "Zoidberg"?

Remember, you can access the value of a particular key in a hash with

hash_name[key]


#20

i do understand that but i don't know how to put it @arjofocolovi