[前][次][番号順一覧][スレッド一覧]

ruby-changes:38864

From: nobu <ko1@a...>
Date: Thu, 18 Jun 2015 13:33:14 +0900 (JST)
Subject: [ruby-changes:38864] nobu:r50945 (trunk): error.c: NameError#receiver

nobu	2015-06-18 13:32:50 +0900 (Thu, 18 Jun 2015)

  New Revision: 50945

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50945

  Log:
    error.c: NameError#receiver
    
    * error.c (name_err_receiver): add NameError#receiver method.
      [Feature #10881]

  Modified files:
    trunk/ChangeLog
    trunk/error.c
    trunk/test/ruby/test_exception.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50944)
+++ ChangeLog	(revision 50945)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jun 18 13:32:46 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* error.c (name_err_receiver): add NameError#receiver method.
+	  [Feature #10881]
+
 Thu Jun 18 10:00:06 2015  SHIBATA Hiroshi  <hsbt@r...>
 
 	* safe.c: removed needless doc related $SAFE=3
Index: error.c
===================================================================
--- error.c	(revision 50944)
+++ error.c	(revision 50945)
@@ -1249,6 +1249,22 @@ name_err_mesg_load(VALUE klass, VALUE st https://github.com/ruby/ruby/blob/trunk/error.c#L1249
 
 /*
  * 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
  *
  * Return the arguments passed in as the third parameter to
@@ -1878,6 +1894,7 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L1894
     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);
Index: test/ruby/test_exception.rb
===================================================================
--- test/ruby/test_exception.rb	(revision 50944)
+++ test/ruby/test_exception.rb	(revision 50945)
@@ -659,6 +659,10 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L659
 
   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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L671
       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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]