1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* hash.c (rb_hash_each): Hash#each should yield single value.

[ruby-talk:84420]

* hash.c (env_each): ditto for ENV.each.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4839 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2003-10-24 14:31:14 +00:00
parent f493f61f73
commit befb570536
5 changed files with 51 additions and 7 deletions

View file

@ -1,3 +1,10 @@
Fri Oct 24 23:26:34 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* hash.c (rb_hash_each): Hash#each should yield single value.
[ruby-talk:84420]
* hash.c (env_each): ditto for ENV.each.
Thu Oct 23 20:25:32 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
* lib/webrick/server.rb (GenericServer#start): should rescue

2
file.c
View file

@ -97,7 +97,7 @@ rb_file_path(obj)
{
OpenFile *fptr;
GetOpenFile(obj, fptr);
fptr = RFILE(rb_io_taint_check(obj))->fptr;
if (!fptr->path) return Qnil;
return rb_str_new2(fptr->path);
}

45
hash.c
View file

@ -671,6 +671,23 @@ rb_hash_each_pair(hash)
return hash;
}
static enum st_retval
each_i(key, value)
VALUE key, value;
{
if (key == Qundef) return ST_CONTINUE;
rb_yield(rb_assoc_new(key, value));
return ST_CONTINUE;
}
static VALUE
rb_hash_each(hash)
VALUE hash;
{
rb_hash_foreach(hash, each_i, 0);
return hash;
}
static int
to_a_i(key, value, ary)
VALUE key, value, ary;
@ -1340,8 +1357,9 @@ env_each_value(ehash)
}
static VALUE
env_each(ehash)
env_each_i(ehash, values)
VALUE ehash;
int values;
{
char **env;
VALUE ary = rb_ary_new();
@ -1359,11 +1377,30 @@ env_each(ehash)
FREE_ENVIRON(environ);
for (i=0; i<RARRAY(ary)->len; i+=2) {
rb_yield_values(2, RARRAY(ary)->ptr[i], RARRAY(ary)->ptr[i+1]);
if (values) {
rb_yield_values(2, RARRAY(ary)->ptr[i], RARRAY(ary)->ptr[i+1]);
}
else {
rb_yield(rb_assoc_new(RARRAY(ary)->ptr[i], RARRAY(ary)->ptr[i+1]));
}
}
return ehash;
}
static VALUE
env_each(ehash)
VALUE ehash;
{
return env_each_i(ehash, Qfalse);
}
static VALUE
env_each_pair(ehash)
VALUE ehash;
{
return env_each_i(ehash, Qtrue);
}
static VALUE
env_reject_bang()
{
@ -1779,7 +1816,7 @@ Init_Hash()
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_pair, 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);
@ -1819,7 +1856,7 @@ Init_Hash()
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_pair", env_each, 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);
rb_define_singleton_method(envtbl,"delete", env_delete_m, 1);

View file

@ -1027,7 +1027,7 @@ class CGI
buf = buf.sub(/\A((?:.|\n)*?)(?:#{EOL})?#{boundary}(#{EOL}|--)/n) do
body.print $1
if "--" == $2
if "--" == $2 or EOL == $2
content_length = -1
end
""

View file

@ -213,7 +213,7 @@ module DRbCore
end
def test_10_yield_undumped
@there.xarray2_hash.each do |k, v|
@there.xarray2_hash.each_pair do |k, v|
assert_kind_of(String, k)
assert_kind_of(DRbObject, v)
end