summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-12-11 03:48:08 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-12-11 03:48:08 +0000
commita59c599209a11d4ab0dc0d7626ab3d5ca43a78c2 (patch)
treef4c4400099a7feb51a7b303cbedc0e1f04714f43 /array.c
parent8a91c99905c1bfbf441ec890161538acc8e34120 (diff)
* string.c (rb_str_match_m): should convert an argument into
regexp if it's a string. * array.c (rb_ary_select): Array#select(n,m,...) now works like Array#indexes(n,m,..). [new, experimental] * hash.c (rb_hash_select): ditto. * hash.c (env_select): ditto. * re.c (match_select): ditto. * struct.c (rb_struct_select): ditto. * gc.c (STR_ASSOC): use FL_USER3 instead of FL_USER2. * parse.y (str_extend): make up pushback call. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1905 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/array.c b/array.c
index 0401dd3598..c9b66eb2ed 100644
--- a/array.c
+++ b/array.c
@@ -590,6 +590,8 @@ rb_ary_indexes(argc, argv, ary)
VALUE new_ary;
long i;
+ rb_warn("Array#%s is deprecated; use Array#select",
+ rb_id2name(rb_frame_last_func()));
new_ary = rb_ary_new2(argc);
for (i=0; i<argc; i++) {
rb_ary_push(new_ary, rb_ary_aref(1, argv+i, ary));
@@ -1171,6 +1173,33 @@ rb_ary_filter(ary)
return rb_ary_collect_bang(ary);
}
+static VALUE
+rb_ary_select(argc, argv, ary)
+ int argc;
+ VALUE *argv;
+ VALUE ary;
+{
+ VALUE result = rb_ary_new();
+ long i;
+
+ if (rb_block_given_p()) {
+ if (argc > 0) {
+ rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
+ }
+ for (i = 0; i < RARRAY(ary)->len; i++) {
+ if (RTEST(rb_yield(RARRAY(ary)->ptr[i]))) {
+ rb_ary_push(result, RARRAY(ary)->ptr[i]);
+ }
+ }
+ }
+ else {
+ for (i=0; i<argc; i++) {
+ rb_ary_push(result, rb_ary_entry(ary, NUM2LONG(argv[i])));
+ }
+ }
+ return result;
+}
+
VALUE
rb_ary_delete(ary, item)
VALUE ary;
@@ -1837,6 +1866,7 @@ Init_Array()
rb_define_method(rb_cArray, "sort!", rb_ary_sort_bang, 0);
rb_define_method(rb_cArray, "collect", rb_ary_collect, 0);
rb_define_method(rb_cArray, "collect!", rb_ary_collect_bang, 0);
+ rb_define_method(rb_cArray, "select", rb_ary_select, -1);
rb_define_method(rb_cArray, "map", rb_ary_collect, 0);
rb_define_method(rb_cArray, "map!", rb_ary_collect_bang, 0);
rb_define_method(rb_cArray, "filter", rb_ary_filter, 0);