Skip to content

Commit d8b073c

Browse files
authored
Merge pull request #812 from shuuuuun/hash-support-slice
Support `Thor::CoreExt::HashWithIndifferentAccess#slice` method
2 parents c3f77d5 + 4bd417b commit d8b073c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
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
@@ -38,6 +38,10 @@ def fetch(key, *args)
3838
super(convert_key(key), *args)
3939
end
4040

41+
def slice(*keys)
42+
super(*keys.map{ |key| convert_key(key) })
43+
end
44+
4145
def key?(key)
4246
super(convert_key(key))
4347
end

spec/core_ext/hash_with_indifferent_access_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@
4040
expect(@hash.fetch(:missing, :found)).to eq(:found)
4141
end
4242

43+
it "supports slice" do
44+
expect(@hash.slice("foo")).to eq({"foo" => "bar"})
45+
expect(@hash.slice(:foo)).to eq({"foo" => "bar"})
46+
47+
expect(@hash.slice("baz")).to eq({"baz" => "bee"})
48+
expect(@hash.slice(:baz)).to eq({"baz" => "bee"})
49+
50+
expect(@hash.slice("foo", "baz")).to eq({"foo" => "bar", "baz" => "bee"})
51+
expect(@hash.slice(:foo, :baz)).to eq({"foo" => "bar", "baz" => "bee"})
52+
53+
expect(@hash.slice("missing")).to eq({})
54+
expect(@hash.slice(:missing)).to eq({})
55+
end
56+
4357
it "has key checkable by either strings or symbols" do
4458
expect(@hash.key?("foo")).to be true
4559
expect(@hash.key?(:foo)).to be true

0 commit comments

Comments
 (0)