Hi,

At Sat, 28 Apr 2001 19:35:25 +0900,
Marko Schulz <in6x059 / public.uni-hamburg.de> wrote:
> I would actually prefer an version that is able to insert more than
> one element, just like push can add more than one element at the
> end. So here is my take:
> 
> 
>   class Array 
>     def insert(pos, *elem)
>       self[pos,0] = elem
>       self
>     end
>   end

I agree.

Here is a simple implementation.


diff -u -r1.45 array.c --- array.c 2001/03/13 05:45:08 1.45 +++ array.c 2001/05/01 04:13:49 @@ -634,6 +634,23 @@ return argv[1]; } +static VALUE +rb_ary_insert(argc, argv, ary) + int argc; + VALUE *argv; + VALUE ary; +{ + VALUE pos, items; + + if (argc < 2) { + rb_raise(rb_eArgError, "wrong # of arguments(at least 2)"); + } + pos = argv[0]; + items = rb_ary_new4(argc - 1, argv + 1); + rb_ary_replace(ary, NUM2LONG(pos), 0, items); + return ary; +} + VALUE rb_ary_each(ary) VALUE ary; @@ -1673,6 +1690,7 @@ rb_define_method(rb_cArray, "pop", rb_ary_pop, 0); rb_define_method(rb_cArray, "shift", rb_ary_shift, 0); rb_define_method(rb_cArray, "unshift", rb_ary_unshift_m, -1); + rb_define_method(rb_cArray, "insert", rb_ary_insert, -1); rb_define_method(rb_cArray, "each", rb_ary_each, 0); rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0); rb_define_method(rb_cArray, "reverse_each", rb_ary_reverse_each, 0);
Shugo