What happens if you try to access a key that doesn’t exist, though?
In many languages, you’ll get an error of some kind. Not so in Ruby: you’ll instead get the special value nil.
Along with false, nil is one of two non-true values in Ruby. (Every other object is regarded as “truthy,” meaning that if you were to type if 2 or if “bacon”, the code in that if statement would be run.)
It’s important to realize that false and nil are not the same thing: false means “not true,” while nil is Ruby’s way of saying “nothing at all.”
Instructions
Go ahead and try to access a key in creatures that doesn’t exist.
I could go down the list of animals, but do you see the pattern?
What happens if you type in an animal that isn’t on the list? Go ahead and try it. You should get the result nil
In this example, the animal name is the key and the number is the value
So typing in a key that doesn’t exist just means “type in an animal name that isn’t on the list of the creatures hash”
No, because it won’t help you to copy paste a code just to get to the next exercise. If you’re having troubles with an exercise, post your code, the number of the exercise and the error you’re getting in a new thread, and people will try to help you.
So this does not work for me. I tried putting the hash name and request the item
creatures[“Xwing”]
or
creatures[“Zoidberg”]
and I get
undefined method `’ for nil:NilClass
Ok, never mind. It looks like my part above was ok, I had to take out sine text around the hash name creatures before it would work. I was too trusting that the first part of the code was right.