3. Nil: a Formal Introduction

I have not clue what I am supposed to do here:/

Nil: a Formal Introduction

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.”


Go ahead and try to access a key in creatures that doesn’t exist.

creatures = { “weasels” => 0,
“puppies” => 6,
“platypuses” => 3,
“canaries” => 1,
“Heffalumps” => 7,
“Tiggers” => 1

1 Like

They’re asking you to access a key in the hash creatures. A key that doesn’t exist in the hash.

How do you access a key from a hash? If you don’t remember, you should go back to the previous lessons.

Here’s a hint.

if you type


and hit “save and submit” you get the result 0

if you type


and hit “save and submit” you get the result 6

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”

Hope that helps


There’s something wrong. I called a key that isn’t in the hash (I even called the suggested one “Zoidberg”):


And it doesn’t give me a “nil” response but an error:

uninitialized constant Context::Zoidberg

Hey dr_chrizley,

You forgot to put “” around Zoidberg.

creatures["Zoidberg"] instead

Syntax is the bain of a coders existence.

1 Like

:unamused: You’re right. Thanks for the quick response.

Thanks everyone! I was missing the quotation marks lol I feel dumb :joy:

can some one post the full correct code please?

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.

1 Like

example: my_pets = hash.new {


example: my_pets[’ do something’]

example: puts my_pets[do something else’]

Hint to other newbies: if you’re trying to call a .each function, you may be overthinking it.

I was confused by this to but that explains it well. thanks

Just type a string with number of argument, which doesn’t exist: “creatures[6]”, for example

You can also do this: “creatures[“Cats”]” instead for other animals that doesn’t appear on the list.

So this does not work for me. I tried putting the hash name and request the item
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.