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 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
nil are not the same thing:
false means “not true,” while
nil is Ruby’s way of saying “nothing at all.”
Go ahead and try to access a key in
creatures that doesn’t exist.