Skill level: beginner
When accessing an item in a Ruby hash, there are many times when we need to set a default value in the event the key is missing.
|| or Operator
One technique we can use is the
I tend to stay away from this technique due to the sometimes unexpected behavior
produced when using it with boolean values.
The following code is checking for the
boolean_value key in the options
hash. The expected behavior is that if the key is present, its value will
be used. If the key is missing then the value will default to
The problem here is that if the value is either
will be set to true.
1 2 3 4
Fetch is another technique and the one I use most of the time.