ruby-changes:59000
From: Nobuyoshi <ko1@a...>
Date: Sun, 1 Dec 2019 02:24:57 +0900 (JST)
Subject: [ruby-changes:59000] 5e0479f26a (master): ENV.update should not call block on existing keys
https://git.ruby-lang.org/ruby.git/commit/?id=5e0479f26a From 5e0479f26afe1505afd9014ea96a206a88845828 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Tue, 1 Oct 2019 12:23:25 +0900 Subject: ENV.update should not call block on existing keys [Bug #16192] diff --git a/hash.c b/hash.c index e11afb0..6b3f844 100644 --- a/hash.c +++ b/hash.c @@ -5931,8 +5931,16 @@ env_replace(VALUE env, VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L5931 static int env_update_i(VALUE key, VALUE val, VALUE _) { - if (rb_block_given_p()) { - val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val); + env_aset(key, val); + return ST_CONTINUE; +} + +static int +env_update_block_i(VALUE key, VALUE val, VALUE _) +{ + VALUE oldval = rb_f_getenv(Qnil, key); + if (!NIL_P(oldval)) { + val = rb_yield_values(3, key, oldval, val); } env_aset(key, val); return ST_CONTINUE; @@ -5955,7 +5963,9 @@ env_update(VALUE env, VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L5963 { if (env == hash) return env; hash = to_hash(hash); - rb_hash_foreach(hash, env_update_i, 0); + rb_foreach_func *func = rb_block_given_p() ? + env_update_block_i : env_update_i; + rb_hash_foreach(hash, func, 0); return env; } diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index d9301ff..ffce9b7 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -445,7 +445,7 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L445 ENV.clear ENV["foo"] = "bar" ENV["baz"] = "qux" - ENV.update({"baz"=>"quux","a"=>"b"}) {|k, v1, v2| v1 ? k + "_" + v1 + "_" + v2 : v2 } + ENV.update({"baz"=>"quux","a"=>"b"}) {|k, v1, v2| k + "_" + v1 + "_" + v2 } check(ENV.to_hash.to_a, [%w(foo bar), %w(baz baz_qux_quux), %w(a b)]) end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/