summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-11-03 14:07:48 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-11-03 14:07:48 +0000
commit6e8eea7abe958e7323e0941687ca2e917426d681 (patch)
tree3d714f6a0d12fb29aa1a87e325629c33b666f7da /hash.c
parentfbc00c877258057fe282659cf5e7d0a1c9dcc80e (diff)
* hash.c (rb_hash_each_pair): make Hash#each to be alias to
Hash#each_pair for compatibility and clarity. * hash.c (env_each_pair): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13809 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c70
1 files changed, 10 insertions, 60 deletions
diff --git a/hash.c b/hash.c
index 821bfcbb8a..385e056248 100644
--- a/hash.c
+++ b/hash.c
@@ -1084,39 +1084,6 @@ static int
each_pair_i(VALUE key, VALUE value)
{
if (key == Qundef) return ST_CONTINUE;
- rb_yield_values(2, key, value);
- return ST_CONTINUE;
-}
-
-/*
- * call-seq:
- * hsh.each_pair {| key_value_array | block } -> hsh
- *
- * Calls <i>block</i> once for each key in <i>hsh</i>, passing the
- * key and value to the block as a two-element array.
- *
- * h = { "a" => 100, "b" => 200 }
- * h.each_pair {|(key, value)| puts "#{key} is #{value}" }
- *
- * <em>produces:</em>
- *
- * a is 100
- * b is 200
- *
- */
-
-static VALUE
-rb_hash_each_pair(VALUE hash)
-{
- RETURN_ENUMERATOR(hash, 0, 0);
- rb_hash_foreach(hash, each_pair_i, 0);
- return hash;
-}
-
-static int
-each_i(VALUE key, VALUE value)
-{
- if (key == Qundef) return ST_CONTINUE;
rb_yield(rb_assoc_new(key, value));
return ST_CONTINUE;
}
@@ -1124,10 +1091,10 @@ each_i(VALUE key, VALUE value)
/*
* call-seq:
* hsh.each {| key, value | block } -> hsh
+ * hsh.each_pair {| key, value | block } -> hsh
*
* Calls <i>block</i> once for each key in <i>hsh</i>, passing the key-value
- * pair as parameters. Also see <code>Hash#each_pair</code>, which
- * passes the key and value to the block as a two-element array.
+ * pair as parameters.
*
* h = { "a" => 100, "b" => 200 }
* h.each {|key, value| puts "#{key} is #{value}" }
@@ -1140,10 +1107,10 @@ each_i(VALUE key, VALUE value)
*/
static VALUE
-rb_hash_each(VALUE hash)
+rb_hash_each_pair(VALUE hash)
{
RETURN_ENUMERATOR(hash, 0, 0);
- rb_hash_foreach(hash, each_i, 0);
+ rb_hash_foreach(hash, each_pair_i, 0);
return hash;
}
@@ -2134,12 +2101,14 @@ env_each_value(VALUE ehash)
}
static VALUE
-env_each_i(VALUE ehash, int values)
+env_each_pair(VALUE ehash)
{
char **env;
VALUE ary;
long i;
+ RETURN_ENUMERATOR(ehash, 0, 0);
+
rb_secure(4);
ary = rb_ary_new();
env = GET_ENVIRON(environ);
@@ -2154,31 +2123,12 @@ env_each_i(VALUE ehash, int values)
FREE_ENVIRON(environ);
for (i=0; i<RARRAY_LEN(ary); i+=2) {
- if (values) {
- rb_yield_values(2, RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]);
- }
- else {
- rb_yield(rb_assoc_new(RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]));
- }
+ rb_yield(rb_assoc_new(RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]));
}
return ehash;
}
static VALUE
-env_each(VALUE ehash)
-{
- RETURN_ENUMERATOR(ehash, 0, 0);
- return env_each_i(ehash, Qtrue);
-}
-
-static VALUE
-env_each_pair(VALUE ehash)
-{
- RETURN_ENUMERATOR(ehash, 0, 0);
- return env_each_i(ehash, Qfalse);
-}
-
-static VALUE
env_reject_bang(void)
{
volatile VALUE keys;
@@ -2622,10 +2572,10 @@ Init_Hash(void)
rb_define_method(rb_cHash,"length", rb_hash_size, 0);
rb_define_method(rb_cHash,"empty?", rb_hash_empty_p, 0);
- rb_define_method(rb_cHash,"each", rb_hash_each, 0);
rb_define_method(rb_cHash,"each_value", rb_hash_each_value, 0);
rb_define_method(rb_cHash,"each_key", rb_hash_each_key, 0);
rb_define_method(rb_cHash,"each_pair", rb_hash_each_pair, 0);
+ rb_define_method(rb_cHash,"each", rb_hash_each_pair, 0);
rb_define_method(rb_cHash,"keys", rb_hash_keys, 0);
rb_define_method(rb_cHash,"values", rb_hash_values, 0);
@@ -2666,7 +2616,7 @@ Init_Hash(void)
rb_define_singleton_method(envtbl,"fetch", env_fetch, -1);
rb_define_singleton_method(envtbl,"[]=", env_aset, 2);
rb_define_singleton_method(envtbl,"store", env_aset, 2);
- rb_define_singleton_method(envtbl,"each", env_each, 0);
+ rb_define_singleton_method(envtbl,"each", env_each_pair, 0);
rb_define_singleton_method(envtbl,"each_pair", env_each_pair, 0);
rb_define_singleton_method(envtbl,"each_key", env_each_key, 0);
rb_define_singleton_method(envtbl,"each_value", env_each_value, 0);