Skip to Content
Hashes and Symbols
Setting Your Own Default

You don’t have to settle for nil as a default value, however. If you create your hash using the syntax, you can specify a default like so:

my_hash ="Trady Blix")

Now if you try to access a nonexistent key in my_hash, you’ll get "Trady Blix" as a result.

You can always read more hashy goodness in the official Ruby documentation



In the meantime, create a hash called no_nil_hash and give it any default value other than nil.

Folder Icon

Take this course for free

Already have an account?