From 17c0aff0d6d8a2629b535a52a93e348904e27b97 Mon Sep 17 00:00:00 2001 From: marcandre Date: Tue, 6 Nov 2012 17:13:48 +0000 Subject: * hash.c: Support for enumerators created by ENV: each, each_value, ... [Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37512 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- hash.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'hash.c') diff --git a/hash.c b/hash.c index 126fd41dc8..b8acbc8491 100644 --- a/hash.c +++ b/hash.c @@ -2548,13 +2548,31 @@ env_keys(void) * * An Enumerator is returned if no block is given. */ +static VALUE +rb_env_size(VALUE ehash) +{ + char **env; + long cnt = 0; + + rb_secure(4); + + env = GET_ENVIRON(environ); + for (; *env ; ++env) { + if (strchr(*env, '=')) { + cnt++; + } + } + FREE_ENVIRON(environ); + return LONG2FIX(cnt); +} + static VALUE env_each_key(VALUE ehash) { VALUE keys; long i; - RETURN_ENUMERATOR(ehash, 0, 0); + RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); keys = env_keys(); /* rb_secure(4); */ for (i=0; i