summaryrefslogtreecommitdiff
path: root/error.c
diff options
context:
space:
mode:
authorkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-01-26 04:51:14 +0000
committerkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-01-26 04:51:14 +0000
commitcd83d267311a45938d5c8d6be493ed288465c58b (patch)
treed0997247bc91823753b1412ba2de5effd9d720d7 /error.c
parent4fb218f9a0c98e75c1ffd8859c615d2e870e5f68 (diff)
KeyError#initialize accepts receiver and key.
[Feature #14313][ruby-core:84626] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62049 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'error.c')
-rw-r--r--error.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/error.c b/error.c
index 52ff87bc0f..d2699b91e3 100644
--- a/error.c
+++ b/error.c
@@ -1681,6 +1681,55 @@ rb_key_err_new(VALUE mesg, VALUE recv, VALUE key)
/*
* call-seq:
+ * KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
+ *
+ * Construct a new +KeyError+ exception with the given message,
+ * receiver and key.
+ */
+
+static VALUE
+key_err_initialize(int argc, VALUE *argv, VALUE self)
+{
+ VALUE message;
+ VALUE options;
+ VALUE receiver = Qnil;
+ VALUE key = Qnil;
+
+ rb_scan_args(argc, argv, "01:", &message, &options);
+
+ if (NIL_P(message)) {
+ rb_call_super(0, NULL);
+ }
+ else {
+ rb_call_super(1, &message);
+ }
+
+ if (!NIL_P(options)) {
+ static ID keywords[2];
+ VALUE values[2];
+ if (!keywords[0]) {
+ CONST_ID(keywords[0], "receiver");
+ }
+ if (!keywords[1]) {
+ CONST_ID(keywords[1], "key");
+ }
+ rb_get_kwargs(options, keywords, 0, 2, values);
+ if (values[0] != Qundef) {
+ receiver = values[0];
+ }
+ if (values[1] != Qundef) {
+ key = values[1];
+ }
+ }
+
+ rb_ivar_set(self, id_receiver, receiver);
+ rb_ivar_set(self, id_key, key);
+
+ return self;
+}
+
+/*
+ * call-seq:
* SyntaxError.new([msg]) -> syntax_error
*
* Construct a SyntaxError exception.
@@ -2299,6 +2348,7 @@ Init_Exception(void)
rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
+ rb_define_method(rb_eKeyError, "initialize", key_err_initialize, -1);
rb_define_method(rb_eKeyError, "receiver", key_err_receiver, 0);
rb_define_method(rb_eKeyError, "key", key_err_key, 0);
rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);