* 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
This commit is contained in:
nobu 2015-06-18 04:32:50 +00:00
Родитель 4219194730
Коммит 51de63dfb9
3 изменённых файлов: 28 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,8 @@
Thu Jun 18 13:32:46 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* error.c (name_err_receiver): add NameError#receiver method.
[Feature #10881]
Thu Jun 18 10:00:06 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
* safe.c: removed needless doc related $SAFE=3

17
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);

Просмотреть файл

@ -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