...

/

Setting a Default Value in Hash

Setting a Default Value in Hash

Learn how to set default values in a hash in Ruby.

We'll cover the following...

Interview question

It is often useful to have default values in a hash. We might even want to make a bookmark since we can use this trick while solving interview questions. We’ll take a look at one of these questions.

Given a sentence, calculate the usage frequency for each word. For example, there are two “the” words in the sentence, one “dog,” and so on. How do we solve this problem?

Imagine we have the string "the quick brown fox jumps over the lazy dog". Let’s split this string into ...