Ruby 2.6 KeyError#initialize accepts message, receiver, and key as arguments

Ruby 2.6.0-preview2 was recently released. Ruby 2.5.0 introduced public instance methods KeyError#key and KeyError#receiver. You can read my earlier blog about KeyError#key & KeyError#receiver here. Ruby csv library raises a KeyError on CSV::Row#fetch method when the field is not found. def fetch(header, *varargs) # … raise KeyError, "key not found: #{header}" # … end Instead […]

Ruby 2.6.0-preview2 was recently released.
Ruby 2.5.0 introduced public instance methods KeyError#key and KeyError#receiver. You can read my earlier blog about KeyError#key & KeyError#receiver here.
Ruby csv library raises a KeyError on CSV::Row#fetch method when the field is not found.
def fetch(header, *varargs) # … raise KeyError, "key not found: #{header}" # … end Instead of raising a KeyError shown above, how about if this is possible? –
raise KeyError.new("key not found", key: header) with this we can check the KeyError object for the error message and key using KeyError#message and KeyError#key methods which were introduced in Ruby 2.
Source: Codemancers