Exception: KeyError
- Inherits:
-
IndexError
- Object
- Exception
- StandardError
- IndexError
- KeyError
- Defined in:
- error.c,
error.c
Overview
Raised when the specified key is not found. It is a subclass of IndexError.
h = {"foo" => :bar}
h.fetch("foo") #=> :bar
h.fetch("baz") #=> KeyError: key not found: "baz"
Instance Method Summary collapse
-
#new(message = nil, receiver: nil, key: nil) ⇒ Object
constructor
Construct a new
KeyError
exception with the given message, receiver and key. -
#key ⇒ Object
Return the key caused this KeyError exception.
-
#receiver ⇒ Object
Return the receiver associated with this KeyError exception.
Methods inherited from Exception
#==, #backtrace, #backtrace_locations, #cause, #exception, exception, #full_message, #inspect, #message, #set_backtrace, #to_s, to_tty?
Constructor Details
#new(message = nil, receiver: nil, key: nil) ⇒ Object
Construct a new KeyError
exception with the given message, receiver and key.
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 |
# File 'error.c', line 2073
static VALUE
key_err_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE options;
rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
if (!NIL_P(options)) {
ID keywords[2];
VALUE values[numberof(keywords)];
int i;
keywords[0] = id_receiver;
keywords[1] = id_key;
rb_get_kwargs(options, keywords, 0, numberof(values), values);
for (i = 0; i < numberof(values); ++i) {
if (values[i] != Qundef) {
rb_ivar_set(self, keywords[i], values[i]);
}
}
}
return self;
}
|
Instance Method Details
#key ⇒ Object
Return the key caused this KeyError exception.
2044 2045 2046 2047 2048 2049 2050 2051 2052 |
# File 'error.c', line 2044
static VALUE
key_err_key(VALUE self)
{
VALUE key;
key = rb_ivar_lookup(self, id_key, Qundef);
if (key != Qundef) return key;
rb_raise(rb_eArgError, "no key is available");
}
|
#receiver ⇒ Object
Return the receiver associated with this KeyError exception.
2027 2028 2029 2030 2031 2032 2033 2034 2035 |
# File 'error.c', line 2027
static VALUE
key_err_receiver(VALUE self)
{
VALUE recv;
recv = rb_ivar_lookup(self, id_receiver, Qundef);
if (recv != Qundef) return recv;
rb_raise(rb_eArgError, "no receiver is available");
}
|