diff --git a/hash.c b/hash.c index e9d994bf70..191bbe4955 100644 --- a/hash.c +++ b/hash.c @@ -4766,12 +4766,21 @@ env_delete(VALUE name) /* * call-seq: - * ENV.delete(name) -> value - * ENV.delete(name) { |name| block } -> value - * - * Deletes the environment variable with +name+ and returns the value of the - * variable. If a block is given it will be called when the named environment - * does not exist. + * ENV.delete(name) -> value + * ENV.delete(name) { |name| block } -> value + * ENV.delete(missing_name) -> nil + * ENV.delete(missing_name) { |name| block } -> nil + * + * Deletes the environment variable for +name+ if it exists (ignoring the block, if given); returns +nil+: + * ENV.delete('LINES') # => '300' + * ENV.delete('COLUMNS') { |name| fail 'boo' } # => '120' + * Returns +nil+ if the environment variable does not exist and block not given: + * ENV.delete('NOSUCH') # => nil + * Calls the block and returns +nil+ if the environment variable does not exist and block given: + * ENV.delete('NOSUCH') { |name| } # => nil + * + * Raises TypeError if +name+ is not a +String+: + * ENV.delete(1) # => TypeError raised */ static VALUE env_delete_m(VALUE obj, VALUE name) diff --git a/spec/ruby/core/env/delete_spec.rb b/spec/ruby/core/env/delete_spec.rb index 1e677fb252..eb85d26918 100644 --- a/spec/ruby/core/env/delete_spec.rb +++ b/spec/ruby/core/env/delete_spec.rb @@ -16,9 +16,25 @@ ENV.delete("foo").should == "bar" end + it "ignores the block if the environment variable exists" do + ENV["foo"] = "bar" + begin + -> { ENV.delete("foo") { |name| fail name } }.should_not raise_error(RuntimeError) + end + end + it "yields the name to the given block if the named environment variable does not exist" do ENV.delete("foo") ENV.delete("foo") { |name| ScratchPad.record name } ScratchPad.recorded.should == "foo" end + + it "returns nil if the named environment variable does not exist and block given" do + ENV.delete("foo") + ENV.delete("foo") { |name| name }.should == nil + end + + it "raises TypeError if name is not a String" do + -> { ENV.delete(1) }.should raise_error(TypeError) + end end