summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-01-28 08:44:45 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-01-28 08:44:45 +0000
commit4f38c453b4fccfbc529909781c2c1659f256697b (patch)
treef5f2eb688ce0d96f6b51515366f2dbf048cc3639 /array.c
parentae6afaaedbe2c88abbfc315a66685e6b99e4050c (diff)
* eval.c (is_defined): defined?(Foo::Baz) should check constants
only, no methods. * eval.c (is_defined): should not dump core on defined?(a::b) where a is not a class nor a module. * object.c (Init_Object): remove dup and clone from TrueClass, FalseClass, and NilClass. * array.c (rb_ary_fill): Array#fill takes block to get the value to fill. * string.c (rb_str_to_i): to_i(0) auto-detects base radix. * array.c (rb_ary_initialize): fill by the block evaluation value if block is given. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2021 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c54
1 files changed, 45 insertions, 9 deletions
diff --git a/array.c b/array.c
index f4ec707..f2ff470 100644
--- a/array.c
+++ b/array.c
@@ -228,12 +228,18 @@ rb_ary_initialize(argc, argv, ary)
rb_ary_modify(ary);
if (rb_scan_args(argc, argv, "02", &size, &val) == 0) {
RARRAY(ary)->len = 0;
+ if (rb_block_given_p()) {
+ rb_warning("given block not used");
+ }
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;
+ if (argc == 1 && !FIXNUM_P(size)) {
+ val = rb_check_convert_type(size, T_ARRAY, "Array", "to_ary");
+ if (!NIL_P(val)) {
+ rb_ary_replace(ary, val);
+ return ary;
+ }
}
len = NUM2LONG(size);
@@ -247,8 +253,21 @@ rb_ary_initialize(argc, argv, ary)
RARRAY(ary)->aux.capa = len;
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
}
- memfill(RARRAY(ary)->ptr, len, val);
- RARRAY(ary)->len = len;
+ if (rb_block_given_p()) {
+ long i;
+
+ if (argc > 1) {
+ rb_raise(rb_eArgError, "wrong number of arguments");
+ }
+ for (i=0; i<len; i++) {
+ RARRAY(ary)->ptr[i] = rb_yield(INT2NUM(i));
+ RARRAY(ary)->len = i+1;
+ }
+ }
+ else {
+ memfill(RARRAY(ary)->ptr, len, val);
+ RARRAY(ary)->len = len;
+ }
return ary;
}
@@ -1343,6 +1362,7 @@ static VALUE
rb_ary_replace(ary, ary2)
VALUE ary, ary2;
{
+ if (ary == ary2) return ary;
ary2 = to_ary(ary2);
rb_ary_update(ary, 0, RARRAY(ary)->len, ary2);
return ary;
@@ -1370,12 +1390,20 @@ rb_ary_fill(argc, argv, ary)
VALUE item, arg1, arg2;
long beg, end, len;
VALUE *p, *pend;
+ int block_p;
- rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
+ if (rb_block_given_p()) {
+ block_p = Qtrue;
+ rb_scan_args(argc, argv, "02", &arg1, &arg2);
+ argc += 1; /* hackish */
+ }
+ else {
+ rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
+ }
switch (argc) {
case 1:
beg = 0;
- len = RARRAY(ary)->len - beg;
+ len = RARRAY(ary)->len;
break;
case 2:
if (rb_range_beg_len(arg1, &beg, &len, RARRAY(ary)->len, 1)) {
@@ -1405,8 +1433,16 @@ rb_ary_fill(argc, argv, ary)
}
p = RARRAY(ary)->ptr + beg; pend = p + len;
- while (p < pend) {
- *p++ = item;
+ if (block_p) {
+ while (p < pend) {
+ *p++ = rb_yield(INT2NUM(beg));
+ beg++;
+ }
+ }
+ else {
+ while (p < pend) {
+ *p++ = item;
+ }
}
return ary;
}