ruby-changes:49931
From: kou <ko1@a...>
Date: Fri, 26 Jan 2018 13:51:19 +0900 (JST)
Subject: [ruby-changes:49931] kou:r62049 (trunk): KeyError#initialize accepts receiver and key.
kou 2018-01-26 13:51:14 +0900 (Fri, 26 Jan 2018) New Revision: 62049 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62049 Log: KeyError#initialize accepts receiver and key. [Feature #14313][ruby-core:84626] Added files: trunk/test/ruby/test_key_error.rb Modified files: trunk/error.c Index: test/ruby/test_key_error.rb =================================================================== --- test/ruby/test_key_error.rb (nonexistent) +++ test/ruby/test_key_error.rb (revision 62049) @@ -0,0 +1,38 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_key_error.rb#L1 +require 'test/unit' + +class TestKeyError < Test::Unit::TestCase + def test_default + error = KeyError.new + assert_equal("KeyError", error.message) + end + + def test_message + error = KeyError.new("Message") + assert_equal("Message", error.message) + end + + def test_receiver + receiver = Object.new + error = KeyError.new(receiver: receiver) + assert_equal(receiver, error.receiver) + end + + def test_key + error = KeyError.new(key: :key) + assert_equal(:key, error.key) + end + + def test_receiver_and_key + receiver = Object.new + error = KeyError.new(receiver: receiver, key: :key) + assert_equal([receiver, :key], + [error.receiver, error.key]) + end + + def test_all + receiver = Object.new + error = KeyError.new("Message", receiver: receiver, key: :key) + assert_equal(["Message", receiver, :key], + [error.message, error.receiver, error.key]) + end +end Property changes on: test/ruby/test_key_error.rb ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +LF \ No newline at end of property Index: error.c =================================================================== --- error.c (revision 62048) +++ error.c (revision 62049) @@ -1681,6 +1681,55 @@ rb_key_err_new(VALUE mesg, VALUE recv, V https://github.com/ruby/ruby/blob/trunk/error.c#L1681 /* * 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) https://github.com/ruby/ruby/blob/trunk/error.c#L2348 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); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/