3. Nil: a Formal Introduction


#1

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

Instructions

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
}


#2

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.


#4

Here's a hint.

if you type

creatures["weasels"]

and hit "save and submit" you get the result 0

if you type

creatures["puppies"]

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


#5

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

creatures[Zoidberg]

And it doesn't give me a "nil" response but an error:

uninitialized constant Context::Zoidberg


#6

Hey dr_chrizley,

You forgot to put "" around Zoidberg.

Try
creatures["Zoidberg"] instead

Syntax is the bain of a coders existence.


#7

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


#8

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


#9

can some one post the full correct code please?


#10

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.


#11

example: my_pets = hash.new {

}

example: my_pets[' do something']

example: puts my_pets[do something else']


#12

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


#13

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


#14

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


#15

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


#16

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.