Skip to content

Commit f0c2166

Browse files
justincampbellsferik
authored andcommitted
Add #fetch to HashWithIndifferentAccess
1 parent 5279d59 commit f0c2166

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/thor/core_ext/hash_with_indifferent_access.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def delete(key)
2828
super(convert_key(key))
2929
end
3030

31+
def fetch(key, *args)
32+
super(convert_key(key), *args)
33+
end
34+
3135
def key?(key)
3236
super(convert_key(key))
3337
end

spec/core_ext/hash_with_indifferent_access_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
expect(@hash.delete(:foo)).to eq("bar")
1515
end
1616

17+
it "supports fetch" do
18+
expect(@hash.fetch("foo")).to eq("bar")
19+
expect(@hash.fetch("foo", nil)).to eq("bar")
20+
expect(@hash.fetch(:foo)).to eq("bar")
21+
expect(@hash.fetch(:foo, nil)).to eq("bar")
22+
end
23+
1724
it "has key checkable by either strings or symbols" do
1825
expect(@hash.key?("foo")).to be true
1926
expect(@hash.key?(:foo)).to be true
20-
2127
expect(@hash.key?("nothing")).to be false
2228
expect(@hash.key?(:nothing)).to be false
2329
end

0 commit comments

Comments
 (0)