1 /**********************************************************************
6 created at: Fri Aug 6 09:46:12 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
12 **********************************************************************/
14 #include "ruby/ruby.h"
15 #include "ruby/util.h"
22 #define ARY_DEFAULT_SIZE 16
25 rb_mem_clear(register VALUE
*mem
, register long size
)
33 memfill(register VALUE
*mem
, register long size
, register VALUE val
)
40 #define ARY_SHARED_P(a) FL_TEST(a, ELTS_SHARED)
42 #define ARY_SET_LEN(ary, n) do { \
43 RARRAY(ary)->len = (n);\
46 #define ARY_CAPA(ary) RARRAY(ary)->aux.capa
47 #define RESIZE_CAPA(ary,capacity) do {\
48 REALLOC_N(RARRAY(ary)->ptr, VALUE, (capacity));\
49 RARRAY(ary)->aux.capa = (capacity);\
53 rb_ary_modify_check(VALUE ary
)
55 if (OBJ_FROZEN(ary
)) rb_error_frozen("array");
56 if (!OBJ_TAINTED(ary
) && rb_safe_level() >= 4)
57 rb_raise(rb_eSecurityError
, "Insecure: can't modify array");
61 rb_ary_modify(VALUE ary
)
65 rb_ary_modify_check(ary
);
66 if (ARY_SHARED_P(ary
)) {
67 ptr
= ALLOC_N(VALUE
, RARRAY_LEN(ary
));
68 FL_UNSET(ary
, ELTS_SHARED
);
69 RARRAY(ary
)->aux
.capa
= RARRAY_LEN(ary
);
70 MEMCPY(ptr
, RARRAY_PTR(ary
), VALUE
, RARRAY_LEN(ary
));
71 RARRAY(ary
)->ptr
= ptr
;
76 rb_ary_freeze(VALUE ary
)
78 return rb_obj_freeze(ary
);
83 * array.frozen? -> true or false
85 * Return <code>true</code> if this array is frozen (or temporarily frozen
86 * while being sorted).
90 rb_ary_frozen_p(VALUE ary
)
92 if (OBJ_FROZEN(ary
)) return Qtrue
;
97 ary_alloc(VALUE klass
)
99 NEWOBJ(ary
, struct RArray
);
100 OBJSETUP(ary
, klass
, T_ARRAY
);
110 ary_new(VALUE klass
, long len
)
115 rb_raise(rb_eArgError
, "negative array size (or size too big)");
117 if (len
> LONG_MAX
/ sizeof(VALUE
)) {
118 rb_raise(rb_eArgError
, "array size too big");
120 ary
= ary_alloc(klass
);
122 RARRAY(ary
)->ptr
= ALLOC_N(VALUE
, len
);
123 RARRAY(ary
)->aux
.capa
= len
;
129 rb_ary_new2(long len
)
131 return ary_new(rb_cArray
, len
);
138 return rb_ary_new2(ARY_DEFAULT_SIZE
);
144 rb_ary_new3(long n
, ...)
150 ary
= rb_ary_new2(n
);
153 for (i
=0; i
<n
; i
++) {
154 RARRAY_PTR(ary
)[i
] = va_arg(ar
, VALUE
);
158 RARRAY(ary
)->len
= n
;
163 rb_ary_new4(long n
, const VALUE
*elts
)
167 ary
= rb_ary_new2(n
);
169 MEMCPY(RARRAY_PTR(ary
), elts
, VALUE
, n
);
170 RARRAY(ary
)->len
= n
;
177 rb_ary_free(VALUE ary
)
179 if (!ARY_SHARED_P(ary
)) {
180 xfree(RARRAY(ary
)->ptr
);
185 ary_make_shared(VALUE ary
)
187 if (ARY_SHARED_P(ary
)) {
188 return RARRAY(ary
)->aux
.shared
;
191 NEWOBJ(shared
, struct RArray
);
192 OBJSETUP(shared
, 0, T_ARRAY
);
194 shared
->len
= RARRAY(ary
)->len
;
195 shared
->ptr
= RARRAY(ary
)->ptr
;
196 shared
->aux
.capa
= RARRAY(ary
)->aux
.capa
;
197 RARRAY(ary
)->aux
.shared
= (VALUE
)shared
;
198 FL_SET(ary
, ELTS_SHARED
);
200 return (VALUE
)shared
;
205 rb_assoc_new(VALUE car
, VALUE cdr
)
207 return rb_ary_new3(2, car
, cdr
);
213 return rb_convert_type(ary
, T_ARRAY
, "Array", "to_ary");
217 rb_check_array_type(VALUE ary
)
219 return rb_check_convert_type(ary
, T_ARRAY
, "Array", "to_ary");
224 * Array.try_convert(obj) -> array or nil
226 * Try to convert <i>obj</i> into an array, using to_ary method.
227 * Returns converted array or nil if <i>obj</i> cannot be converted
228 * for any reason. This method is to check if an argument is an
231 * Array.try_convert([1]) # => [1]
232 * Array.try_convert("1") # => nil
234 * if tmp = Array.try_convert(arg)
235 * # the argument is an array
236 * elsif tmp = String.try_convert(arg)
237 * # the argument is a string
243 rb_ary_s_try_convert(VALUE dummy
, VALUE ary
)
245 return rb_check_array_type(ary
);
250 * Array.new(size=0, obj=nil)
252 * Array.new(size) {|index| block }
254 * Returns a new array. In the first form, the new array is
255 * empty. In the second it is created with _size_ copies of _obj_
256 * (that is, _size_ references to the same
257 * _obj_). The third form creates a copy of the array
258 * passed as a parameter (the array is generated by calling
259 * to_ary on the parameter). In the last form, an array
260 * of the given size is created. Each element in this array is
261 * calculated by passing the element's index to the given block and
262 * storing the return value.
268 * # only one copy of the object is created
269 * a = Array.new(2, Hash.new)
270 * a[0]['cat'] = 'feline'
272 * a[1]['cat'] = 'Felix'
275 * # here multiple copies are created
276 * a = Array.new(2) { Hash.new }
277 * a[0]['cat'] = 'feline'
280 * squares = Array.new(5) {|i| i*i}
283 * copy = Array.new(squares)
287 rb_ary_initialize(int argc
, VALUE
*argv
, VALUE ary
)
294 if (RARRAY_PTR(ary
) && !ARY_SHARED_P(ary
)) {
295 free(RARRAY(ary
)->ptr
);
297 RARRAY(ary
)->len
= 0;
298 if (rb_block_given_p()) {
299 rb_warning("given block not used");
303 rb_scan_args(argc
, argv
, "02", &size
, &val
);
304 if (argc
== 1 && !FIXNUM_P(size
)) {
305 val
= rb_check_array_type(size
);
307 rb_ary_replace(ary
, val
);
312 len
= NUM2LONG(size
);
314 rb_raise(rb_eArgError
, "negative array size");
316 if (len
> LONG_MAX
/ sizeof(VALUE
)) {
317 rb_raise(rb_eArgError
, "array size too big");
320 RESIZE_CAPA(ary
, len
);
321 if (rb_block_given_p()) {
325 rb_warn("block supersedes default value argument");
327 for (i
=0; i
<len
; i
++) {
328 rb_ary_store(ary
, i
, rb_yield(LONG2NUM(i
)));
329 RARRAY(ary
)->len
= i
+ 1;
333 memfill(RARRAY_PTR(ary
), len
, val
);
334 RARRAY(ary
)->len
= len
;
341 * Returns a new array populated with the given objects.
343 * Array.[]( 1, 'a', /^A/ )
344 * Array[ 1, 'a', /^A/ ]
349 rb_ary_s_create(int argc
, VALUE
*argv
, VALUE klass
)
351 VALUE ary
= ary_alloc(klass
);
354 rb_raise(rb_eArgError
, "negative array size");
356 RARRAY(ary
)->ptr
= ALLOC_N(VALUE
, argc
);
357 RARRAY(ary
)->aux
.capa
= argc
;
358 MEMCPY(RARRAY_PTR(ary
), argv
, VALUE
, argc
);
359 RARRAY(ary
)->len
= argc
;
365 rb_ary_store(VALUE ary
, long idx
, VALUE val
)
368 idx
+= RARRAY_LEN(ary
);
370 rb_raise(rb_eIndexError
, "index %ld out of array",
371 idx
- RARRAY_LEN(ary
));
376 if (idx
>= ARY_CAPA(ary
)) {
377 long new_capa
= ARY_CAPA(ary
) / 2;
379 if (new_capa
< ARY_DEFAULT_SIZE
) {
380 new_capa
= ARY_DEFAULT_SIZE
;
382 if (new_capa
+ idx
< new_capa
) {
383 rb_raise(rb_eArgError
, "index too big");
386 if (new_capa
* (long)sizeof(VALUE
) <= new_capa
) {
387 rb_raise(rb_eArgError
, "index too big");
389 RESIZE_CAPA(ary
, new_capa
);
391 if (idx
> RARRAY_LEN(ary
)) {
392 rb_mem_clear(RARRAY_PTR(ary
) + RARRAY_LEN(ary
),
393 idx
-RARRAY_LEN(ary
) + 1);
396 if (idx
>= RARRAY_LEN(ary
)) {
397 RARRAY(ary
)->len
= idx
+ 1;
399 RARRAY_PTR(ary
)[idx
] = val
;
403 ary_shared_array(VALUE klass
, VALUE ary
)
405 VALUE val
= ary_alloc(klass
);
407 ary_make_shared(ary
);
408 RARRAY(val
)->ptr
= RARRAY(ary
)->ptr
;
409 RARRAY(val
)->len
= RARRAY(ary
)->len
;
410 RARRAY(val
)->aux
.shared
= RARRAY(ary
)->aux
.shared
;
411 FL_SET(val
, ELTS_SHARED
);
416 ary_shared_first(int argc
, VALUE
*argv
, VALUE ary
, int last
)
422 rb_scan_args(argc
, argv
, "1", &nv
);
424 if (n
> RARRAY_LEN(ary
)) {
428 rb_raise(rb_eArgError
, "negative array size");
431 offset
= RARRAY_LEN(ary
) - n
;
433 result
= ary_shared_array(rb_cArray
, ary
);
434 RARRAY(result
)->ptr
+= offset
;
435 RARRAY(result
)->len
= n
;
442 * array << obj -> array
444 * Append---Pushes the given object on to the end of this array. This
445 * expression returns the array itself, so several appends
446 * may be chained together.
448 * [ 1, 2 ] << "c" << "d" << [ 3, 4 ]
449 * #=> [ 1, 2, "c", "d", [ 3, 4 ] ]
454 rb_ary_push(VALUE ary
, VALUE item
)
456 rb_ary_store(ary
, RARRAY_LEN(ary
), item
);
462 * array.push(obj, ... ) -> array
464 * Append---Pushes the given object(s) on to the end of this array. This
465 * expression returns the array itself, so several appends
466 * may be chained together.
468 * a = [ "a", "b", "c" ]
469 * a.push("d", "e", "f")
470 * #=> ["a", "b", "c", "d", "e", "f"]
474 rb_ary_push_m(int argc
, VALUE
*argv
, VALUE ary
)
477 rb_ary_push(ary
, *argv
++);
483 rb_ary_pop(VALUE ary
)
486 rb_ary_modify_check(ary
);
487 if (RARRAY_LEN(ary
) == 0) return Qnil
;
488 if (!ARY_SHARED_P(ary
) &&
489 RARRAY_LEN(ary
) * 3 < ARY_CAPA(ary
) &&
490 ARY_CAPA(ary
) > ARY_DEFAULT_SIZE
)
492 RESIZE_CAPA(ary
, RARRAY_LEN(ary
) * 2);
494 n
= RARRAY_LEN(ary
)-1;
495 RARRAY(ary
)->len
= n
;
496 return RARRAY_PTR(ary
)[n
];
501 * array.pop -> obj or nil
502 * array.pop(n) -> array
504 * Removes the last element from <i>self</i> and returns it, or
505 * <code>nil</code> if the array is empty.
507 * If a number _n_ is given, returns an array of the last n elements
508 * (or less) just like <code>array.slice!(-n, n)</code> does.
510 * a = [ "a", "b", "c", "d" ]
512 * a.pop(2) #=> ["b", "c"]
517 rb_ary_pop_m(int argc
, VALUE
*argv
, VALUE ary
)
522 return rb_ary_pop(ary
);
525 rb_ary_modify_check(ary
);
526 result
= ary_shared_first(argc
, argv
, ary
, Qtrue
);
527 RARRAY(ary
)->len
-= RARRAY_LEN(result
);
532 rb_ary_shift(VALUE ary
)
536 rb_ary_modify_check(ary
);
537 if (RARRAY_LEN(ary
) == 0) return Qnil
;
538 top
= RARRAY_PTR(ary
)[0];
539 if (!ARY_SHARED_P(ary
)) {
540 if (RARRAY_LEN(ary
) < ARY_DEFAULT_SIZE
) {
541 MEMMOVE(RARRAY_PTR(ary
), RARRAY_PTR(ary
)+1, VALUE
, RARRAY_LEN(ary
)-1);
545 RARRAY_PTR(ary
)[0] = Qnil
;
546 ary_make_shared(ary
);
548 RARRAY(ary
)->ptr
++; /* shift ptr */
556 * array.shift -> obj or nil
557 * array.shift(n) -> array
559 * Returns the first element of <i>self</i> and removes it (shifting all
560 * other elements down by one). Returns <code>nil</code> if the array
563 * If a number _n_ is given, returns an array of the first n elements
564 * (or less) just like <code>array.slice!(0, n)</code> does.
566 * args = [ "-m", "-q", "filename" ]
567 * args.shift #=> "-m"
568 * args #=> ["-q", "filename"]
570 * args = [ "-m", "-q", "filename" ]
571 * args.shift(2) #=> ["-m", "-q"]
572 * args #=> ["filename"]
576 rb_ary_shift_m(int argc
, VALUE
*argv
, VALUE ary
)
582 return rb_ary_shift(ary
);
585 rb_ary_modify_check(ary
);
586 result
= ary_shared_first(argc
, argv
, ary
, Qfalse
);
587 n
= RARRAY_LEN(result
);
588 if (ARY_SHARED_P(ary
)) {
589 RARRAY(ary
)->ptr
+= n
;
590 RARRAY(ary
)->len
-= n
;
593 MEMMOVE(RARRAY_PTR(ary
), RARRAY_PTR(ary
)+n
, VALUE
, RARRAY_LEN(ary
)-n
);
594 RARRAY(ary
)->len
-= n
;
602 * array.unshift(obj, ...) -> array
604 * Prepends objects to the front of <i>array</i>.
605 * other elements up one.
607 * a = [ "b", "c", "d" ]
608 * a.unshift("a") #=> ["a", "b", "c", "d"]
609 * a.unshift(1, 2) #=> [ 1, 2, "a", "b", "c", "d"]
613 rb_ary_unshift_m(int argc
, VALUE
*argv
, VALUE ary
)
617 if (argc
== 0) return ary
;
619 if (RARRAY(ary
)->aux
.capa
<= (len
= RARRAY(ary
)->len
) + argc
) {
620 RESIZE_CAPA(ary
, len
+ argc
+ ARY_DEFAULT_SIZE
);
624 MEMMOVE(RARRAY(ary
)->ptr
+ argc
, RARRAY(ary
)->ptr
, VALUE
, len
);
625 MEMCPY(RARRAY(ary
)->ptr
, argv
, VALUE
, argc
);
626 RARRAY(ary
)->len
+= argc
;
632 rb_ary_unshift(VALUE ary
, VALUE item
)
634 return rb_ary_unshift_m(1,&item
,ary
);
637 /* faster version - use this if you don't need to treat negative offset */
639 rb_ary_elt(VALUE ary
, long offset
)
641 if (RARRAY_LEN(ary
) == 0) return Qnil
;
642 if (offset
< 0 || RARRAY_LEN(ary
) <= offset
) {
645 return RARRAY_PTR(ary
)[offset
];
649 rb_ary_entry(VALUE ary
, long offset
)
652 offset
+= RARRAY_LEN(ary
);
654 return rb_ary_elt(ary
, offset
);
658 rb_ary_subseq(VALUE ary
, long beg
, long len
)
660 VALUE klass
, ary2
, shared
;
663 if (beg
> RARRAY_LEN(ary
)) return Qnil
;
664 if (beg
< 0 || len
< 0) return Qnil
;
666 if (RARRAY_LEN(ary
) < len
|| RARRAY_LEN(ary
) < beg
+ len
) {
667 len
= RARRAY_LEN(ary
) - beg
;
669 klass
= rb_obj_class(ary
);
670 if (len
== 0) return ary_new(klass
, 0);
672 shared
= ary_make_shared(ary
);
673 ptr
= RARRAY_PTR(ary
);
674 ary2
= ary_alloc(klass
);
675 RARRAY(ary2
)->ptr
= ptr
+ beg
;
676 RARRAY(ary2
)->len
= len
;
677 RARRAY(ary2
)->aux
.shared
= shared
;
678 FL_SET(ary2
, ELTS_SHARED
);
685 * array[index] -> obj or nil
686 * array[start, length] -> an_array or nil
687 * array[range] -> an_array or nil
688 * array.slice(index) -> obj or nil
689 * array.slice(start, length) -> an_array or nil
690 * array.slice(range) -> an_array or nil
692 * Element Reference---Returns the element at _index_,
693 * or returns a subarray starting at _start_ and
694 * continuing for _length_ elements, or returns a subarray
695 * specified by _range_.
696 * Negative indices count backward from the end of the
697 * array (-1 is the last element). Returns nil if the index
698 * (or starting index) are out of range.
700 * a = [ "a", "b", "c", "d", "e" ]
701 * a[2] + a[0] + a[1] #=> "cab"
703 * a[1, 2] #=> [ "b", "c" ]
704 * a[1..3] #=> [ "b", "c", "d" ]
705 * a[4..7] #=> [ "e" ]
707 * a[-3, 3] #=> [ "c", "d", "e" ]
716 rb_ary_aref(int argc
, VALUE
*argv
, VALUE ary
)
722 beg
= NUM2LONG(argv
[0]);
723 len
= NUM2LONG(argv
[1]);
725 beg
+= RARRAY_LEN(ary
);
727 return rb_ary_subseq(ary
, beg
, len
);
730 rb_scan_args(argc
, argv
, "11", 0, 0);
733 /* special case - speeding up */
735 return rb_ary_entry(ary
, FIX2LONG(arg
));
737 /* check if idx is Range */
738 switch (rb_range_beg_len(arg
, &beg
, &len
, RARRAY_LEN(ary
), 0)) {
744 return rb_ary_subseq(ary
, beg
, len
);
746 return rb_ary_entry(ary
, NUM2LONG(arg
));
751 * array.at(index) -> obj or nil
753 * Returns the element at _index_. A
754 * negative index counts from the end of _self_. Returns +nil+
755 * if the index is out of range. See also <code>Array#[]</code>.
757 * a = [ "a", "b", "c", "d", "e" ]
763 rb_ary_at(VALUE ary
, VALUE pos
)
765 return rb_ary_entry(ary
, NUM2LONG(pos
));
770 * array.first -> obj or nil
771 * array.first(n) -> an_array
773 * Returns the first element, or the first +n+ elements, of the array.
774 * If the array is empty, the first form returns <code>nil</code>, and the
775 * second form returns an empty array.
777 * a = [ "q", "r", "s", "t" ]
779 * a.first(2) #=> ["q", "r"]
783 rb_ary_first(int argc
, VALUE
*argv
, VALUE ary
)
786 if (RARRAY_LEN(ary
) == 0) return Qnil
;
787 return RARRAY_PTR(ary
)[0];
790 return ary_shared_first(argc
, argv
, ary
, Qfalse
);
796 * array.last -> obj or nil
797 * array.last(n) -> an_array
799 * Returns the last element(s) of <i>self</i>. If the array is empty,
800 * the first form returns <code>nil</code>.
802 * a = [ "w", "x", "y", "z" ]
804 * a.last(2) #=> ["y", "z"]
808 rb_ary_last(int argc
, VALUE
*argv
, VALUE ary
)
811 if (RARRAY_LEN(ary
) == 0) return Qnil
;
812 return RARRAY_PTR(ary
)[RARRAY_LEN(ary
)-1];
815 return ary_shared_first(argc
, argv
, ary
, Qtrue
);
821 * array.fetch(index) -> obj
822 * array.fetch(index, default ) -> obj
823 * array.fetch(index) {|index| block } -> obj
825 * Tries to return the element at position <i>index</i>. If the index
826 * lies outside the array, the first form throws an
827 * <code>IndexError</code> exception, the second form returns
828 * <i>default</i>, and the third form returns the value of invoking
829 * the block, passing in the index. Negative values of <i>index</i>
830 * count from the end of the array.
832 * a = [ 11, 22, 33, 44 ]
835 * a.fetch(4, 'cat') #=> "cat"
836 * a.fetch(4) { |i| i*i } #=> 16
840 rb_ary_fetch(int argc
, VALUE
*argv
, VALUE ary
)
846 rb_scan_args(argc
, argv
, "11", &pos
, &ifnone
);
847 block_given
= rb_block_given_p();
848 if (block_given
&& argc
== 2) {
849 rb_warn("block supersedes default value argument");
854 idx
+= RARRAY_LEN(ary
);
856 if (idx
< 0 || RARRAY_LEN(ary
) <= idx
) {
857 if (block_given
) return rb_yield(pos
);
859 rb_raise(rb_eIndexError
, "index %ld out of array", idx
);
863 return RARRAY_PTR(ary
)[idx
];
868 * array.index(obj) -> int or nil
869 * array.index {|item| block} -> int or nil
871 * Returns the index of the first object in <i>self</i> such that is
872 * <code>==</code> to <i>obj</i>. If a block is given instead of an
873 * argument, returns first object for which <em>block</em> is true.
874 * Returns <code>nil</code> if no match is found.
876 * a = [ "a", "b", "c" ]
878 * a.index("z") #=> nil
879 * a.index{|x|x=="b"} #=> 1
881 * This is an alias of <code>#find_index</code>.
885 rb_ary_index(int argc
, VALUE
*argv
, VALUE ary
)
891 RETURN_ENUMERATOR(ary
, 0, 0);
892 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
893 if (RTEST(rb_yield(RARRAY_PTR(ary
)[i
]))) {
899 rb_scan_args(argc
, argv
, "01", &val
);
900 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
901 if (rb_equal(RARRAY_PTR(ary
)[i
], val
))
909 * array.rindex(obj) -> int or nil
911 * Returns the index of the last object in <i>array</i>
912 * <code>==</code> to <i>obj</i>. If a block is given instead of an
913 * argument, returns first object for which <em>block</em> is
914 * true. Returns <code>nil</code> if no match is found.
916 * a = [ "a", "b", "b", "b", "c" ]
917 * a.rindex("b") #=> 3
918 * a.rindex("z") #=> nil
919 * a.rindex{|x|x=="b"} #=> 3
923 rb_ary_rindex(int argc
, VALUE
*argv
, VALUE ary
)
926 long i
= RARRAY_LEN(ary
);
929 RETURN_ENUMERATOR(ary
, 0, 0);
931 if (RTEST(rb_yield(RARRAY_PTR(ary
)[i
])))
933 if (i
> RARRAY_LEN(ary
)) {
939 rb_scan_args(argc
, argv
, "01", &val
);
941 if (rb_equal(RARRAY_PTR(ary
)[i
], val
))
943 if (i
> RARRAY_LEN(ary
)) {
951 rb_ary_to_ary(VALUE obj
)
953 if (TYPE(obj
) == T_ARRAY
) {
956 if (rb_respond_to(obj
, rb_intern("to_ary"))) {
959 return rb_ary_new3(1, obj
);
963 rb_ary_splice(VALUE ary
, long beg
, long len
, VALUE rpl
)
967 if (len
< 0) rb_raise(rb_eIndexError
, "negative length (%ld)", len
);
969 beg
+= RARRAY_LEN(ary
);
971 beg
-= RARRAY_LEN(ary
);
972 rb_raise(rb_eIndexError
, "index %ld out of array", beg
);
975 if (RARRAY_LEN(ary
) < len
|| RARRAY_LEN(ary
) < beg
+ len
) {
976 len
= RARRAY_LEN(ary
) - beg
;
983 rpl
= rb_ary_to_ary(rpl
);
984 rlen
= RARRAY_LEN(rpl
);
987 if (beg
>= RARRAY_LEN(ary
)) {
989 if (len
>= ARY_CAPA(ary
)) {
990 RESIZE_CAPA(ary
, len
);
992 rb_mem_clear(RARRAY_PTR(ary
) + RARRAY_LEN(ary
), beg
- RARRAY_LEN(ary
));
994 MEMCPY(RARRAY_PTR(ary
) + beg
, RARRAY_PTR(rpl
), VALUE
, rlen
);
996 RARRAY(ary
)->len
= len
;
1001 if (beg
+ len
> RARRAY_LEN(ary
)) {
1002 len
= RARRAY_LEN(ary
) - beg
;
1005 alen
= RARRAY_LEN(ary
) + rlen
- len
;
1006 if (alen
>= ARY_CAPA(ary
)) {
1007 RESIZE_CAPA(ary
, alen
);
1011 MEMMOVE(RARRAY_PTR(ary
) + beg
+ rlen
, RARRAY_PTR(ary
) + beg
+ len
,
1012 VALUE
, RARRAY_LEN(ary
) - (beg
+ len
));
1013 RARRAY(ary
)->len
= alen
;
1016 MEMMOVE(RARRAY_PTR(ary
) + beg
, RARRAY_PTR(rpl
), VALUE
, rlen
);
1023 * array[index] = obj -> obj
1024 * array[start, length] = obj or an_array or nil -> obj or an_array or nil
1025 * array[range] = obj or an_array or nil -> obj or an_array or nil
1027 * Element Assignment---Sets the element at _index_,
1028 * or replaces a subarray starting at _start_ and
1029 * continuing for _length_ elements, or replaces a subarray
1030 * specified by _range_. If indices are greater than
1031 * the current capacity of the array, the array grows
1032 * automatically. A negative indices will count backward
1033 * from the end of the array. Inserts elements if _length_ is
1034 * zero. An +IndexError+ is raised if a negative index points
1035 * past the beginning of the array. See also
1036 * <code>Array#push</code>, and <code>Array#unshift</code>.
1039 * a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
1040 * a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
1041 * a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, nil, "4"]
1042 * a[0, 2] = "?" #=> ["?", 2, nil, "4"]
1043 * a[0..2] = "A" #=> ["A", "4"]
1044 * a[-1] = "Z" #=> ["A", "Z"]
1045 * a[1..-1] = nil #=> ["A", nil]
1046 * a[1..-1] = [] #=> ["A"]
1050 rb_ary_aset(int argc
, VALUE
*argv
, VALUE ary
)
1052 long offset
, beg
, len
;
1055 rb_ary_splice(ary
, NUM2LONG(argv
[0]), NUM2LONG(argv
[1]), argv
[2]);
1059 rb_raise(rb_eArgError
, "wrong number of arguments (%d for 2)", argc
);
1061 if (FIXNUM_P(argv
[0])) {
1062 offset
= FIX2LONG(argv
[0]);
1065 if (rb_range_beg_len(argv
[0], &beg
, &len
, RARRAY_LEN(ary
), 1)) {
1066 /* check if idx is Range */
1067 rb_ary_splice(ary
, beg
, len
, argv
[1]);
1071 offset
= NUM2LONG(argv
[0]);
1073 rb_ary_store(ary
, offset
, argv
[1]);
1079 * array.insert(index, obj...) -> array
1081 * Inserts the given values before the element with the given index
1082 * (which may be negative).
1085 * a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
1086 * a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
1090 rb_ary_insert(int argc
, VALUE
*argv
, VALUE ary
)
1094 if (argc
== 1) return ary
;
1096 rb_raise(rb_eArgError
, "wrong number of arguments (at least 1)");
1098 pos
= NUM2LONG(argv
[0]);
1100 pos
= RARRAY_LEN(ary
);
1105 rb_ary_splice(ary
, pos
, 0, rb_ary_new4(argc
- 1, argv
+ 1));
1111 * array.each {|item| block } -> array
1113 * Calls <i>block</i> once for each element in <i>self</i>, passing that
1114 * element as a parameter.
1116 * a = [ "a", "b", "c" ]
1117 * a.each {|x| print x, " -- " }
1125 rb_ary_each(VALUE ary
)
1129 RETURN_ENUMERATOR(ary
, 0, 0);
1130 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
1131 rb_yield(RARRAY_PTR(ary
)[i
]);
1138 * array.each_index {|index| block } -> array
1140 * Same as <code>Array#each</code>, but passes the index of the element
1141 * instead of the element itself.
1143 * a = [ "a", "b", "c" ]
1144 * a.each_index {|x| print x, " -- " }
1152 rb_ary_each_index(VALUE ary
)
1155 RETURN_ENUMERATOR(ary
, 0, 0);
1157 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
1158 rb_yield(LONG2NUM(i
));
1165 * array.reverse_each {|item| block }
1167 * Same as <code>Array#each</code>, but traverses <i>self</i> in reverse
1170 * a = [ "a", "b", "c" ]
1171 * a.reverse_each {|x| print x, " " }
1179 rb_ary_reverse_each(VALUE ary
)
1183 RETURN_ENUMERATOR(ary
, 0, 0);
1184 len
= RARRAY_LEN(ary
);
1186 rb_yield(RARRAY_PTR(ary
)[len
]);
1187 if (RARRAY_LEN(ary
) < len
) {
1188 len
= RARRAY_LEN(ary
);
1196 * array.length -> int
1198 * Returns the number of elements in <i>self</i>. May be zero.
1200 * [ 1, 2, 3, 4, 5 ].length #=> 5
1204 rb_ary_length(VALUE ary
)
1206 long len
= RARRAY_LEN(ary
);
1207 return LONG2NUM(len
);
1212 * array.empty? -> true or false
1214 * Returns <code>true</code> if <i>self</i> array contains no elements.
1216 * [].empty? #=> true
1220 rb_ary_empty_p(VALUE ary
)
1222 if (RARRAY_LEN(ary
) == 0)
1228 rb_ary_dup(VALUE ary
)
1230 VALUE dup
= rb_ary_new2(RARRAY_LEN(ary
));
1232 MEMCPY(RARRAY_PTR(dup
), RARRAY_PTR(ary
), VALUE
, RARRAY_LEN(ary
));
1233 RARRAY(dup
)->len
= RARRAY_LEN(ary
);
1234 OBJ_INFECT(dup
, ary
);
1239 extern VALUE rb_output_fs
;
1242 recursive_join(VALUE ary
, VALUE argp
, int recur
)
1244 VALUE
*arg
= (VALUE
*)argp
;
1246 return rb_usascii_str_new2("[...]");
1248 return rb_ary_join(arg
[0], arg
[1]);
1252 rb_ary_join(VALUE ary
, VALUE sep
)
1258 if (RARRAY_LEN(ary
) == 0) return rb_str_new(0, 0);
1259 if (OBJ_TAINTED(ary
) || OBJ_TAINTED(sep
)) taint
= Qtrue
;
1261 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
1262 tmp
= rb_check_string_type(RARRAY_PTR(ary
)[i
]);
1263 len
+= NIL_P(tmp
) ? 10 : RSTRING_LEN(tmp
);
1267 len
+= RSTRING_LEN(sep
) * (RARRAY_LEN(ary
) - 1);
1269 result
= rb_str_buf_new(len
);
1270 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
1271 tmp
= RARRAY_PTR(ary
)[i
];
1272 switch (TYPE(tmp
)) {
1281 tmp
= rb_exec_recursive(recursive_join
, ary
, (VALUE
)args
);
1285 tmp
= rb_obj_as_string(tmp
);
1287 if (i
> 0 && !NIL_P(sep
))
1288 rb_str_buf_append(result
, sep
);
1289 rb_str_buf_append(result
, tmp
);
1290 if (OBJ_TAINTED(tmp
)) taint
= Qtrue
;
1293 if (taint
) OBJ_TAINT(result
);
1299 * array.join(sep=$,) -> str
1301 * Returns a string created by converting each element of the array to
1302 * a string, separated by <i>sep</i>.
1304 * [ "a", "b", "c" ].join #=> "abc"
1305 * [ "a", "b", "c" ].join("-") #=> "a-b-c"
1309 rb_ary_join_m(int argc
, VALUE
*argv
, VALUE ary
)
1313 rb_scan_args(argc
, argv
, "01", &sep
);
1314 if (NIL_P(sep
)) sep
= rb_output_fs
;
1316 return rb_ary_join(ary
, sep
);
1320 inspect_ary(VALUE ary
, VALUE dummy
, int recur
)
1322 int tainted
= OBJ_TAINTED(ary
);
1326 if (recur
) return rb_tainted_str_new2("[...]");
1327 str
= rb_str_buf_new2("[");
1328 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
1329 s
= rb_inspect(RARRAY_PTR(ary
)[i
]);
1330 if (OBJ_TAINTED(s
)) tainted
= Qtrue
;
1331 if (i
> 0) rb_str_buf_cat2(str
, ", ");
1332 rb_str_buf_append(str
, s
);
1334 rb_str_buf_cat2(str
, "]");
1335 if (tainted
) OBJ_TAINT(str
);
1341 * array.to_s -> string
1342 * array.inspect -> string
1344 * Create a printable version of <i>array</i>.
1348 rb_ary_inspect(VALUE ary
)
1350 if (RARRAY_LEN(ary
) == 0) return rb_usascii_str_new2("[]");
1351 return rb_exec_recursive(inspect_ary
, ary
, 0);
1355 rb_ary_to_s(VALUE ary
)
1357 return rb_ary_inspect(ary
);
1362 * array.to_a -> array
1364 * Returns _self_. If called on a subclass of Array, converts
1365 * the receiver to an Array object.
1369 rb_ary_to_a(VALUE ary
)
1371 if (rb_obj_class(ary
) != rb_cArray
) {
1372 VALUE dup
= rb_ary_new2(RARRAY_LEN(ary
));
1373 rb_ary_replace(dup
, ary
);
1381 * array.to_ary -> array
1387 rb_ary_to_ary_m(VALUE ary
)
1393 rb_ary_reverse(VALUE ary
)
1399 if (RARRAY_LEN(ary
) > 1) {
1400 p1
= RARRAY_PTR(ary
);
1401 p2
= p1
+ RARRAY_LEN(ary
) - 1; /* points last item */
1414 * array.reverse! -> array
1416 * Reverses _self_ in place.
1418 * a = [ "a", "b", "c" ]
1419 * a.reverse! #=> ["c", "b", "a"]
1420 * a #=> ["c", "b", "a"]
1424 rb_ary_reverse_bang(VALUE ary
)
1426 return rb_ary_reverse(ary
);
1431 * array.reverse -> an_array
1433 * Returns a new array containing <i>self</i>'s elements in reverse order.
1435 * [ "a", "b", "c" ].reverse #=> ["c", "b", "a"]
1436 * [ 1 ].reverse #=> [1]
1440 rb_ary_reverse_m(VALUE ary
)
1442 return rb_ary_reverse(rb_ary_dup(ary
));
1446 sort_1(const void *ap
, const void *bp
, void *dummy
)
1448 VALUE a
= *(const VALUE
*)ap
, b
= *(const VALUE
*)bp
;
1449 VALUE retval
= rb_yield_values(2, a
, b
);
1452 n
= rb_cmpint(retval
, a
, b
);
1457 sort_2(const void *ap
, const void *bp
, void *dummy
)
1460 VALUE a
= *(const VALUE
*)ap
, b
= *(const VALUE
*)bp
;
1463 if (FIXNUM_P(a
) && FIXNUM_P(b
)) {
1464 if ((long)a
> (long)b
) return 1;
1465 if ((long)a
< (long)b
) return -1;
1468 if (TYPE(a
) == T_STRING
) {
1469 if (TYPE(b
) == T_STRING
) return rb_str_cmp(a
, b
);
1472 retval
= rb_funcall(a
, id_cmp
, 1, b
);
1473 n
= rb_cmpint(retval
, a
, b
);
1480 * array.sort! -> array
1481 * array.sort! {| a,b | block } -> array
1483 * Sorts _self_. Comparisons for
1484 * the sort will be done using the <code><=></code> operator or using
1485 * an optional code block. The block implements a comparison between
1486 * <i>a</i> and <i>b</i>, returning -1, 0, or +1. See also
1487 * <code>Enumerable#sort_by</code>.
1489 * a = [ "d", "a", "e", "c", "b" ]
1490 * a.sort #=> ["a", "b", "c", "d", "e"]
1491 * a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
1495 rb_ary_sort_bang(VALUE ary
)
1498 if (RARRAY_LEN(ary
) > 1) {
1499 VALUE tmp
= ary_make_shared(ary
);
1501 RBASIC(tmp
)->klass
= 0;
1502 ruby_qsort(RARRAY_PTR(tmp
), RARRAY_LEN(tmp
), sizeof(VALUE
),
1503 rb_block_given_p()?sort_1
:sort_2
, 0);
1504 RARRAY(ary
)->ptr
= RARRAY(tmp
)->ptr
;
1505 RARRAY(ary
)->len
= RARRAY(tmp
)->len
;
1506 RARRAY(ary
)->aux
.capa
= RARRAY(tmp
)->aux
.capa
;
1507 FL_UNSET(ary
, ELTS_SHARED
);
1508 rb_gc_force_recycle(tmp
);
1515 * array.sort -> an_array
1516 * array.sort {| a,b | block } -> an_array
1518 * Returns a new array created by sorting <i>self</i>. Comparisons for
1519 * the sort will be done using the <code><=></code> operator or using
1520 * an optional code block. The block implements a comparison between
1521 * <i>a</i> and <i>b</i>, returning -1, 0, or +1. See also
1522 * <code>Enumerable#sort_by</code>.
1524 * a = [ "d", "a", "e", "c", "b" ]
1525 * a.sort #=> ["a", "b", "c", "d", "e"]
1526 * a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
1530 rb_ary_sort(VALUE ary
)
1532 ary
= rb_ary_dup(ary
);
1533 rb_ary_sort_bang(ary
);
1540 * array.collect {|item| block } -> an_array
1541 * array.map {|item| block } -> an_array
1543 * Invokes <i>block</i> once for each element of <i>self</i>. Creates a
1544 * new array containing the values returned by the block.
1545 * See also <code>Enumerable#collect</code>.
1547 * a = [ "a", "b", "c", "d" ]
1548 * a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
1549 * a #=> ["a", "b", "c", "d"]
1553 rb_ary_collect(VALUE ary
)
1558 RETURN_ENUMERATOR(ary
, 0, 0);
1559 collect
= rb_ary_new2(RARRAY_LEN(ary
));
1560 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
1561 rb_ary_push(collect
, rb_yield(RARRAY_PTR(ary
)[i
]));
1569 * array.collect! {|item| block } -> array
1570 * array.map! {|item| block } -> array
1572 * Invokes the block once for each element of _self_, replacing the
1573 * element with the value returned by _block_.
1574 * See also <code>Enumerable#collect</code>.
1576 * a = [ "a", "b", "c", "d" ]
1577 * a.collect! {|x| x + "!" }
1578 * a #=> [ "a!", "b!", "c!", "d!" ]
1582 rb_ary_collect_bang(VALUE ary
)
1586 RETURN_ENUMERATOR(ary
, 0, 0);
1588 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
1589 rb_ary_store(ary
, i
, rb_yield(RARRAY_PTR(ary
)[i
]));
1595 rb_get_values_at(VALUE obj
, long olen
, int argc
, VALUE
*argv
, VALUE (*func
) (VALUE
, long))
1597 VALUE result
= rb_ary_new2(argc
);
1598 long beg
, len
, i
, j
;
1600 for (i
=0; i
<argc
; i
++) {
1601 if (FIXNUM_P(argv
[i
])) {
1602 rb_ary_push(result
, (*func
)(obj
, FIX2LONG(argv
[i
])));
1605 /* check if idx is Range */
1606 switch (rb_range_beg_len(argv
[i
], &beg
, &len
, olen
, 0)) {
1612 for (j
=0; j
<len
; j
++) {
1613 rb_ary_push(result
, (*func
)(obj
, j
+beg
));
1617 rb_ary_push(result
, (*func
)(obj
, NUM2LONG(argv
[i
])));
1624 * array.values_at(selector,... ) -> an_array
1626 * Returns an array containing the elements in
1627 * _self_ corresponding to the given selector(s). The selectors
1628 * may be either integer indices or ranges.
1629 * See also <code>Array#select</code>.
1631 * a = %w{ a b c d e f }
1632 * a.values_at(1, 3, 5)
1633 * a.values_at(1, 3, 5, 7)
1634 * a.values_at(-1, -3, -5, -7)
1635 * a.values_at(1..3, 2...5)
1639 rb_ary_values_at(int argc
, VALUE
*argv
, VALUE ary
)
1641 return rb_get_values_at(ary
, RARRAY_LEN(ary
), argc
, argv
, rb_ary_entry
);
1647 * array.select {|item| block } -> an_array
1649 * Invokes the block passing in successive elements from <i>array</i>,
1650 * returning an array containing those elements for which the block
1651 * returns a true value (equivalent to <code>Enumerable#select</code>).
1653 * a = %w{ a b c d e f }
1654 * a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"]
1658 rb_ary_select(VALUE ary
)
1663 RETURN_ENUMERATOR(ary
, 0, 0);
1664 result
= rb_ary_new2(RARRAY_LEN(ary
));
1665 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
1666 if (RTEST(rb_yield(RARRAY_PTR(ary
)[i
]))) {
1667 rb_ary_push(result
, rb_ary_elt(ary
, i
));
1675 * array.delete(obj) -> obj or nil
1676 * array.delete(obj) { block } -> obj or nil
1678 * Deletes items from <i>self</i> that are equal to <i>obj</i>. If
1679 * the item is not found, returns <code>nil</code>. If the optional
1680 * code block is given, returns the result of <i>block</i> if the item
1683 * a = [ "a", "b", "b", "b", "c" ]
1684 * a.delete("b") #=> "b"
1686 * a.delete("z") #=> nil
1687 * a.delete("z") { "not found" } #=> "not found"
1691 rb_ary_delete(VALUE ary
, VALUE item
)
1695 for (i1
= i2
= 0; i1
< RARRAY_LEN(ary
); i1
++) {
1696 VALUE e
= RARRAY_PTR(ary
)[i1
];
1698 if (rb_equal(e
, item
)) continue;
1700 rb_ary_store(ary
, i2
, e
);
1704 if (RARRAY_LEN(ary
) == i2
) {
1705 if (rb_block_given_p()) {
1706 return rb_yield(item
);
1712 if (RARRAY_LEN(ary
) > i2
) {
1713 RARRAY(ary
)->len
= i2
;
1714 if (i2
* 2 < ARY_CAPA(ary
) &&
1715 ARY_CAPA(ary
) > ARY_DEFAULT_SIZE
) {
1716 RESIZE_CAPA(ary
, i2
*2);
1724 rb_ary_delete_at(VALUE ary
, long pos
)
1726 long len
= RARRAY_LEN(ary
);
1729 if (pos
>= len
) return Qnil
;
1732 if (pos
< 0) return Qnil
;
1736 del
= RARRAY_PTR(ary
)[pos
];
1737 MEMMOVE(RARRAY_PTR(ary
)+pos
, RARRAY_PTR(ary
)+pos
+1, VALUE
,
1738 RARRAY_LEN(ary
)-pos
-1);
1746 * array.delete_at(index) -> obj or nil
1748 * Deletes the element at the specified index, returning that element,
1749 * or <code>nil</code> if the index is out of range. See also
1750 * <code>Array#slice!</code>.
1752 * a = %w( ant bat cat dog )
1753 * a.delete_at(2) #=> "cat"
1754 * a #=> ["ant", "bat", "dog"]
1755 * a.delete_at(99) #=> nil
1759 rb_ary_delete_at_m(VALUE ary
, VALUE pos
)
1761 return rb_ary_delete_at(ary
, NUM2LONG(pos
));
1766 * array.slice!(index) -> obj or nil
1767 * array.slice!(start, length) -> sub_array or nil
1768 * array.slice!(range) -> sub_array or nil
1770 * Deletes the element(s) given by an index (optionally with a length)
1771 * or by a range. Returns the deleted object, subarray, or
1772 * <code>nil</code> if the index is out of range.
1774 * a = [ "a", "b", "c" ]
1775 * a.slice!(1) #=> "b"
1777 * a.slice!(-1) #=> "c"
1779 * a.slice!(100) #=> nil
1784 rb_ary_slice_bang(int argc
, VALUE
*argv
, VALUE ary
)
1789 if (rb_scan_args(argc
, argv
, "11", &arg1
, &arg2
) == 2) {
1790 pos
= NUM2LONG(arg1
);
1791 len
= NUM2LONG(arg2
);
1794 pos
= RARRAY_LEN(ary
) + pos
;
1795 if (pos
< 0) return Qnil
;
1797 arg2
= rb_ary_new4(len
, RARRAY_PTR(ary
)+pos
);
1798 RBASIC(arg2
)->klass
= rb_obj_class(ary
);
1799 rb_ary_splice(ary
, pos
, len
, Qundef
); /* Qnil/rb_ary_new2(0) */
1803 if (!FIXNUM_P(arg1
)) {
1804 switch (rb_range_beg_len(arg1
, &pos
, &len
, RARRAY_LEN(ary
), 0)) {
1807 goto delete_pos_len
;
1817 return rb_ary_delete_at(ary
, NUM2LONG(arg1
));
1822 * array.reject! {|item| block } -> array or nil
1824 * Equivalent to <code>Array#delete_if</code>, deleting elements from
1825 * _self_ for which the block evaluates to true, but returns
1826 * <code>nil</code> if no changes were made. Also see
1827 * <code>Enumerable#reject</code>.
1831 rb_ary_reject_bang(VALUE ary
)
1835 RETURN_ENUMERATOR(ary
, 0, 0);
1837 for (i1
= i2
= 0; i1
< RARRAY_LEN(ary
); i1
++) {
1838 VALUE v
= RARRAY_PTR(ary
)[i1
];
1839 if (RTEST(rb_yield(v
))) continue;
1841 rb_ary_store(ary
, i2
, v
);
1846 if (RARRAY_LEN(ary
) == i2
) return Qnil
;
1847 if (i2
< RARRAY_LEN(ary
))
1848 RARRAY(ary
)->len
= i2
;
1854 * array.reject {|item| block } -> an_array
1856 * Returns a new array containing the items in _self_
1857 * for which the block is not true.
1861 rb_ary_reject(VALUE ary
)
1863 RETURN_ENUMERATOR(ary
, 0, 0);
1864 ary
= rb_ary_dup(ary
);
1865 rb_ary_reject_bang(ary
);
1871 * array.delete_if {|item| block } -> array
1873 * Deletes every element of <i>self</i> for which <i>block</i> evaluates
1874 * to <code>true</code>.
1876 * a = [ "a", "b", "c" ]
1877 * a.delete_if {|x| x >= "b" } #=> ["a"]
1881 rb_ary_delete_if(VALUE ary
)
1883 rb_ary_reject_bang(ary
);
1888 take_i(VALUE val
, VALUE
*args
, int argc
, VALUE
*argv
)
1890 if (args
[1]-- == 0) rb_iter_break();
1891 if (argc
> 1) val
= rb_ary_new4(argc
, argv
);
1892 rb_ary_push(args
[0], val
);
1897 take_items(VALUE obj
, long n
)
1899 VALUE result
= rb_ary_new2(n
);
1902 args
[0] = result
; args
[1] = (VALUE
)n
;
1903 rb_block_call(obj
, rb_intern("each"), 0, 0, take_i
, (VALUE
)args
);
1910 * array.zip(arg, ...) -> an_array
1911 * array.zip(arg, ...) {| arr | block } -> nil
1913 * Converts any arguments to arrays, then merges elements of
1914 * <i>self</i> with corresponding elements from each argument. This
1915 * generates a sequence of <code>self.size</code> <em>n</em>-element
1916 * arrays, where <em>n</em> is one more that the count of arguments. If
1917 * the size of any argument is less than <code>enumObj.size</code>,
1918 * <code>nil</code> values are supplied. If a block given, it is
1919 * invoked for each output array, otherwise an array of arrays is
1924 * [1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
1925 * [1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]]
1926 * a.zip([1,2],[8]) #=> [[4,1,8], [5,2,nil], [6,nil,nil]]
1930 rb_ary_zip(argc
, argv
, ary
)
1937 VALUE result
= Qnil
;
1939 len
= RARRAY_LEN(ary
);
1940 for (i
=0; i
<argc
; i
++) {
1941 argv
[i
] = take_items(argv
[i
], len
);
1943 if (!rb_block_given_p()) {
1944 result
= rb_ary_new2(len
);
1947 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
1948 VALUE tmp
= rb_ary_new2(argc
+1);
1950 rb_ary_push(tmp
, rb_ary_elt(ary
, i
));
1951 for (j
=0; j
<argc
; j
++) {
1952 rb_ary_push(tmp
, rb_ary_elt(argv
[j
], i
));
1954 if (NIL_P(result
)) {
1958 rb_ary_push(result
, tmp
);
1966 * array.transpose -> an_array
1968 * Assumes that <i>self</i> is an array of arrays and transposes the
1971 * a = [[1,2], [3,4], [5,6]]
1972 * a.transpose #=> [[1, 3, 5], [2, 4, 6]]
1976 rb_ary_transpose(VALUE ary
)
1978 long elen
= -1, alen
, i
, j
;
1979 VALUE tmp
, result
= 0;
1981 alen
= RARRAY_LEN(ary
);
1982 if (alen
== 0) return rb_ary_dup(ary
);
1983 for (i
=0; i
<alen
; i
++) {
1984 tmp
= to_ary(rb_ary_elt(ary
, i
));
1985 if (elen
< 0) { /* first element */
1986 elen
= RARRAY_LEN(tmp
);
1987 result
= rb_ary_new2(elen
);
1988 for (j
=0; j
<elen
; j
++) {
1989 rb_ary_store(result
, j
, rb_ary_new2(alen
));
1992 else if (elen
!= RARRAY_LEN(tmp
)) {
1993 rb_raise(rb_eIndexError
, "element size differs (%ld should be %ld)",
1994 RARRAY_LEN(tmp
), elen
);
1996 for (j
=0; j
<elen
; j
++) {
1997 rb_ary_store(rb_ary_elt(result
, j
), i
, rb_ary_elt(tmp
, j
));
2005 * array.replace(other_array) -> array
2007 * Replaces the contents of <i>self</i> with the contents of
2008 * <i>other_array</i>, truncating or expanding if necessary.
2010 * a = [ "a", "b", "c", "d", "e" ]
2011 * a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
2012 * a #=> ["x", "y", "z"]
2016 rb_ary_replace(VALUE copy
, VALUE orig
)
2021 orig
= to_ary(orig
);
2022 rb_ary_modify_check(copy
);
2023 if (copy
== orig
) return copy
;
2024 shared
= ary_make_shared(orig
);
2025 if (!ARY_SHARED_P(copy
)) {
2026 ptr
= RARRAY(copy
)->ptr
;
2029 RARRAY(copy
)->ptr
= RARRAY(orig
)->ptr
;
2030 RARRAY(copy
)->len
= RARRAY(orig
)->len
;
2031 RARRAY(copy
)->aux
.shared
= shared
;
2032 FL_SET(copy
, ELTS_SHARED
);
2039 * array.clear -> array
2041 * Removes all elements from _self_.
2043 * a = [ "a", "b", "c", "d", "e" ]
2048 rb_ary_clear(VALUE ary
)
2051 RARRAY(ary
)->len
= 0;
2052 if (ARY_DEFAULT_SIZE
* 2 < ARY_CAPA(ary
)) {
2053 RESIZE_CAPA(ary
, ARY_DEFAULT_SIZE
* 2);
2060 * array.fill(obj) -> array
2061 * array.fill(obj, start [, length]) -> array
2062 * array.fill(obj, range ) -> array
2063 * array.fill {|index| block } -> array
2064 * array.fill(start [, length] ) {|index| block } -> array
2065 * array.fill(range) {|index| block } -> array
2067 * The first three forms set the selected elements of <i>self</i> (which
2068 * may be the entire array) to <i>obj</i>. A <i>start</i> of
2069 * <code>nil</code> is equivalent to zero. A <i>length</i> of
2070 * <code>nil</code> is equivalent to <i>self.length</i>. The last three
2071 * forms fill the array with the value of the block. The block is
2072 * passed the absolute index of each element to be filled.
2074 * a = [ "a", "b", "c", "d" ]
2075 * a.fill("x") #=> ["x", "x", "x", "x"]
2076 * a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
2077 * a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
2078 * a.fill {|i| i*i} #=> [0, 1, 4, 9]
2079 * a.fill(-2) {|i| i*i*i} #=> [0, 1, 8, 27]
2083 rb_ary_fill(int argc
, VALUE
*argv
, VALUE ary
)
2085 VALUE item
, arg1
, arg2
;
2086 long beg
= 0, end
= 0, len
= 0;
2088 int block_p
= Qfalse
;
2090 if (rb_block_given_p()) {
2092 rb_scan_args(argc
, argv
, "02", &arg1
, &arg2
);
2093 argc
+= 1; /* hackish */
2096 rb_scan_args(argc
, argv
, "12", &item
, &arg1
, &arg2
);
2101 len
= RARRAY_LEN(ary
);
2104 if (rb_range_beg_len(arg1
, &beg
, &len
, RARRAY_LEN(ary
), 1)) {
2109 beg
= NIL_P(arg1
) ? 0 : NUM2LONG(arg1
);
2111 beg
= RARRAY_LEN(ary
) + beg
;
2112 if (beg
< 0) beg
= 0;
2114 len
= NIL_P(arg2
) ? RARRAY_LEN(ary
) - beg
: NUM2LONG(arg2
);
2115 if (len
< 0) rb_raise(rb_eIndexError
, "negative length (%ld)", len
);
2121 rb_raise(rb_eArgError
, "argument too big");
2123 if (RARRAY_LEN(ary
) < end
) {
2124 if (end
>= ARY_CAPA(ary
)) {
2125 RESIZE_CAPA(ary
, end
);
2127 rb_mem_clear(RARRAY_PTR(ary
) + RARRAY_LEN(ary
), end
- RARRAY_LEN(ary
));
2128 RARRAY(ary
)->len
= end
;
2135 for (i
=beg
; i
<end
; i
++) {
2136 v
= rb_yield(LONG2NUM(i
));
2137 if (i
>=RARRAY_LEN(ary
)) break;
2138 RARRAY_PTR(ary
)[i
] = v
;
2142 p
= RARRAY_PTR(ary
) + beg
;
2153 * array + other_array -> an_array
2155 * Concatenation---Returns a new array built by concatenating the
2156 * two arrays together to produce a third array.
2158 * [ 1, 2, 3 ] + [ 4, 5 ] #=> [ 1, 2, 3, 4, 5 ]
2162 rb_ary_plus(VALUE x
, VALUE y
)
2168 len
= RARRAY_LEN(x
) + RARRAY_LEN(y
);
2169 z
= rb_ary_new2(len
);
2170 MEMCPY(RARRAY_PTR(z
), RARRAY_PTR(x
), VALUE
, RARRAY_LEN(x
));
2171 MEMCPY(RARRAY_PTR(z
) + RARRAY_LEN(x
), RARRAY_PTR(y
), VALUE
, RARRAY_LEN(y
));
2172 RARRAY(z
)->len
= len
;
2178 * array.concat(other_array) -> array
2180 * Appends the elements in other_array to _self_.
2182 * [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
2187 rb_ary_concat(VALUE x
, VALUE y
)
2190 if (RARRAY_LEN(y
) > 0) {
2191 rb_ary_splice(x
, RARRAY_LEN(x
), 0, y
);
2199 * array * int -> an_array
2200 * array * str -> a_string
2202 * Repetition---With a String argument, equivalent to
2203 * self.join(str). Otherwise, returns a new array
2204 * built by concatenating the _int_ copies of _self_.
2207 * [ 1, 2, 3 ] * 3 #=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
2208 * [ 1, 2, 3 ] * "," #=> "1,2,3"
2213 rb_ary_times(VALUE ary
, VALUE times
)
2218 tmp
= rb_check_string_type(times
);
2220 return rb_ary_join(ary
, tmp
);
2223 len
= NUM2LONG(times
);
2224 if (len
== 0) return ary_new(rb_obj_class(ary
), 0);
2226 rb_raise(rb_eArgError
, "negative argument");
2228 if (LONG_MAX
/len
< RARRAY_LEN(ary
)) {
2229 rb_raise(rb_eArgError
, "argument too big");
2231 len
*= RARRAY_LEN(ary
);
2233 ary2
= ary_new(rb_obj_class(ary
), len
);
2234 RARRAY(ary2
)->len
= len
;
2236 for (i
=0; i
<len
; i
+=RARRAY_LEN(ary
)) {
2237 MEMCPY(RARRAY_PTR(ary2
)+i
, RARRAY_PTR(ary
), VALUE
, RARRAY_LEN(ary
));
2239 OBJ_INFECT(ary2
, ary
);
2246 * array.assoc(obj) -> an_array or nil
2248 * Searches through an array whose elements are also arrays
2249 * comparing _obj_ with the first element of each contained array
2251 * Returns the first contained array that matches (that
2252 * is, the first associated array),
2253 * or +nil+ if no match is found.
2254 * See also <code>Array#rassoc</code>.
2256 * s1 = [ "colors", "red", "blue", "green" ]
2257 * s2 = [ "letters", "a", "b", "c" ]
2259 * a = [ s1, s2, s3 ]
2260 * a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
2261 * a.assoc("foo") #=> nil
2265 rb_ary_assoc(VALUE ary
, VALUE key
)
2270 for (i
= 0; i
< RARRAY_LEN(ary
); ++i
) {
2271 v
= rb_check_array_type(RARRAY_PTR(ary
)[i
]);
2272 if (!NIL_P(v
) && RARRAY_LEN(v
) > 0 &&
2273 rb_equal(RARRAY_PTR(v
)[0], key
))
2281 * array.rassoc(obj) -> an_array or nil
2283 * Searches through the array whose elements are also arrays. Compares
2284 * _obj_ with the second element of each contained array using
2285 * <code>==</code>. Returns the first contained array that matches. See
2286 * also <code>Array#assoc</code>.
2288 * a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
2289 * a.rassoc("two") #=> [2, "two"]
2290 * a.rassoc("four") #=> nil
2294 rb_ary_rassoc(VALUE ary
, VALUE value
)
2299 for (i
= 0; i
< RARRAY_LEN(ary
); ++i
) {
2300 v
= RARRAY_PTR(ary
)[i
];
2301 if (TYPE(v
) == T_ARRAY
&&
2302 RARRAY_LEN(v
) > 1 &&
2303 rb_equal(RARRAY_PTR(v
)[1], value
))
2310 recursive_equal(VALUE ary1
, VALUE ary2
, int recur
)
2314 if (recur
) return Qfalse
;
2315 for (i
=0; i
<RARRAY_LEN(ary1
); i
++) {
2316 if (!rb_equal(rb_ary_elt(ary1
, i
), rb_ary_elt(ary2
, i
)))
2324 * array == other_array -> bool
2326 * Equality---Two arrays are equal if they contain the same number
2327 * of elements and if each element is equal to (according to
2328 * Object.==) the corresponding element in the other array.
2330 * [ "a", "c" ] == [ "a", "c", 7 ] #=> false
2331 * [ "a", "c", 7 ] == [ "a", "c", 7 ] #=> true
2332 * [ "a", "c", 7 ] == [ "a", "d", "f" ] #=> false
2337 rb_ary_equal(VALUE ary1
, VALUE ary2
)
2339 if (ary1
== ary2
) return Qtrue
;
2340 if (TYPE(ary2
) != T_ARRAY
) {
2341 if (!rb_respond_to(ary2
, rb_intern("to_ary"))) {
2344 return rb_equal(ary2
, ary1
);
2346 if (RARRAY_LEN(ary1
) != RARRAY_LEN(ary2
)) return Qfalse
;
2347 return rb_exec_recursive(recursive_equal
, ary1
, ary2
);
2351 recursive_eql(VALUE ary1
, VALUE ary2
, int recur
)
2355 if (recur
) return Qfalse
;
2356 for (i
=0; i
<RARRAY_LEN(ary1
); i
++) {
2357 if (!rb_eql(rb_ary_elt(ary1
, i
), rb_ary_elt(ary2
, i
)))
2365 * array.eql?(other) -> true or false
2367 * Returns <code>true</code> if _array_ and _other_ are the same object,
2368 * or are both arrays with the same content.
2372 rb_ary_eql(VALUE ary1
, VALUE ary2
)
2374 if (ary1
== ary2
) return Qtrue
;
2375 if (TYPE(ary2
) != T_ARRAY
) return Qfalse
;
2376 if (RARRAY_LEN(ary1
) != RARRAY_LEN(ary2
)) return Qfalse
;
2377 return rb_exec_recursive(recursive_eql
, ary1
, ary2
);
2381 recursive_hash(VALUE ary
, VALUE dummy
, int recur
)
2389 h
= RARRAY_LEN(ary
);
2390 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
2391 h
= (h
<< 1) | (h
<0 ? 1 : 0);
2392 n
= rb_hash(RARRAY_PTR(ary
)[i
]);
2400 * array.hash -> fixnum
2402 * Compute a hash-code for this array. Two arrays with the same content
2403 * will have the same hash code (and will compare using <code>eql?</code>).
2407 rb_ary_hash(VALUE ary
)
2409 return rb_exec_recursive(recursive_hash
, ary
, 0);
2414 * array.include?(obj) -> true or false
2416 * Returns <code>true</code> if the given object is present in
2417 * <i>self</i> (that is, if any object <code>==</code> <i>anObject</i>),
2418 * <code>false</code> otherwise.
2420 * a = [ "a", "b", "c" ]
2421 * a.include?("b") #=> true
2422 * a.include?("z") #=> false
2426 rb_ary_includes(VALUE ary
, VALUE item
)
2430 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
2431 if (rb_equal(RARRAY_PTR(ary
)[i
], item
)) {
2440 recursive_cmp(VALUE ary1
, VALUE ary2
, int recur
)
2444 if (recur
) return Qnil
;
2445 len
= RARRAY_LEN(ary1
);
2446 if (len
> RARRAY_LEN(ary2
)) {
2447 len
= RARRAY_LEN(ary2
);
2449 for (i
=0; i
<len
; i
++) {
2450 VALUE v
= rb_funcall(rb_ary_elt(ary1
, i
), id_cmp
, 1, rb_ary_elt(ary2
, i
));
2451 if (v
!= INT2FIX(0)) {
2460 * array <=> other_array -> -1, 0, +1
2462 * Comparison---Returns an integer (-1, 0,
2463 * or +1) if this array is less than, equal to, or greater than
2464 * other_array. Each object in each array is compared
2465 * (using <=>). If any value isn't
2466 * equal, then that inequality is the return value. If all the
2467 * values found are equal, then the return is based on a
2468 * comparison of the array lengths. Thus, two arrays are
2469 * ``equal'' according to <code>Array#<=></code> if and only if they have
2470 * the same length and the value of each element is equal to the
2471 * value of the corresponding element in the other array.
2473 * [ "a", "a", "c" ] <=> [ "a", "b", "c" ] #=> -1
2474 * [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1
2479 rb_ary_cmp(VALUE ary1
, VALUE ary2
)
2484 ary2
= to_ary(ary2
);
2485 if (ary1
== ary2
) return INT2FIX(0);
2486 v
= rb_exec_recursive(recursive_cmp
, ary1
, ary2
);
2487 if (v
!= Qundef
) return v
;
2488 len
= RARRAY_LEN(ary1
) - RARRAY_LEN(ary2
);
2489 if (len
== 0) return INT2FIX(0);
2490 if (len
> 0) return INT2FIX(1);
2495 ary_make_hash(VALUE ary1
, VALUE ary2
)
2497 VALUE hash
= rb_hash_new();
2500 for (i
=0; i
<RARRAY_LEN(ary1
); i
++) {
2501 rb_hash_aset(hash
, RARRAY_PTR(ary1
)[i
], Qtrue
);
2504 for (i
=0; i
<RARRAY_LEN(ary2
); i
++) {
2505 rb_hash_aset(hash
, RARRAY_PTR(ary2
)[i
], Qtrue
);
2513 * array - other_array -> an_array
2515 * Array Difference---Returns a new array that is a copy of
2516 * the original array, removing any items that also appear in
2517 * other_array. (If you need set-like behavior, see the
2518 * library class Set.)
2520 * [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
2524 rb_ary_diff(VALUE ary1
, VALUE ary2
)
2527 volatile VALUE hash
;
2530 hash
= ary_make_hash(to_ary(ary2
), 0);
2531 ary3
= rb_ary_new();
2533 for (i
=0; i
<RARRAY_LEN(ary1
); i
++) {
2534 if (st_lookup(RHASH_TBL(hash
), RARRAY_PTR(ary1
)[i
], 0)) continue;
2535 rb_ary_push(ary3
, rb_ary_elt(ary1
, i
));
2542 * array & other_array
2544 * Set Intersection---Returns a new array
2545 * containing elements common to the two arrays, with no duplicates.
2547 * [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]
2552 rb_ary_and(VALUE ary1
, VALUE ary2
)
2554 VALUE hash
, ary3
, v
, vv
;
2557 ary2
= to_ary(ary2
);
2558 ary3
= rb_ary_new2(RARRAY_LEN(ary1
) < RARRAY_LEN(ary2
) ?
2559 RARRAY_LEN(ary1
) : RARRAY_LEN(ary2
));
2560 hash
= ary_make_hash(ary2
, 0);
2562 if (RHASH_EMPTY_P(hash
))
2565 for (i
=0; i
<RARRAY_LEN(ary1
); i
++) {
2566 v
= vv
= rb_ary_elt(ary1
, i
);
2567 if (st_delete(RHASH_TBL(hash
), (st_data_t
*)&vv
, 0)) {
2568 rb_ary_push(ary3
, v
);
2577 * array | other_array -> an_array
2579 * Set Union---Returns a new array by joining this array with
2580 * other_array, removing duplicates.
2582 * [ "a", "b", "c" ] | [ "c", "d", "a" ]
2583 * #=> [ "a", "b", "c", "d" ]
2587 rb_ary_or(VALUE ary1
, VALUE ary2
)
2593 ary2
= to_ary(ary2
);
2594 ary3
= rb_ary_new2(RARRAY_LEN(ary1
)+RARRAY_LEN(ary2
));
2595 hash
= ary_make_hash(ary1
, ary2
);
2597 for (i
=0; i
<RARRAY_LEN(ary1
); i
++) {
2598 v
= vv
= rb_ary_elt(ary1
, i
);
2599 if (st_delete(RHASH_TBL(hash
), (st_data_t
*)&vv
, 0)) {
2600 rb_ary_push(ary3
, v
);
2603 for (i
=0; i
<RARRAY_LEN(ary2
); i
++) {
2604 v
= vv
= rb_ary_elt(ary2
, i
);
2605 if (st_delete(RHASH_TBL(hash
), (st_data_t
*)&vv
, 0)) {
2606 rb_ary_push(ary3
, v
);
2614 * array.uniq! -> array or nil
2616 * Removes duplicate elements from _self_.
2617 * Returns <code>nil</code> if no changes are made (that is, no
2618 * duplicates are found).
2620 * a = [ "a", "a", "b", "b", "c" ]
2621 * a.uniq! #=> ["a", "b", "c"]
2622 * b = [ "a", "b", "c" ]
2627 rb_ary_uniq_bang(VALUE ary
)
2632 hash
= ary_make_hash(ary
, 0);
2634 if (RARRAY_LEN(ary
) == RHASH_SIZE(hash
)) {
2637 for (i
=j
=0; i
<RARRAY_LEN(ary
); i
++) {
2638 v
= vv
= rb_ary_elt(ary
, i
);
2639 if (st_delete(RHASH_TBL(hash
), (st_data_t
*)&vv
, 0)) {
2640 rb_ary_store(ary
, j
++, v
);
2643 RARRAY(ary
)->len
= j
;
2650 * array.uniq -> an_array
2652 * Returns a new array by removing duplicate values in <i>self</i>.
2654 * a = [ "a", "a", "b", "b", "c" ]
2655 * a.uniq #=> ["a", "b", "c"]
2659 rb_ary_uniq(VALUE ary
)
2661 ary
= rb_ary_dup(ary
);
2662 rb_ary_uniq_bang(ary
);
2668 * array.compact! -> array or nil
2670 * Removes +nil+ elements from array.
2671 * Returns +nil+ if no changes were made.
2673 * [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
2674 * [ "a", "b", "c" ].compact! #=> nil
2678 rb_ary_compact_bang(VALUE ary
)
2684 p
= t
= RARRAY_PTR(ary
);
2685 end
= p
+ RARRAY_LEN(ary
);
2691 if (RARRAY_LEN(ary
) == (p
- RARRAY_PTR(ary
))) {
2694 n
= p
- RARRAY_PTR(ary
);
2695 RESIZE_CAPA(ary
, n
);
2696 RARRAY(ary
)->len
= n
;
2703 * array.compact -> an_array
2705 * Returns a copy of _self_ with all +nil+ elements removed.
2707 * [ "a", nil, "b", nil, "c", nil ].compact
2708 * #=> [ "a", "b", "c" ]
2712 rb_ary_compact(VALUE ary
)
2714 ary
= rb_ary_dup(ary
);
2715 rb_ary_compact_bang(ary
);
2721 * array.nitems -> int
2722 * array.nitems { |item| block } -> int
2724 * Returns the number of non-<code>nil</code> elements in _self_.
2725 * If a block is given, the elements yielding a true value are
2730 * [ 1, nil, 3, nil, 5 ].nitems #=> 3
2731 * [5,6,7,8,9].nitems { |x| x % 2 != 0 } #=> 3
2735 rb_ary_nitems(VALUE ary
)
2739 if (rb_block_given_p()) {
2742 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
2743 VALUE v
= RARRAY_PTR(ary
)[i
];
2744 if (RTEST(rb_yield(v
))) n
++;
2748 VALUE
*p
= RARRAY_PTR(ary
);
2749 VALUE
*pend
= p
+ RARRAY_LEN(ary
);
2752 if (!NIL_P(*p
)) n
++;
2760 flatten(VALUE ary
, int level
, int *modified
)
2763 VALUE stack
, result
, tmp
, elt
;
2767 stack
= rb_ary_new();
2768 result
= ary_new(rb_class_of(ary
), RARRAY_LEN(ary
));
2769 memo
= st_init_numtable();
2770 st_insert(memo
, (st_data_t
)ary
, (st_data_t
)Qtrue
);
2774 while (i
< RARRAY_LEN(ary
)) {
2775 elt
= RARRAY_PTR(ary
)[i
++];
2776 tmp
= rb_check_array_type(elt
);
2777 if (NIL_P(tmp
) || (level
>= 0 && RARRAY_LEN(stack
) / 2 >= level
)) {
2778 rb_ary_push(result
, elt
);
2782 id
= (st_data_t
)tmp
;
2783 if (st_lookup(memo
, id
, 0)) {
2784 rb_raise(rb_eArgError
, "tried to flatten recursive array");
2786 st_insert(memo
, id
, (st_data_t
)Qtrue
);
2787 rb_ary_push(stack
, ary
);
2788 rb_ary_push(stack
, LONG2NUM(i
));
2793 if (RARRAY_LEN(stack
) == 0) {
2796 id
= (st_data_t
)ary
;
2797 st_delete(memo
, &id
, 0);
2798 tmp
= rb_ary_pop(stack
);
2800 ary
= rb_ary_pop(stack
);
2808 * array.flatten! -> array or nil
2809 * array.flatten!(level) -> array or nil
2811 * Flattens _self_ in place.
2812 * Returns <code>nil</code> if no modifications were made (i.e.,
2813 * <i>array</i> contains no subarrays.) If the optional <i>level</i>
2814 * argument determines the level of recursion to flatten.
2816 * a = [ 1, 2, [3, [4, 5] ] ]
2817 * a.flatten! #=> [1, 2, 3, 4, 5]
2818 * a.flatten! #=> nil
2819 * a #=> [1, 2, 3, 4, 5]
2820 * a = [ 1, 2, [3, [4, 5] ] ]
2821 * a.flatten!(1) #=> [1, 2, 3, [4, 5]]
2825 rb_ary_flatten_bang(int argc
, VALUE
*argv
, VALUE ary
)
2827 int mod
= 0, level
= -1;
2830 rb_scan_args(argc
, argv
, "01", &lv
);
2831 if (!NIL_P(lv
)) level
= NUM2INT(lv
);
2832 if (level
== 0) return ary
;
2834 result
= flatten(ary
, level
, &mod
);
2835 if (mod
== 0) return Qnil
;
2836 rb_ary_replace(ary
, result
);
2843 * array.flatten -> an_array
2844 * array.flatten(level) -> an_array
2846 * Returns a new array that is a one-dimensional flattening of this
2847 * array (recursively). That is, for every element that is an array,
2848 * extract its elements into the new array. If the optional
2849 * <i>level</i> argument determines the level of recursion to flatten.
2851 * s = [ 1, 2, 3 ] #=> [1, 2, 3]
2852 * t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
2853 * a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
2854 * a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2855 * a = [ 1, 2, [3, [4, 5] ] ]
2856 * a.flatten(1) #=> [1, 2, 3, [4, 5]]
2860 rb_ary_flatten(int argc
, VALUE
*argv
, VALUE ary
)
2862 int mod
= 0, level
= -1;
2865 rb_scan_args(argc
, argv
, "01", &lv
);
2866 if (!NIL_P(lv
)) level
= NUM2INT(lv
);
2867 if (level
== 0) return ary
;
2869 result
= flatten(ary
, level
, &mod
);
2870 if (OBJ_TAINTED(ary
)) OBJ_TAINT(result
);
2877 * array.shuffle! -> array or nil
2879 * Shuffles elements in _self_ in place.
2884 rb_ary_shuffle_bang(VALUE ary
)
2886 long i
= RARRAY_LEN(ary
);
2890 long j
= rb_genrand_real()*i
;
2891 VALUE tmp
= RARRAY_PTR(ary
)[--i
];
2892 RARRAY_PTR(ary
)[i
] = RARRAY_PTR(ary
)[j
];
2893 RARRAY_PTR(ary
)[j
] = tmp
;
2901 * array.shuffle -> an_array
2903 * Returns a new array with elements of this array shuffled.
2905 * a = [ 1, 2, 3 ] #=> [1, 2, 3]
2906 * a.shuffle #=> [2, 3, 1]
2910 rb_ary_shuffle(VALUE ary
)
2912 ary
= rb_ary_dup(ary
);
2913 rb_ary_shuffle_bang(ary
);
2920 * array.choice -> obj
2922 * Choose a random element from an array.
2927 rb_ary_choice(VALUE ary
)
2931 i
= RARRAY_LEN(ary
);
2932 if (i
== 0) return Qnil
;
2933 j
= rb_genrand_real()*i
;
2934 return RARRAY_PTR(ary
)[j
];
2940 * ary.cycle {|obj| block }
2941 * ary.cycle(n) {|obj| block }
2943 * Calls <i>block</i> for each element repeatedly _n_ times or
2944 * forever if none or nil is given. If a non-positive number is
2945 * given or the array is empty, does nothing. Returns nil if the
2946 * loop has finished without getting interrupted.
2948 * a = ["a", "b", "c"]
2949 * a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever.
2950 * a.cycle(2) {|x| puts x } # print, a, b, c, a, b, c.
2955 rb_ary_cycle(int argc
, VALUE
*argv
, VALUE ary
)
2960 rb_scan_args(argc
, argv
, "01", &nv
);
2962 RETURN_ENUMERATOR(ary
, argc
, argv
);
2968 if (n
<= 0) return Qnil
;
2971 while (RARRAY_LEN(ary
) > 0 && (n
< 0 || 0 < n
--)) {
2972 for (i
=0; i
<RARRAY_LEN(ary
); i
++) {
2973 rb_yield(RARRAY_PTR(ary
)[i
]);
2979 #define tmpbuf(n, size) rb_str_tmp_new((n)*(size))
2982 * Recursively compute permutations of r elements of the set [0..n-1].
2983 * When we have a complete permutation of array indexes, copy the values
2984 * at those indexes into a new array and yield that array.
2986 * n: the size of the set
2987 * r: the number of elements in each permutation
2988 * p: the array (of size r) that we're filling in
2989 * index: what index we're filling in now
2990 * used: an array of booleans: whether a given index is already used
2991 * values: the Ruby array that holds the actual values to permute
2994 permute0(long n
, long r
, long *p
, long index
, int *used
, VALUE values
)
2997 for (i
= 0; i
< n
; i
++) {
3000 if (index
< r
-1) { /* if not done yet */
3001 used
[i
] = 1; /* mark index used */
3002 permute0(n
, r
, p
, index
+1, /* recurse */
3004 used
[i
] = 0; /* index unused */
3007 /* We have a complete permutation of array indexes */
3008 /* Build a ruby array of the corresponding values */
3009 /* And yield it to the associated block */
3010 VALUE result
= rb_ary_new2(r
);
3011 VALUE
*result_array
= RARRAY_PTR(result
);
3012 const VALUE
*values_array
= RARRAY_PTR(values
);
3014 for (j
= 0; j
< r
; j
++) result_array
[j
] = values_array
[p
[j
]];
3015 RARRAY(result
)->len
= r
;
3024 * ary.permutation { |p| block } -> array
3025 * ary.permutation -> enumerator
3026 * ary.permutation(n) { |p| block } -> array
3027 * ary.permutation(n) -> enumerator
3029 * When invoked with a block, yield all permutations of length <i>n</i>
3030 * of the elements of <i>ary</i>, then return the array itself.
3031 * If <i>n</i> is not specified, yield all permutations of all elements.
3032 * The implementation makes no guarantees about the order in which
3033 * the permutations are yielded.
3035 * When invoked without a block, return an enumerator object instead.
3040 * a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
3041 * a.permutation(1).to_a #=> [[1],[2],[3]]
3042 * a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
3043 * a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
3044 * a.permutation(0).to_a #=> [[]] # one permutation of length 0
3045 * a.permutation(4).to_a #=> [] # no permutations of length 4
3049 rb_ary_permutation(int argc
, VALUE
*argv
, VALUE ary
)
3054 n
= RARRAY_LEN(ary
); /* Array length */
3055 RETURN_ENUMERATOR(ary
, argc
, argv
); /* Return enumerator if no block */
3056 rb_scan_args(argc
, argv
, "01", &num
);
3057 r
= NIL_P(num
) ? n
: NUM2LONG(num
); /* Permutation size from argument */
3059 if (r
< 0 || n
< r
) {
3060 /* no permutations: yield nothing */
3062 else if (r
== 0) { /* exactly one permutation: the zero-length array */
3063 rb_yield(rb_ary_new2(0));
3065 else if (r
== 1) { /* this is a special, easy case */
3066 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
3067 rb_yield(rb_ary_new3(1, RARRAY_PTR(ary
)[i
]));
3070 else { /* this is the general case */
3071 volatile VALUE t0
= tmpbuf(n
,sizeof(long));
3072 long *p
= (long*)RSTRING_PTR(t0
);
3073 volatile VALUE t1
= tmpbuf(n
,sizeof(int));
3074 int *used
= (int*)RSTRING_PTR(t1
);
3075 VALUE ary0
= ary_make_shared(ary
); /* private defensive copy of ary */
3077 for (i
= 0; i
< n
; i
++) used
[i
] = 0; /* initialize array */
3079 permute0(n
, r
, p
, 0, used
, ary0
); /* compute and yield permutations */
3087 combi_len(long n
, long k
)
3091 if (k
*2 > n
) k
= n
-k
;
3092 if (k
== 0) return 1;
3093 if (k
< 0) return 0;
3095 for (i
=1; i
<= k
; i
++,n
--) {
3099 rb_raise(rb_eRangeError
, "too big for combination");
3108 * ary.combination(n) { |c| block } -> ary
3109 * ary.combination(n) -> enumerator
3111 * When invoked with a block, yields all combinations of length <i>n</i>
3112 * of elements from <i>ary</i> and then returns <i>ary</i> itself.
3113 * The implementation makes no guarantees about the order in which
3114 * the combinations are yielded.
3116 * When invoked without a block, returns an enumerator object instead.
3121 * a.combination(1).to_a #=> [[1],[2],[3],[4]]
3122 * a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
3123 * a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
3124 * a.combination(4).to_a #=> [[1,2,3,4]]
3125 * a.combination(0).to_a #=> [[]] # one combination of length 0
3126 * a.combination(5).to_a #=> [] # no combinations of length 5
3131 rb_ary_combination(VALUE ary
, VALUE num
)
3136 RETURN_ENUMERATOR(ary
, 1, &num
);
3137 len
= RARRAY_LEN(ary
);
3138 if (n
< 0 || len
< n
) {
3142 rb_yield(rb_ary_new2(0));
3145 for (i
= 0; i
< len
; i
++) {
3146 rb_yield(rb_ary_new3(1, RARRAY_PTR(ary
)[i
]));
3150 volatile VALUE t0
= tmpbuf(n
+1, sizeof(long));
3151 long *stack
= (long*)RSTRING_PTR(t0
);
3152 long nlen
= combi_len(len
, n
);
3153 volatile VALUE cc
= rb_ary_new2(n
);
3154 VALUE
*chosen
= RARRAY_PTR(cc
);
3157 RBASIC(cc
)->klass
= 0;
3158 MEMZERO(stack
, long, n
);
3160 for (i
= 0; i
< nlen
; i
++) {
3161 chosen
[lev
] = RARRAY_PTR(ary
)[stack
[lev
+1]];
3162 for (lev
++; lev
< n
; lev
++) {
3163 chosen
[lev
] = RARRAY_PTR(ary
)[stack
[lev
+1] = stack
[lev
]+1];
3165 rb_yield(rb_ary_new4(n
, chosen
));
3168 } while (lev
&& (stack
[lev
+1]+n
== len
+lev
+1));
3176 * ary.product(other_ary, ...)
3178 * Returns an array of all combinations of elements from all arrays.
3179 * The length of the returned array is the product of the length
3180 * of ary and the argument arrays
3182 * [1,2,3].product([4,5]) # => [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
3183 * [1,2].product([1,2]) # => [[1,1],[1,2],[2,1],[2,2]]
3184 * [1,2].product([3,4],[5,6]) # => [[1,3,5],[1,3,6],[1,4,5],[1,4,6],
3185 * # [2,3,5],[2,3,6],[2,4,5],[2,4,6]]
3186 * [1,2].product() # => [[1],[2]]
3187 * [1,2].product([]) # => []
3191 rb_ary_product(int argc
, VALUE
*argv
, VALUE ary
)
3193 int n
= argc
+1; /* How many arrays we're operating on */
3194 volatile VALUE t0
= tmpbuf(n
, sizeof(VALUE
));
3195 volatile VALUE t1
= tmpbuf(n
, sizeof(int));
3196 VALUE
*arrays
= (VALUE
*)RSTRING_PTR(t0
); /* The arrays we're computing the product of */
3197 int *counters
= (int*)RSTRING_PTR(t1
); /* The current position in each one */
3198 VALUE result
; /* The array we'll be returning */
3202 RBASIC(t0
)->klass
= 0;
3203 RBASIC(t1
)->klass
= 0;
3205 /* initialize the arrays of arrays */
3207 for (i
= 1; i
< n
; i
++) arrays
[i
] = to_ary(argv
[i
-1]);
3209 /* initialize the counters for the arrays */
3210 for (i
= 0; i
< n
; i
++) counters
[i
] = 0;
3212 /* Compute the length of the result array; return [] if any is empty */
3213 for (i
= 0; i
< n
; i
++) {
3214 long k
= RARRAY_LEN(arrays
[i
]), l
= resultlen
;
3215 if (k
== 0) return rb_ary_new2(0);
3217 if (resultlen
< k
|| resultlen
< l
|| resultlen
/ k
!= l
) {
3218 rb_raise(rb_eRangeError
, "too big to product");
3222 /* Otherwise, allocate and fill in an array of results */
3223 result
= rb_ary_new2(resultlen
);
3224 for (i
= 0; i
< resultlen
; i
++) {
3226 /* fill in one subarray */
3227 VALUE subarray
= rb_ary_new2(n
);
3228 for (j
= 0; j
< n
; j
++) {
3229 rb_ary_push(subarray
, rb_ary_entry(arrays
[j
], counters
[j
]));
3232 /* put it on the result array */
3233 rb_ary_push(result
, subarray
);
3236 * Increment the last counter. If it overflows, reset to 0
3237 * and increment the one before it.
3241 while (m
> 0 && counters
[m
] == RARRAY_LEN(arrays
[m
])) {
3253 * ary.take(n) => array
3255 * Returns first n elements from <i>ary</i>.
3257 * a = [1, 2, 3, 4, 5, 0]
3258 * a.take(3) # => [1, 2, 3]
3263 rb_ary_take(VALUE obj
, VALUE n
)
3265 long len
= NUM2LONG(n
);
3267 rb_raise(rb_eArgError
, "attempt to take negative size");
3269 return rb_ary_subseq(obj
, 0, len
);
3274 * ary.take_while {|arr| block } => array
3276 * Passes elements to the block until the block returns nil or false,
3277 * then stops iterating and returns an array of all prior elements.
3279 * a = [1, 2, 3, 4, 5, 0]
3280 * a.take_while {|i| i < 3 } # => [1, 2]
3285 rb_ary_take_while(VALUE ary
)
3289 RETURN_ENUMERATOR(ary
, 0, 0);
3290 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
3291 if (!RTEST(rb_yield(RARRAY_PTR(ary
)[i
]))) break;
3293 return rb_ary_take(ary
, LONG2FIX(i
));
3298 * ary.drop(n) => array
3300 * Drops first n elements from <i>ary</i>, and returns rest elements
3303 * a = [1, 2, 3, 4, 5, 0]
3304 * a.drop(3) # => [4, 5, 0]
3309 rb_ary_drop(VALUE ary
, VALUE n
)
3312 long pos
= NUM2LONG(n
);
3314 rb_raise(rb_eArgError
, "attempt to drop negative size");
3317 result
= rb_ary_subseq(ary
, pos
, RARRAY_LEN(ary
));
3318 if (result
== Qnil
) result
= rb_ary_new();
3324 * ary.drop_while {|arr| block } => array
3326 * Drops elements up to, but not including, the first element for
3327 * which the block returns nil or false and returns an array
3328 * containing the remaining elements.
3330 * a = [1, 2, 3, 4, 5, 0]
3331 * a.drop_while {|i| i < 3 } # => [3, 4, 5, 0]
3336 rb_ary_drop_while(VALUE ary
)
3340 RETURN_ENUMERATOR(ary
, 0, 0);
3341 for (i
= 0; i
< RARRAY_LEN(ary
); i
++) {
3342 if (!RTEST(rb_yield(RARRAY_PTR(ary
)[i
]))) break;
3344 return rb_ary_drop(ary
, LONG2FIX(i
));
3349 /* Arrays are ordered, integer-indexed collections of any object.
3350 * Array indexing starts at 0, as in C or Java. A negative index is
3351 * assumed to be relative to the end of the array---that is, an index of -1
3352 * indicates the last element of the array, -2 is the next to last
3353 * element in the array, and so on.
3359 rb_cArray
= rb_define_class("Array", rb_cObject
);
3360 rb_include_module(rb_cArray
, rb_mEnumerable
);
3362 rb_define_alloc_func(rb_cArray
, ary_alloc
);
3363 rb_define_singleton_method(rb_cArray
, "[]", rb_ary_s_create
, -1);
3364 rb_define_singleton_method(rb_cArray
, "try_convert", rb_ary_s_try_convert
, 1);
3365 rb_define_method(rb_cArray
, "initialize", rb_ary_initialize
, -1);
3366 rb_define_method(rb_cArray
, "initialize_copy", rb_ary_replace
, 1);
3368 rb_define_method(rb_cArray
, "to_s", rb_ary_inspect
, 0);
3369 rb_define_method(rb_cArray
, "inspect", rb_ary_inspect
, 0);
3370 rb_define_method(rb_cArray
, "to_a", rb_ary_to_a
, 0);
3371 rb_define_method(rb_cArray
, "to_ary", rb_ary_to_ary_m
, 0);
3372 rb_define_method(rb_cArray
, "frozen?", rb_ary_frozen_p
, 0);
3374 rb_define_method(rb_cArray
, "==", rb_ary_equal
, 1);
3375 rb_define_method(rb_cArray
, "eql?", rb_ary_eql
, 1);
3376 rb_define_method(rb_cArray
, "hash", rb_ary_hash
, 0);
3378 rb_define_method(rb_cArray
, "[]", rb_ary_aref
, -1);
3379 rb_define_method(rb_cArray
, "[]=", rb_ary_aset
, -1);
3380 rb_define_method(rb_cArray
, "at", rb_ary_at
, 1);
3381 rb_define_method(rb_cArray
, "fetch", rb_ary_fetch
, -1);
3382 rb_define_method(rb_cArray
, "first", rb_ary_first
, -1);
3383 rb_define_method(rb_cArray
, "last", rb_ary_last
, -1);
3384 rb_define_method(rb_cArray
, "concat", rb_ary_concat
, 1);
3385 rb_define_method(rb_cArray
, "<<", rb_ary_push
, 1);
3386 rb_define_method(rb_cArray
, "push", rb_ary_push_m
, -1);
3387 rb_define_method(rb_cArray
, "pop", rb_ary_pop_m
, -1);
3388 rb_define_method(rb_cArray
, "shift", rb_ary_shift_m
, -1);
3389 rb_define_method(rb_cArray
, "unshift", rb_ary_unshift_m
, -1);
3390 rb_define_method(rb_cArray
, "insert", rb_ary_insert
, -1);
3391 rb_define_method(rb_cArray
, "each", rb_ary_each
, 0);
3392 rb_define_method(rb_cArray
, "each_index", rb_ary_each_index
, 0);
3393 rb_define_method(rb_cArray
, "reverse_each", rb_ary_reverse_each
, 0);
3394 rb_define_method(rb_cArray
, "length", rb_ary_length
, 0);
3395 rb_define_alias(rb_cArray
, "size", "length");
3396 rb_define_method(rb_cArray
, "empty?", rb_ary_empty_p
, 0);
3397 rb_define_method(rb_cArray
, "find_index", rb_ary_index
, -1);
3398 rb_define_method(rb_cArray
, "index", rb_ary_index
, -1);
3399 rb_define_method(rb_cArray
, "rindex", rb_ary_rindex
, -1);
3400 rb_define_method(rb_cArray
, "join", rb_ary_join_m
, -1);
3401 rb_define_method(rb_cArray
, "reverse", rb_ary_reverse_m
, 0);
3402 rb_define_method(rb_cArray
, "reverse!", rb_ary_reverse_bang
, 0);
3403 rb_define_method(rb_cArray
, "sort", rb_ary_sort
, 0);
3404 rb_define_method(rb_cArray
, "sort!", rb_ary_sort_bang
, 0);
3405 rb_define_method(rb_cArray
, "collect", rb_ary_collect
, 0);
3406 rb_define_method(rb_cArray
, "collect!", rb_ary_collect_bang
, 0);
3407 rb_define_method(rb_cArray
, "map", rb_ary_collect
, 0);
3408 rb_define_method(rb_cArray
, "map!", rb_ary_collect_bang
, 0);
3409 rb_define_method(rb_cArray
, "select", rb_ary_select
, 0);
3410 rb_define_method(rb_cArray
, "values_at", rb_ary_values_at
, -1);
3411 rb_define_method(rb_cArray
, "delete", rb_ary_delete
, 1);
3412 rb_define_method(rb_cArray
, "delete_at", rb_ary_delete_at_m
, 1);
3413 rb_define_method(rb_cArray
, "delete_if", rb_ary_delete_if
, 0);
3414 rb_define_method(rb_cArray
, "reject", rb_ary_reject
, 0);
3415 rb_define_method(rb_cArray
, "reject!", rb_ary_reject_bang
, 0);
3416 rb_define_method(rb_cArray
, "zip", rb_ary_zip
, -1);
3417 rb_define_method(rb_cArray
, "transpose", rb_ary_transpose
, 0);
3418 rb_define_method(rb_cArray
, "replace", rb_ary_replace
, 1);
3419 rb_define_method(rb_cArray
, "clear", rb_ary_clear
, 0);
3420 rb_define_method(rb_cArray
, "fill", rb_ary_fill
, -1);
3421 rb_define_method(rb_cArray
, "include?", rb_ary_includes
, 1);
3422 rb_define_method(rb_cArray
, "<=>", rb_ary_cmp
, 1);
3424 rb_define_method(rb_cArray
, "slice", rb_ary_aref
, -1);
3425 rb_define_method(rb_cArray
, "slice!", rb_ary_slice_bang
, -1);
3427 rb_define_method(rb_cArray
, "assoc", rb_ary_assoc
, 1);
3428 rb_define_method(rb_cArray
, "rassoc", rb_ary_rassoc
, 1);
3430 rb_define_method(rb_cArray
, "+", rb_ary_plus
, 1);
3431 rb_define_method(rb_cArray
, "*", rb_ary_times
, 1);
3433 rb_define_method(rb_cArray
, "-", rb_ary_diff
, 1);
3434 rb_define_method(rb_cArray
, "&", rb_ary_and
, 1);
3435 rb_define_method(rb_cArray
, "|", rb_ary_or
, 1);
3437 rb_define_method(rb_cArray
, "uniq", rb_ary_uniq
, 0);
3438 rb_define_method(rb_cArray
, "uniq!", rb_ary_uniq_bang
, 0);
3439 rb_define_method(rb_cArray
, "compact", rb_ary_compact
, 0);
3440 rb_define_method(rb_cArray
, "compact!", rb_ary_compact_bang
, 0);
3441 rb_define_method(rb_cArray
, "flatten", rb_ary_flatten
, -1);
3442 rb_define_method(rb_cArray
, "flatten!", rb_ary_flatten_bang
, -1);
3443 rb_define_method(rb_cArray
, "nitems", rb_ary_nitems
, 0);
3444 rb_define_method(rb_cArray
, "shuffle!", rb_ary_shuffle_bang
, 0);
3445 rb_define_method(rb_cArray
, "shuffle", rb_ary_shuffle
, 0);
3446 rb_define_method(rb_cArray
, "choice", rb_ary_choice
, 0);
3447 rb_define_method(rb_cArray
, "cycle", rb_ary_cycle
, -1);
3448 rb_define_method(rb_cArray
, "permutation", rb_ary_permutation
, -1);
3449 rb_define_method(rb_cArray
, "combination", rb_ary_combination
, 1);
3450 rb_define_method(rb_cArray
, "product", rb_ary_product
, -1);
3452 rb_define_method(rb_cArray
, "take", rb_ary_take
, 1);
3453 rb_define_method(rb_cArray
, "take_while", rb_ary_take_while
, 0);
3454 rb_define_method(rb_cArray
, "drop", rb_ary_drop
, 1);
3455 rb_define_method(rb_cArray
, "drop_while", rb_ary_drop_while
, 0);
3457 id_cmp
= rb_intern("<=>");