summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-10-02 04:31:23 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-10-02 04:31:23 +0000
commit75eee0bafdaa5adf30dc87cbb01b5f2e24ac8a01 (patch)
tree520cc63c5cccf0d97fd78bf8dbe62558ecff7f70 /array.c
parent34a31235c9eeaa9c1d1bcacd7470d2b37429a19f (diff)
* ext/socket/socket.c (unix_addr): getsockname(2) may result len = 0.
* ext/socket/socket.c (unix_peeraddr): getpeername(2) may result len = 0. * string.c (rb_str_subpat_set): support function for new argument pattern String#[re,offset] = val. [new] * eval.c (POP_BLOCK): rb_gc_force_recycle() was called too much. Should not be called if SCOPE_DONT_RECYCLE is set. * string.c (rb_str_aref_m): new argument pattern String#[re,offset]. [new] * string.c (rb_str_substr): should return an instance of receiver's class. * string.c (rb_str_succ): ditto. * array.c (rb_ary_subseq): ditto. * array.c (rb_ary_initialize): Array.new([1,2,3]) => [1,2,3]. [new] * string.c (rb_str_reverse): should return an instance of receiver's class. * string.c (rb_str_times): ditto. * array.c (rb_ary_times): ditto * string.c (str_gsub): ditto. * string.c (rb_str_ljust): ditto. * string.c (rb_str_rjust): ditto. * string.c (rb_str_center): ditto. * eval.c (eval): retrieves file, line information from binding. * eval.c (intersect_fds): counts intersecting fds. * eval.c (rb_thread_schedule): only fds requested by each thread count as select_value. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1761 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c69
1 files changed, 41 insertions, 28 deletions
diff --git a/array.c b/array.c
index 6e085ae..81b09cd 100644
--- a/array.c
+++ b/array.c
@@ -179,6 +179,22 @@ rb_ary_s_new(argc, argv, klass)
}
static VALUE
+to_ary(ary)
+ VALUE ary;
+{
+ return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
+}
+
+static VALUE
+to_ary_failed(failed)
+ int *failed;
+{
+ *failed = Qtrue;
+}
+
+static VALUE rb_ary_replace _((VALUE, VALUE));
+
+static VALUE
rb_ary_initialize(argc, argv, ary)
int argc;
VALUE *argv;
@@ -187,11 +203,17 @@ rb_ary_initialize(argc, argv, ary)
long len;
VALUE size, val;
+ rb_ary_modify(ary);
if (rb_scan_args(argc, argv, "02", &size, &val) == 0) {
+ RARRAY(ary)->len = 0;
+ return ary;
+ }
+
+ if (argc == 1 && !FIXNUM_P(size) && rb_respond_to(size, rb_intern("to_ary"))) {
+ rb_ary_replace(ary, rb_convert_type(size, T_ARRAY, "Array", "to_ary"));
return ary;
}
- rb_ary_modify(ary);
len = NUM2LONG(size);
if (len < 0) {
rb_raise(rb_eArgError, "negative array size");
@@ -409,6 +431,7 @@ rb_ary_subseq(ary, beg, len)
ary2 = rb_ary_new2(len);
MEMCPY(RARRAY(ary2)->ptr, RARRAY(ary)->ptr+beg, VALUE, len);
RARRAY(ary2)->len = len;
+ RBASIC(ary2)->klass = rb_obj_class(ary);
return ary2;
}
@@ -544,6 +567,20 @@ rb_ary_indexes(argc, argv, ary)
return new_ary;
}
+VALUE
+rb_ary_to_ary(obj)
+ VALUE obj;
+{
+ if (NIL_P(obj)) return rb_ary_new2(0);
+ if (TYPE(obj) == T_ARRAY) {
+ return obj;
+ }
+ if (rb_respond_to(obj, rb_intern("to_ary"))) {
+ return rb_convert_type(obj, T_ARRAY, "Array", "to_ary");
+ }
+ return rb_ary_new3(1, obj);
+}
+
static void
rb_ary_update(ary, beg, len, rpl)
VALUE ary;
@@ -552,12 +589,7 @@ rb_ary_update(ary, beg, len, rpl)
{
long rlen;
- if (NIL_P(rpl)) {
- rpl = rb_ary_new2(0);
- }
- else if (TYPE(rpl) != T_ARRAY) {
- rpl = rb_ary_new3(1, rpl);
- }
+ rpl = rb_ary_to_ary(rpl);
rlen = RARRAY(rpl)->len;
if (len < 0) rb_raise(rb_eIndexError, "negative length %d", len);
@@ -739,27 +771,6 @@ rb_ary_dup(ary)
return dup;
}
-static VALUE
-to_ary(ary)
- VALUE ary;
-{
- return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
-}
-
-VALUE
-rb_ary_to_ary(obj)
- VALUE obj;
-{
- if (NIL_P(obj)) return rb_ary_new2(0);
- if (TYPE(obj) == T_ARRAY) {
- return obj;
- }
- if (rb_respond_to(obj, rb_intern("to_ary"))) {
- return rb_convert_type(obj, T_ARRAY, "Array", "to_ary");
- }
- return rb_ary_new3(1, obj);
-}
-
extern VALUE rb_output_fs;
static VALUE
@@ -1342,6 +1353,8 @@ rb_ary_times(ary, times)
for (i=0; i<len; i+=RARRAY(ary)->len) {
MEMCPY(RARRAY(ary2)->ptr+i, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
}
+ OBJ_INFECT(ary2, ary);
+ RBASIC(ary2)->klass = rb_obj_class(ary);
return ary2;
}