Skip to Content
Learn
Hashes and Symbols
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

1.

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

Folder Icon

Take this course for free

Already have an account?