зеркало из https://github.com/github/ruby.git
* 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
This commit is contained in:
Родитель
19ed71c8d0
Коммит
17c0aff0d6
34
hash.c
34
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<RARRAY_LEN(keys); i++) {
|
||||
rb_yield(RARRAY_PTR(keys)[i]);
|
||||
|
@ -2603,7 +2621,7 @@ env_each_value(VALUE ehash)
|
|||
VALUE values;
|
||||
long i;
|
||||
|
||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
values = env_values(); /* rb_secure(4); */
|
||||
for (i=0; i<RARRAY_LEN(values); i++) {
|
||||
rb_yield(RARRAY_PTR(values)[i]);
|
||||
|
@ -2629,7 +2647,7 @@ env_each_pair(VALUE ehash)
|
|||
VALUE ary;
|
||||
long i;
|
||||
|
||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
|
||||
rb_secure(4);
|
||||
ary = rb_ary_new();
|
||||
|
@ -2666,7 +2684,7 @@ env_reject_bang(VALUE ehash)
|
|||
long i;
|
||||
int del = 0;
|
||||
|
||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
keys = env_keys(); /* rb_secure(4); */
|
||||
for (i=0; i<RARRAY_LEN(keys); i++) {
|
||||
VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
|
||||
|
@ -2694,7 +2712,7 @@ env_reject_bang(VALUE ehash)
|
|||
static VALUE
|
||||
env_delete_if(VALUE ehash)
|
||||
{
|
||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
env_reject_bang(ehash);
|
||||
return envtbl;
|
||||
}
|
||||
|
@ -2735,7 +2753,7 @@ env_select(VALUE ehash)
|
|||
VALUE result;
|
||||
char **env;
|
||||
|
||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
rb_secure(4);
|
||||
result = rb_hash_new();
|
||||
env = GET_ENVIRON(environ);
|
||||
|
@ -2769,7 +2787,7 @@ env_select_bang(VALUE ehash)
|
|||
long i;
|
||||
int del = 0;
|
||||
|
||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
keys = env_keys(); /* rb_secure(4); */
|
||||
for (i=0; i<RARRAY_LEN(keys); i++) {
|
||||
VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
|
||||
|
@ -2797,7 +2815,7 @@ env_select_bang(VALUE ehash)
|
|||
static VALUE
|
||||
env_keep_if(VALUE ehash)
|
||||
{
|
||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
env_select_bang(ehash);
|
||||
return envtbl;
|
||||
}
|
||||
|
|
|
@ -450,6 +450,12 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_size_for_enum_created_from_env
|
||||
%i[each_pair reject! delete_if select select! keep_if].each do |method|
|
||||
assert_equal ENV.size, ENV.send(method).size
|
||||
end
|
||||
end
|
||||
|
||||
def check_consistency_for_combinatorics(method)
|
||||
[ [], [:a, :b, :c, :d, :e] ].product([-2, 0, 2, 5, 6]) do |array, arg|
|
||||
assert_equal array.send(method, arg).to_a.size, array.send(method, arg).size,
|
||||
|
|
Загрузка…
Ссылка в новой задаче