Intermediate Ruby
Q74 / 100

What does "Hash#fetch(key, default)" do differently from "hash[key]"?

Correct! Well done.

Incorrect.

The correct answer is B) fetch raises a KeyError if the key is missing and no default/block is given, while hash[key] simply returns nil for missing keys

B

Correct Answer

fetch raises a KeyError if the key is missing and no default/block is given, while hash[key] simply returns nil for missing keys

Explanation

fetch provides safer access by raising an error (or returning a specified default/block result) for missing keys, while [] silently returns nil.

Progress
74/100