* eval.c (avalue_to_svalue): use rb_check_array_type() again.

Clarify how "to_ary" and "to_a" work. [ruby-talk:68155]

* eval.c (svalue_to_avalue): ditto.

* eval.c (svalue_to_mrhs): ditto.

* eval.c (rb_eval): unary splat to use to_a, but we need a hack to
  exclude Object#to_a until it's removed.

* object.c (rb_Array): check obj.respond_to?("to_a").  Currently
  all object respond_to "to_a", but Object#to_a will be removed.

* range.c (Init_Range): undefine to_ary.

* re.c (Init_Regexp): ditto.

* regex.c (re_compile_pattern): do not warn if "-" is at the top
  or last of character class.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3633 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2003-03-29 02:56:18 +00:00
parent 95785964f6
commit fec60bc7c4
8 changed files with 84 additions and 33 deletions

View File

@ -1,3 +1,22 @@
Sat Mar 29 09:48:35 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* eval.c (avalue_to_svalue): use rb_check_array_type() again.
Clarify how "to_ary" and "to_a" work. [ruby-talk:68155]
* eval.c (svalue_to_avalue): ditto.
* eval.c (svalue_to_mrhs): ditto.
* eval.c (rb_eval): unary splat to use to_a, but we need a hack to
exclude Object#to_a until it's removed.
* object.c (rb_Array): check obj.respond_to?("to_a"). Currently
all object respond_to "to_a", but Object#to_a will be removed.
* range.c (Init_Range): undefine to_ary.
* re.c (Init_Regexp): ditto.
Sat Mar 29 09:47:52 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net> Sat Mar 29 09:47:52 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
* MANIFEST (ext/aix_mksym.rb): remove obsolete file. * MANIFEST (ext/aix_mksym.rb): remove obsolete file.
@ -10,6 +29,11 @@ Fri Mar 28 19:33:39 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
* variable.c (classname): remove temporary class path when exact * variable.c (classname): remove temporary class path when exact
name found. name found.
Fri Mar 28 18:29:23 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* regex.c (re_compile_pattern): do not warn if "-" is at the top
or last of character class.
Thu Mar 27 12:10:15 2003 Tanaka Akira <akr@m17n.org> Thu Mar 27 12:10:15 2003 Tanaka Akira <akr@m17n.org>
* regex.c (re_compile_pattern): fix [:name:] handling. * regex.c (re_compile_pattern): fix [:name:] handling.

72
eval.c
View File

@ -2201,42 +2201,47 @@ static VALUE
avalue_to_svalue(v) avalue_to_svalue(v)
VALUE v; VALUE v;
{ {
if (TYPE(v) != T_ARRAY) { VALUE tmp, top;
tmp = rb_check_array_type(v);
if (NIL_P(v)) {
return v; return v;
} }
if (RARRAY(v)->len == 0) { if (RARRAY(tmp)->len == 0) {
return Qundef; return Qundef;
} }
if (RARRAY(v)->len == 1) { if (RARRAY(tmp)->len == 1) {
VALUE tmp = RARRAY(v)->ptr[0]; top = rb_check_array_type(RARRAY(tmp)->ptr[0]);
if (TYPE(tmp) != T_ARRAY) { if (NIL_P(top)) {
return tmp; return RARRAY(tmp)->ptr[0];
} }
if (RARRAY(tmp)->len > 1) { if (RARRAY(top)->len > 1) {
return v; return v;
} }
return tmp; return top;
} }
return v; return tmp;
} }
static VALUE static VALUE
svalue_to_avalue(v) svalue_to_avalue(v)
VALUE v; VALUE v;
{ {
VALUE tmp; VALUE tmp, top;
if (v == Qundef) return rb_ary_new2(0); if (v == Qundef) return rb_ary_new2(0);
if (TYPE(v) != T_ARRAY) { tmp = rb_check_array_type(v);
if (NIL_P(tmp)) {
return rb_ary_new3(1, v); return rb_ary_new3(1, v);
} }
if (RARRAY(v)->len == 1) { if (RARRAY(tmp)->len == 1) {
tmp = RARRAY(v)->ptr[0]; top = rb_check_array_type(RARRAY(tmp)->ptr[0]);
if (TYPE(tmp) == T_ARRAY && RARRAY(tmp)->len > 1) if (!NIL_P(top) && RARRAY(top)->len > 1) {
return v; return v;
}
return rb_ary_new3(1, v); return rb_ary_new3(1, v);
} }
return v; return tmp;
} }
static VALUE static VALUE
@ -2257,15 +2262,18 @@ svalue_to_mrhs(v, lhs)
VALUE v; VALUE v;
NODE *lhs; NODE *lhs;
{ {
VALUE tmp;
if (v == Qundef) return rb_ary_new2(0); if (v == Qundef) return rb_ary_new2(0);
if (TYPE(v) != T_ARRAY) { tmp = rb_check_array_type(v);
if (NIL_P(tmp)) {
return rb_ary_new3(1, v); return rb_ary_new3(1, v);
} }
/* no lhs means splat lhs only */ /* no lhs means splat lhs only */
if (!lhs && RARRAY(v)->len <= 1) { if (!lhs && RARRAY(tmp)->len <= 1) {
return rb_ary_new3(1, v); return rb_ary_new3(1, v);
} }
return v; return tmp;
} }
static VALUE static VALUE
@ -2652,15 +2660,31 @@ rb_eval(self, n)
break; break;
case NODE_SPLAT: case NODE_SPLAT:
{ #if 0
VALUE tmp; /* simplified version after Object#to_a removed */
result = rb_eval(self, node->nd_head);
if (NIL_P(result)) result = rb_ary_new3(1, Qnil);
result = avalue_splat(rb_Array(result));
#else
result = rb_eval(self, node->nd_head);
if (NIL_P(result)) result = rb_ary_new3(1, Qnil);
if (TYPE(result) != T_ARRAY) {
VALUE tmp = rb_check_array_type(result);
if (NIL_P(tmp)) {
VALUE origin;
ID id = rb_intern("to_a");
result = rb_eval(self, node->nd_head); if (search_method(CLASS_OF(result), id, &origin) &&
tmp = rb_check_array_type(result); origin != RCLASS(rb_cObject)->super) { /* exclude Object#to_a */
if (!NIL_P(tmp)) { result = rb_funcall(result, id, 0);
result = avalue_splat(tmp); }
else {
result = rb_ary_new3(1, result);
}
} }
} }
result = avalue_splat(rb_Array(result));
#endif
break; break;
case NODE_SVALUE: case NODE_SVALUE:

