From: Yukihiro Matsumoto Date: 2005-08-03T07:02:02+09:00 Subject: Re: [RCR] array or with non-array Hi, In message "Re: [RCR] array or with non-array" on Wed, 3 Aug 2005 06:31:28 +0900, Simon Strandgaard writes: |Sometimes wish that [3, 5, 13] | [8] can be written as [3, 5, 13] | 8 |that ary |= 42 would be possible. | |chris2 pointed out that it would be inconsistent with [1,2,3] + 4. |maybe let + append 4 to the array? Too much conversion sometimes makes troubles, or hides troubles, for example, Ruby used to convert string and numbers more eagerly, but I stopped that to get a chance to detect human errors. But I'm not sure this conversion is harmless and useful, or not. Let us try for a while. Here's a patch to implement what you want. I will merge it to the HEAD, to see how it goes. matz. diff -p -u -1 -r1.173 array.c --- array.c 5 Jul 2005 14:23:22 -0000 1.173 +++ array.c 2 Aug 2005 22:01:43 -0000 @@ -304,2 +304,13 @@ to_ary(ary) static VALUE +make_ary(ary) + VALUE ary; +{ + VALUE tmp = rb_check_array_type(ary); + if (NIL_P(tmp)) { + return rb_ary_new3(1, ary); + } + return tmp; +} + +static VALUE to_a(ary) @@ -2346,6 +2357,12 @@ rb_ary_plus(x, y) { - VALUE z; + VALUE tmp, z; long len; - y = to_ary(y); + tmp = rb_check_array_type(y); + if (NIL_P(tmp)) { + z = rb_ary_dup(x); + rb_ary_push(z, y); + return z; + } + y = tmp; len = RARRAY(x)->len + RARRAY(y)->len; @@ -2696,6 +2713,6 @@ rb_ary_diff(ary1, ary2) { - VALUE ary3, hash; + VALUE tmp, ary3, hash; long i; - hash = ary_make_hash(to_ary(ary2), 0); + hash = ary_make_hash(make_ary(ary2), 0); ary3 = rb_ary_new(); @@ -2727,5 +2744,5 @@ rb_ary_and(ary1, ary2) - ary2 = to_ary(ary2); + ary2 = make_ary(ary2); ary3 = rb_ary_new2(RARRAY(ary1)->len < RARRAY(ary2)->len ? - RARRAY(ary1)->len : RARRAY(ary2)->len); + RARRAY(ary1)->len : RARRAY(ary2)->len); hash = ary_make_hash(ary2, 0); @@ -2761,3 +2778,3 @@ rb_ary_or(ary1, ary2) - ary2 = to_ary(ary2); + ary2 = make_ary(ary2); ary3 = rb_ary_new2(RARRAY(ary1)->len+RARRAY(ary2)->len);