Project

General

Profile

Bug #16173 ยป diff.txt

 
1
diff --git a/hash.c b/hash.c
2
index e9d994bf70..191bbe4955 100644
3
--- a/hash.c
4
+++ b/hash.c
5
@@ -4766,12 +4766,21 @@ env_delete(VALUE name)
6
 
7
 /*
8
  * call-seq:
9
- *   ENV.delete(name)                  -> value
10
- *   ENV.delete(name) { |name| block } -> value
11
- *
12
- * Deletes the environment variable with +name+ and returns the value of the
13
- * variable.  If a block is given it will be called when the named environment
14
- * does not exist.
15
+ *   ENV.delete(name)                          -> value
16
+ *   ENV.delete(name) { |name| block }         -> value
17
+ *   ENV.delete(missing_name)                  -> nil
18
+ *   ENV.delete(missing_name) { |name| block } -> nil
19
+ *
20
+ * Deletes the environment variable for +name+ if it exists (ignoring the block, if given); returns +nil+:
21
+ *   ENV.delete('LINES') # => '300'
22
+ *   ENV.delete('COLUMNS') { |name| fail 'boo' } # => '120'
23
+ * Returns +nil+ if the environment variable does not exist and block not given:
24
+ *   ENV.delete('NOSUCH') # => nil
25
+ * Calls the block and returns +nil+ if the environment variable does not exist and block given:
26
+ *   ENV.delete('NOSUCH') { |name| } # => nil
27
+ *
28
+ * Raises TypeError if +name+ is not a +String+:
29
+ *   ENV.delete(1) # => TypeError raised
30
  */
31
 static VALUE
32
 env_delete_m(VALUE obj, VALUE name)
33
diff --git a/spec/ruby/core/env/delete_spec.rb b/spec/ruby/core/env/delete_spec.rb
34
index 1e677fb252..eb85d26918 100644
35
--- a/spec/ruby/core/env/delete_spec.rb
36
+++ b/spec/ruby/core/env/delete_spec.rb
37
@@ -16,9 +16,25 @@
38
     ENV.delete("foo").should == "bar"
39
   end
40
 
41
+  it "ignores the block if the environment variable exists" do
42
+    ENV["foo"] = "bar"
43
+    begin
44
+      -> { ENV.delete("foo") { |name| fail name } }.should_not raise_error(RuntimeError)
45
+    end
46
+  end
47
+
48
   it "yields the name to the given block if the named environment variable does not exist" do
49
     ENV.delete("foo")
50
     ENV.delete("foo") { |name| ScratchPad.record name }
51
     ScratchPad.recorded.should == "foo"
52
   end
53
+
54
+  it "returns nil if the named environment variable does not exist and block given" do
55
+    ENV.delete("foo")
56
+    ENV.delete("foo") { |name| name }.should == nil
57
+  end
58
+
59
+  it "raises TypeError if name is not a String" do
60
+    -> { ENV.delete(1) }.should raise_error(TypeError)
61
+  end
62
 end