View File

@ -138,7 +138,7 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self)
const char *name, *stype; const char *name, *stype;
const char *err; const char *err;
rb_secure(4); rb_secure(2);
if (rb_scan_args(argc, argv, "11", &sym, &type) == 2) { if (rb_scan_args(argc, argv, "11", &sym, &type) == 2) {
SafeStringValue(type); SafeStringValue(type);
stype = StringValuePtr(type); stype = StringValuePtr(type);
@ -159,9 +159,8 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self)
name = StringValuePtr(sym); name = StringValuePtr(sym);
} }
Data_Get_Struct(self, struct dl_handle, dlhandle); Data_Get_Struct(self, struct dl_handle, dlhandle);
if (! dlhandle->open) { if (!dlhandle->open) {
rb_raise(rb_eRuntimeError, "Closed handle."); rb_raise(rb_eRuntimeError, "Closed handle.");
} }
handle = dlhandle->ptr; handle = dlhandle->ptr;

View File

@ -330,7 +330,7 @@ rb_dlsym_call(int argc, VALUE argv[], VALUE self)
long ftype; long ftype;
void *func; void *func;
rb_secure(2); rb_secure_update(self);
Data_Get_Struct(self, struct sym_data, sym); Data_Get_Struct(self, struct sym_data, sym);
DEBUG_CODE({ DEBUG_CODE({
printf("rb_dlsym_call(): type = '%s', func = 0x%x\n", sym->type, sym->func); printf("rb_dlsym_call(): type = '%s', func = 0x%x\n", sym->type, sym->func);

View File

@ -1270,10 +1270,16 @@ rb_Array(val)
val = rb_funcall(val, to_ary, 0); val = rb_funcall(val, to_ary, 0);
} }
else { else {
val = rb_funcall(val, rb_intern("to_a"), 0); to_ary = rb_intern("to_a");
if (rb_respond_to(val, to_ary)) {
val = rb_funcall(val, to_ary, 0);
}
else {
val = rb_ary_new3(1, val);
}
} }
if (TYPE(val) != T_ARRAY) { if (TYPE(val) != T_ARRAY) {
rb_raise(rb_eTypeError, "`to_a' did not return Array"); rb_raise(rb_eTypeError, "`%s' did not return Array", rb_id2name(to_ary));
} }
return val; return val;
} }

View File

@ -497,7 +497,6 @@ Init_Range()
rb_define_method(rb_cRange, "end", range_last, 0); rb_define_method(rb_cRange, "end", range_last, 0);
rb_define_method(rb_cRange, "to_s", range_to_s, 0); rb_define_method(rb_cRange, "to_s", range_to_s, 0);
rb_define_method(rb_cRange, "inspect", range_inspect, 0); rb_define_method(rb_cRange, "inspect", range_inspect, 0);
rb_define_alias(rb_cRange, "to_ary", "to_a");
rb_define_method(rb_cRange, "exclude_end?", range_exclude_end_p, 0); rb_define_method(rb_cRange, "exclude_end?", range_exclude_end_p, 0);

1
re.c
View File

@ -1713,7 +1713,6 @@ Init_Regexp()
rb_define_method(rb_cMatch, "begin", match_begin, 1); rb_define_method(rb_cMatch, "begin", match_begin, 1);
rb_define_method(rb_cMatch, "end", match_end, 1); rb_define_method(rb_cMatch, "end", match_end, 1);
rb_define_method(rb_cMatch, "to_a", match_to_a, 0); rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
rb_define_method(rb_cMatch, "to_ary", match_to_a, 0);
rb_define_method(rb_cMatch, "[]", match_aref, -1); rb_define_method(rb_cMatch, "[]", match_aref, -1);
rb_define_method(rb_cMatch, "select", match_select, -1); rb_define_method(rb_cMatch, "select", match_select, -1);
rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0); rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);

View File

@ -1487,7 +1487,7 @@ re_compile_pattern(pattern, size, bufp)
} }
had_char_class = 0; had_char_class = 0;
if (c == '-') if (c == '-' && p != p0 + 1 && *p != ']')
re_warning("character class has `-' without escape"); re_warning("character class has `-' without escape");
if (c == '[' && *p != ':') if (c == '[' && *p != ':')
re_warning("character class has `[' without escape"); re_warning("character class has `[' without escape");