From 51de63dfb94d1f33ac09c7272a077192d733c5e4 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 18 Jun 2015 04:32:50 +0000 Subject: [PATCH] error.c: NameError#receiver * error.c (name_err_receiver): add NameError#receiver method. [Feature #10881] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50945 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ error.c | 17 +++++++++++++++++ test/ruby/test_exception.rb | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/ChangeLog b/ChangeLog index 9edb0240a7..310645bab4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Jun 18 13:32:46 2015 Nobuyoshi Nakada + + * error.c (name_err_receiver): add NameError#receiver method. + [Feature #10881] + Thu Jun 18 10:00:06 2015 SHIBATA Hiroshi * safe.c: removed needless doc related $SAFE=3 diff --git a/error.c b/error.c index 07b04a1036..602ee401c7 100644 --- a/error.c +++ b/error.c @@ -1247,6 +1247,22 @@ name_err_mesg_load(VALUE klass, VALUE str) return str; } +/* + * call-seq: + * name_error.receiver -> object + * + * Return the receiver associated with this NameError exception. + */ + +static VALUE +name_err_receiver(VALUE self) +{ + VALUE *ptr, mesg = rb_attr_get(self, id_mesg); + + TypedData_Get_Struct(mesg, VALUE, &name_err_mesg_data_type, ptr); + return ptr[1]; +} + /* * call-seq: * no_method_error.args -> obj @@ -1878,6 +1894,7 @@ Init_Exception(void) rb_eNameError = rb_define_class("NameError", rb_eStandardError); rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1); rb_define_method(rb_eNameError, "name", name_err_name, 0); + rb_define_method(rb_eNameError, "receiver", name_err_receiver, 0); rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cData); rb_define_singleton_method(rb_cNameErrorMesg, "!", rb_name_err_mesg_new, NAME_ERR_MESG_COUNT); rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1); diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index c68d226333..2817c63bed 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -659,6 +659,10 @@ end.join def test_name_error_info obj = BasicObject.new + class << obj + alias object_id __id__ + def pretty_inspect; "`obj'"; end + end e = assert_raise(NameError) { obj.instance_eval("Object") } @@ -667,11 +671,13 @@ end.join obj.instance_eval {foo} } assert_equal(:foo, e.name) + assert_same(obj, e.receiver) e = assert_raise(NoMethodError) { obj.foo(1, 2) } assert_equal(:foo, e.name) assert_equal([1, 2], e.args) + assert_same(obj, e.receiver) end def test_output_string_encoding