summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
Diffstat (limited to 'array.c')
-rw-r--r--array.c42
1 files changed, 22 insertions, 20 deletions
diff --git a/array.c b/array.c
index 6a6dce4..e39f1f1 100644
--- a/array.c
+++ b/array.c
@@ -403,24 +403,19 @@ ary_index(ary, val)
}
static VALUE
-ary_indexes(ary, args)
- VALUE ary, args;
+ary_indexes(argc, argv, ary)
+ int argc;
+ VALUE *argv;
+ VALUE ary;
{
- VALUE *p, *pend;
VALUE new_ary;
- int i = 0;
+ int i;
- if (!args || NIL_P(args) || RARRAY(args)->len == 0) {
- return ary_new2(0);
+ new_ary = ary_new2(argc);
+ for (i=0; i<argc; i++) {
+ ary_store(new_ary, i, ary_entry(ary, NUM2INT(argv[i])));
}
- new_ary = ary_new2(RARRAY(args)->len);
-
- p = RARRAY(args)->ptr; pend = p + RARRAY(args)->len;
- while (p < pend) {
- ary_store(new_ary, i++, ary_entry(ary, NUM2INT(*p)));
- p++;
- }
return new_ary;
}
@@ -821,20 +816,27 @@ ary_delete_if(ary)
return ary;
}
-#if 0
static VALUE
-ary_replace(ary)
+ary_filter(ary)
VALUE ary;
{
int i;
+ ary_modify(ary);
for (i = 0; i < RARRAY(ary)->len; i++) {
RARRAY(ary)->ptr[i] = rb_yield(RARRAY(ary)->ptr[i]);
}
+ return ary;
+}
+static VALUE
+ary_replace_method(ary, ary2)
+ VALUE ary, ary2;
+{
+ Check_Type(ary2, T_ARRAY);
+ ary_replace(ary, 0, RARRAY(ary2)->len, ary2);
return ary;
}
-#endif
static VALUE
ary_clear(ary)
@@ -1188,7 +1190,8 @@ Init_Array()
rb_define_alias(cArray, "size", "length");
rb_define_method(cArray, "empty?", ary_empty_p, 0);
rb_define_method(cArray, "index", ary_index, 1);
- rb_define_method(cArray, "indexes", ary_indexes, -2);
+ rb_define_method(cArray, "indexes", ary_indexes, -1);
+ rb_define_method(cArray, "indices", ary_indexes, -1);
rb_define_method(cArray, "clone", ary_clone, 0);
rb_define_method(cArray, "dup", ary_dup, 0);
rb_define_method(cArray, "join", ary_join_method, -1);
@@ -1199,9 +1202,8 @@ Init_Array()
rb_define_method(cArray, "delete", ary_delete, 1);
rb_define_method(cArray, "delete_at", ary_delete_at, 1);
rb_define_method(cArray, "delete_if", ary_delete_if, 0);
-#if 0
- rb_define_method(cArray, "replace", ary_replace, 0);
-#endif
+ rb_define_method(cArray, "filter", ary_filter, 0);
+ rb_define_method(cArray, "replace", ary_replace_method, 1);
rb_define_method(cArray, "clear", ary_clear, 0);
rb_define_method(cArray, "fill", ary_fill, -1);
rb_define_method(cArray, "include?", ary_includes, 1);