diff --git a/mrblib/array.rb b/mrblib/array.rb index 5ce61928f..851f15cca 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -59,7 +59,7 @@ class Array idx = 0 len = size while idx < len - self[idx] = block.call self[idx] + self[idx] = block.call(self[idx]) idx += 1 end self @@ -75,32 +75,6 @@ class Array # ISO 15.2.12.5.20 alias map! collect! - ## - # Private method for Array creation. - # - # ISO 15.2.12.5.15 - def initialize(size=0, obj=nil, &block) - if size.is_a?(Array) && obj==nil && block == nil - self.replace(size) - return self - end - size = size.__to_int - raise ArgumentError, "negative array size" if size < 0 - - self.clear - if size > 0 - self[size - 1] = nil # allocate - - idx = 0 - while idx < size - self[idx] = (block)? block.call(idx): obj - idx += 1 - end - end - - self - end - ## # call-seq: # array == other -> true or false diff --git a/src/array.c b/src/array.c index eb3474842..c00b4f5d5 100644 --- a/src/array.c +++ b/src/array.c @@ -292,6 +292,40 @@ mrb_ary_s_create(mrb_state *mrb, mrb_value klass) static void ary_replace(mrb_state*, struct RArray*, struct RArray*); +static mrb_value +mrb_ary_init(mrb_state *mrb, mrb_value ary) +{ + mrb_value ss = mrb_fixnum_value(0); + mrb_value obj = mrb_nil_value(); + mrb_value blk = mrb_nil_value(); + + mrb_get_args(mrb, "|oo&", &ss, &obj, &blk); + + if (mrb_array_p(ss) && mrb_nil_p(obj) && mrb_nil_p(blk)) { + ary_replace(mrb, mrb_ary_ptr(ary), mrb_ary_ptr(ss)); + return ary; + } + + mrb_int size = mrb_as_int(mrb, ss); + struct RArray *a = mrb_ary_ptr(ary); + + if (ARY_CAPA(a) < size) { + ary_expand_capa(mrb, a, size); + } + + for (mrb_int i=0; i