Skip to content
This repository
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor

Showing 2 changed files with 21 additions and 3 deletions. Show diff stats Hide diff stats

  1. +5 1 kernel/common/env.rb
  2. +16 2 spec/ruby/core/env/delete_spec.rb
6 kernel/common/env.rb
@@ -57,7 +57,11 @@ def each
57 57
58 58 def delete(key)
59 59 existing_value = self[key]
60   - self[key] = nil if existing_value
  60 + if existing_value
  61 + self[key] = nil
  62 + elsif block_given?
  63 + yield key
  64 + end
61 65 existing_value
62 66 end
63 67
18 spec/ruby/core/env/delete_spec.rb
... ... @@ -1,9 +1,23 @@
1 1 require File.expand_path('../../../spec_helper', __FILE__)
2 2
3 3 describe "ENV.delete" do
4   - it "removes the variable and return its value" do
  4 + after(:each) do
  5 + ENV.delete("foo")
  6 + end
  7 +
  8 + it "removes the variable from the environment" do
5 9 ENV["foo"] = "bar"
6   - ENV.delete("foo").should == "bar"
  10 + ENV.delete("foo")
7 11 ENV["foo"].should == nil
8 12 end
  13 +
  14 + it "returns the previous value" do
  15 + ENV["foo"] = "bar"
  16 + ENV.delete("foo").should == "bar"
  17 + end
  18 +
  19 + it "yields the name to the given block if the named environment variable does not exist" do
  20 + ENV.delete("foo") { |name| ScratchPad.record name }
  21 + ScratchPad.recorded.should == "foo"
  22 + end
9 23 end

No commit comments for this range

Something went wrong with that request. Please try again.