From ab24be4e98660f4fbef44a03262f25988a260b9f Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 10 Feb 2003 09:40:13 +0000 Subject: [PATCH] * array.c (rb_ary_to_a): return value should be an Array if the receiver is an instance of subclass of Array. * string.c (rb_str_to_s): return value should be a String if the receiver is an instance of subclass of String. * eval.c (rb_call): calls method_missing when superclass method does not exist. * eval.c (rb_f_missing): now handles "no super" case. * object.c (rb_obj_ivar_get): Object#instance_variable_get: new method to get instance variable value without eval(). [new] * object.c (rb_obj_ivar_set): Object#instance_variable_set: new method to set instance variable value without eval(). [new] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 21 +++++++++++++++++++++ array.c | 14 +++++++++++++- eval.c | 7 +++++-- object.c | 26 ++++++++++++++++++++++++++ string.c | 5 +++++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 58ba28eff0710b..533298b198ef9d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Feb 10 10:14:26 2003 Yukihiro Matsumoto + + * array.c (rb_ary_to_a): return value should be an Array if the + receiver is an instance of subclass of Array. + + * string.c (rb_str_to_s): return value should be a String if the + receiver is an instance of subclass of String. + Mon Feb 10 03:33:42 2003 WATANABE Hirofumi * io.c (rb_file_sysopen): rb_file_sysopen_internal() needs four @@ -54,6 +62,19 @@ Sat Feb 8 03:34:28 2003 Akinori MUSHA * ruby.h (NORETURN_STYLE_NEW): Ditto. +Sat Feb 8 00:47:24 2003 Yukihiro Matsumoto + + * eval.c (rb_call): calls method_missing when superclass method + does not exist. + + * eval.c (rb_f_missing): now handles "no super" case. + + * object.c (rb_obj_ivar_get): Object#instance_variable_get: new + method to get instance variable value without eval(). [new] + + * object.c (rb_obj_ivar_set): Object#instance_variable_set: new + method to set instance variable value without eval(). [new] + Fri Feb 7 15:35:21 2003 Nobuyoshi Nakada * intern.h, re.c (rb_memsearch): returns long. diff --git a/array.c b/array.c index 7cedf1455d06f9..666a68a947f7fd 100644 --- a/array.c +++ b/array.c @@ -1000,6 +1000,18 @@ rb_ary_inspect(ary) static VALUE rb_ary_to_a(ary) VALUE ary; +{ + if (rb_obj_class(ary) != rb_cArray) { + VALUE dup = rb_ary_new2(RARRAY(ary)->len); + rb_ary_replace(dup, ary); + return dup; + } + return ary; +} + +static VALUE +rb_ary_to_ary_m(ary) + VALUE ary; { return ary; } @@ -1891,7 +1903,7 @@ Init_Array() rb_define_method(rb_cArray, "to_s", rb_ary_to_s, 0); rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0); rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0); - rb_define_method(rb_cArray, "to_ary", rb_ary_to_a, 0); + rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0); rb_define_method(rb_cArray, "frozen?", rb_ary_frozen_p, 0); rb_define_method(rb_cArray, "==", rb_ary_equal, 1); diff --git a/eval.c b/eval.c index a2cc8bb640e2a2..bd86870d8c6ec1 100644 --- a/eval.c +++ b/eval.c @@ -4352,6 +4352,7 @@ static int last_call_status; #define CSTAT_PRIV 1 #define CSTAT_PROT 2 #define CSTAT_VCALL 4 +#define CSTAT_SUPER 8 static VALUE rb_f_missing(argc, argv, obj) @@ -4412,6 +4413,9 @@ rb_f_missing(argc, argv, obj) exc = rb_eNameError; } } + else if (last_call_status & CSTAT_SUPER) { + format = "super: no superclass method `%s'"; + } if (!format) { format = "undefined method `%s' for %s%s%s"; } @@ -4807,8 +4811,7 @@ rb_call(klass, recv, mid, argc, argv, scope) } else if ((body = rb_get_method_body(&klass, &id, &noex)) == 0) { if (scope == 3) { - rb_name_error(mid, "super: no superclass method `%s'", - rb_id2name(mid)); + return rb_undefined(recv, mid, argc, argv, CSTAT_SUPER); } return rb_undefined(recv, mid, argc, argv, scope==2?CSTAT_VCALL:0); } diff --git a/object.c b/object.c index 7a59abee1300fb..5cfc3ed5793cb4 100644 --- a/object.c +++ b/object.c @@ -925,6 +925,30 @@ rb_obj_public_methods(obj) return rb_class_public_instance_methods(1, argv, CLASS_OF(obj)); } +static VALUE +rb_obj_ivar_get(obj, iv) + VALUE obj, iv; +{ + ID id = rb_to_id(iv); + + if (!rb_is_instance_id(id)) { + rb_name_error(id, "`%s' is not an instance variable name", rb_id2name(id)); + } + return rb_ivar_get(obj, id); +} + +static VALUE +rb_obj_ivar_set(obj, iv, val) + VALUE obj, iv, val; +{ + ID id = rb_to_id(iv); + + if (!rb_is_instance_id(id)) { + rb_name_error(id, "`%s' is not an instance variable name", rb_id2name(id)); + } + return rb_ivar_set(obj, id, val); +} + static VALUE convert_type(val, tname, method, raise) VALUE val; @@ -1346,6 +1370,8 @@ Init_Object() rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, 0); rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, 0); rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); + rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1); + rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2); rb_define_private_method(rb_mKernel, "remove_instance_variable", rb_obj_remove_instance_variable, 1); diff --git a/string.c b/string.c index 99813c969459d6..44f78a6b347cd0 100644 --- a/string.c +++ b/string.c @@ -1849,6 +1849,11 @@ static VALUE rb_str_to_s(str) VALUE str; { + if (rb_obj_class(str) != rb_cString) { + VALUE dup = str_alloc(rb_cString); + rb_str_replace(dup, str); + return dup; + } return str; }