mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
ENV.delete should return the result of block on non-existing key
Fixes [Bug #16173] Co-Authored-By: Burdette Lamar <burdettelamar@yahoo.com> Co-Authored-By: Jeremy Evans <code@jeremyevans.net>
This commit is contained in:
parent
aab2a7cfce
commit
04fddf3573
Notes:
git
2020-06-11 04:49:54 +09:00
3 changed files with 15 additions and 6 deletions
13
hash.c
13
hash.c
|
@ -5538,8 +5538,10 @@ env_delete(VALUE name)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* ENV.delete(name) -> value
|
* ENV.delete(name) -> value
|
||||||
* ENV.delete(name) { |name| block } -> value
|
* ENV.delete(name) { |name| block } -> value
|
||||||
|
* ENV.delete(missing_name) -> nil
|
||||||
|
* ENV.delete(missing_name) { |name| block } -> block_value
|
||||||
*
|
*
|
||||||
* Deletes the environment variable with +name+ if it exists and returns its value:
|
* Deletes the environment variable with +name+ if it exists and returns its value:
|
||||||
* ENV['foo'] = '0'
|
* ENV['foo'] = '0'
|
||||||
|
@ -5547,9 +5549,8 @@ env_delete(VALUE name)
|
||||||
* Returns +nil+ if the named environment variable does not exist:
|
* Returns +nil+ if the named environment variable does not exist:
|
||||||
* ENV.delete('foo') # => nil
|
* ENV.delete('foo') # => nil
|
||||||
* If a block given and the environment variable does not exist,
|
* If a block given and the environment variable does not exist,
|
||||||
* yields +name+ to the block and returns +nil+:
|
* yields +name+ to the block and returns the value of the block:
|
||||||
* ENV.delete('foo') { |name| puts name } # => nil
|
* ENV.delete('foo') { |name| name * 2 } # => "foofoo"
|
||||||
* foo
|
|
||||||
* If a block given and the environment variable exists,
|
* If a block given and the environment variable exists,
|
||||||
* deletes the environment variable and returns its value (ignoring the block):
|
* deletes the environment variable and returns its value (ignoring the block):
|
||||||
* ENV['foo'] = '0'
|
* ENV['foo'] = '0'
|
||||||
|
@ -5563,7 +5564,7 @@ env_delete_m(VALUE obj, VALUE name)
|
||||||
VALUE val;
|
VALUE val;
|
||||||
|
|
||||||
val = env_delete(name);
|
val = env_delete(name);
|
||||||
if (NIL_P(val) && rb_block_given_p()) rb_yield(name);
|
if (NIL_P(val) && rb_block_given_p()) val = rb_yield(name);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
7
spec/ruby/core/env/delete_spec.rb
vendored
7
spec/ruby/core/env/delete_spec.rb
vendored
|
@ -30,6 +30,13 @@ describe "ENV.delete" do
|
||||||
ScratchPad.recorded.should == "foo"
|
ScratchPad.recorded.should == "foo"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ruby_version_is "2.8" do
|
||||||
|
it "returns the result of given block if the named environment variable does not exist" do
|
||||||
|
ENV.delete("foo")
|
||||||
|
ENV.delete("foo") { |name| "bar" }.should == "bar"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "does not evaluate the block if the environment variable exists" do
|
it "does not evaluate the block if the environment variable exists" do
|
||||||
ENV["foo"] = "bar"
|
ENV["foo"] = "bar"
|
||||||
ENV.delete("foo") { |name| fail "Should not happen" }
|
ENV.delete("foo") { |name| fail "Should not happen" }
|
||||||
|
|
|
@ -106,6 +106,7 @@ class TestEnv < Test::Unit::TestCase
|
||||||
assert_invalid_env {|v| ENV.delete(v)}
|
assert_invalid_env {|v| ENV.delete(v)}
|
||||||
assert_nil(ENV.delete("TEST"))
|
assert_nil(ENV.delete("TEST"))
|
||||||
assert_nothing_raised { ENV.delete(PATH_ENV) }
|
assert_nothing_raised { ENV.delete(PATH_ENV) }
|
||||||
|
assert_equal("NO TEST", ENV.delete("TEST") {|name| "NO "+name})
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_getenv
|
def test_getenv
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue