Ruby 2.5.0 was recently released.
Earlier to Ruby 1.9 Hash#fetch
method would raise IndexError
for an invalid key.
An IndexError
is raised when the given index value is invalid.
KeyError
class was introduced in Ruby 1.9 and it inherits from IndexError
class. It is raised when the specified key is not found.
It was proposed that having KeyError#receiver
and KeyError#name
(or KeyError#key
) like NameError, will help to debug and find typo on
did_you_mean
gem.
There was a discussion about what method name is best between name
and key
.
Benoit Daloze suggested that name
is more consistent with other error
classes such as NameError#name
and NoMethodError#name
. Having key
as
method name was approved.
Ruby 2.5.0
Ruby 2.5.0 provides KeyError#receiver
and KeyError#key
as methods to return details of receiver object and key which was not found.
I hope these methods are useful in your future debugging attempts.