summaryrefslogtreecommitdiff
path: root/spec/ruby/core/hash/delete_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/hash/delete_spec.rb')
-rw-r--r--spec/ruby/core/hash/delete_spec.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/spec/ruby/core/hash/delete_spec.rb b/spec/ruby/core/hash/delete_spec.rb
new file mode 100644
index 0000000000..33ff01bf36
--- /dev/null
+++ b/spec/ruby/core/hash/delete_spec.rb
@@ -0,0 +1,58 @@
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+
+describe "Hash#delete" do
+ it "removes the entry and returns the deleted value" do
+ h = { a: 5, b: 2 }
+ h.delete(:b).should == 2
+ h.should == { a: 5 }
+ end
+
+ it "calls supplied block if the key is not found" do
+ { a: 1, b: 10, c: 100 }.delete(:d) { 5 }.should == 5
+ Hash.new(:default).delete(:d) { 5 }.should == 5
+ Hash.new { :default }.delete(:d) { 5 }.should == 5
+ end
+
+ it "returns nil if the key is not found when no block is given" do
+ { a: 1, b: 10, c: 100 }.delete(:d).should == nil
+ Hash.new(:default).delete(:d).should == nil
+ Hash.new { :default }.delete(:d).should == nil
+ end
+
+ # MRI explicitly implements this behavior
+ it "allows removing a key while iterating" do
+ h = { a: 1, b: 2 }
+ visited = []
+ h.each_pair { |k, v|
+ visited << k
+ h.delete(k)
+ }
+ visited.should == [:a, :b]
+ h.should == {}
+ end
+
+ it "allows removing a key while iterating for big hashes" do
+ h = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10,
+ k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20,
+ u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 }
+ visited = []
+ h.each_pair { |k, v|
+ visited << k
+ h.delete(k)
+ }
+ visited.should == [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m,
+ :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z]
+ h.should == {}
+ end
+
+ it "accepts keys with private #hash method" do
+ key = HashSpecs::KeyWithPrivateHash.new
+ { key => 5 }.delete(key).should == 5
+ end
+
+ it "raises a FrozenError if called on a frozen instance" do
+ -> { HashSpecs.frozen_hash.delete("foo") }.should.raise(FrozenError)
+ -> { HashSpecs.empty_frozen_hash.delete("foo") }.should.raise(FrozenError)
+ end
+end
td>set svn:eol-styleshyouhei19 years v1_1devset svn:eol-styleshyouhei19 years v1_1rset svn:eol-styleshyouhei19 years  TagDownloadAuthorAge v4.0.4commit b89eb1bcbf...Takashi Kokubun3 weeks v4.0.3commit 85ddef263a...Takashi Kokubun6 weeks v3_2_11commit 5483bfc1ae...Hiroshi SHIBATA2 months v3_3_11commit 1f2d15125a...Hiroshi SHIBATA2 months v4.0.2commit d3da9fec82...Takashi Kokubun3 months v3_4_9commit 76cca827ab...nagachika3 months v3_2_10commit a3a6d25788...Hiroshi SHIBATA5 months v4.0.1commit e04267a14b...Takashi Kokubun5 months v4.0.0commit 553f1675f3...NARUSE, Yui5 months v4.0.0-preview3commit cfa3e7cf75...Augustin Gottlieb6 months v3_4_8commit 995b59f666...Takashi Kokubun6 months v4.0.0-preview2commit 4fa6e9938c...NARUSE, Yui7 months v3_3_10commit 343ea05002...nagachika7 months v3_4_7commit 7a5688e2a2...Takashi Kokubun8 months v3_4_6commit dbd83256b1...Takashi Kokubun9 months v3_3_9commit f5c772fc7c...nagachika10 months v3_2_9commit 8f611e0c46...Hiroshi SHIBATA10 months v3_4_5commit 20cda200d3...Takashi Kokubun11 months v3_4_4commit a38531fd3f...Takashi Kokubun13 months v3_5_0_preview1commit d06ec25be4...Takashi Kokubun14 months v3_4_3commit d0b7e5b6a0...Takashi Kokubun14 months v3_3_8commit b200bad6cd...nagachika14 months v3_1_7commit 0a3704f218...Hiroshi SHIBATA14 months v3_2_8commit 13f495dc2c...Hiroshi SHIBATA14 months v3_4_2commit d2930f8e7a...Takashi Kokubun16 months v3_2_7commit 02ec315244...nagachika16 months v3_3_7commit be31f993d7...Takashi Kokubun17 months v3_4_1commit 48d4efcb85...NARUSE, Yui17 months v3_4_0commit f450108330...Takashi Kokubun17 months v3_4_0_rc1commit 29caae9991...Kevin Newton18 months v3_3_6commit 75015d4c1f...Takashi Kokubun19 months v3_2_6commit 63aeb018eb...nagachika19 months v3_4_0_preview2commit 32c733f57b...Misaki Shioi20 months v3_3_5commit ef084cc8f4...Takashi Kokubun21 months v3_2_5commit 31d0f1a2e7...nagachika22 months v3_3_4commit be1089c8ec...Takashi Kokubun23 months v3_3_3commit f1c7b6f435...Takashi Kokubun24 months v3_3_2commit e5a195edf6...Takashi Kokubun2 years v3_1_6commit a777087be6...Hiroshi SHIBATA2 years v3_4_0_preview1commit 9d69619623...Nobuyoshi Nakada2 years v3_0_7commit 724a071175...Hiroshi SHIBATA2 years v3_1_5commit 1945f8dc0e...Hiroshi SHIBATA2 years v3_3_1commit c56cd86388...NARUSE, Yui2 years v3_2_4commit af471c0e01...nagachika2 years v3_2_3commit 52bb2ac0a6...nagachika2 years v3_3_0commit 5124f9ac75...NARUSE, Yui2 years v3_3_0_rc1commit a49643340e...NARUSE, Yui2 years v3_3_0_preview3commit 60e19a0b5f...Nobuyoshi Nakada3 years v3_3_0_preview2commit e50fcca9a7...Nathan Froyd3 years v3_3_0_preview1commit a1b01e7701...Yuichiro Kaneko3 years v3_0_6commit 23a532679b...NAKAMURA Usaku3 years v2_7_8commit 1f4d455848...NAKAMURA Usaku3 years