From a59c599209a11d4ab0dc0d7626ab3d5ca43a78c2 Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 11 Dec 2001 03:48:08 +0000 Subject: * 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 --- hash.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'hash.c') diff --git a/hash.c b/hash.c index 94c7e33a33..b91ce7f113 100644 --- a/hash.c +++ b/hash.c @@ -385,6 +385,8 @@ rb_hash_indexes(argc, argv, hash) VALUE indexes; int i; + rb_warn("Hash#%s is deprecated; use Hash#select", + rb_id2name(rb_frame_last_func())); indexes = rb_ary_new2(argc); for (i=0; iptr[i] = rb_hash_aref(hash, argv[i]); @@ -483,6 +485,42 @@ rb_hash_reject(hash) return rb_hash_delete_if(rb_obj_dup(hash)); } +static int +select_i(key, value, result) + VALUE key, value; +{ + VALUE assoc; + + if (key == Qundef) return ST_CONTINUE; + assoc = rb_assoc_new(key, value); + if (RTEST(rb_yield(assoc))) + rb_ary_push(result, assoc); + return ST_CONTINUE; +} + +VALUE +rb_hash_select(argc, argv, hash) + int argc; + VALUE *argv; + VALUE hash; +{ + 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); + } + rb_hash_foreach(hash, select_i, result); + } + else { + for (i=0; i 0) { + rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc); + } + env = GET_ENVIRON(environ); + while (*env) { + char *s = strchr(*env, '='); + if (s) { + VALUE str = rb_tainted_str_new(*env, s-*env); + + if (RTEST(rb_yield(str))) { + rb_ary_push(result, str); + } + } + env++; + } + FREE_ENVIRON(environ); + } + else { + for (i=0; i