Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/thor/core_ext/hash_with_indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def fetch(key, *args)
super(convert_key(key), *args)
end

def slice(*keys)
super(*keys.map{ |key| convert_key(key) })
end

def key?(key)
super(convert_key(key))
end
Expand Down
14 changes: 14 additions & 0 deletions spec/core_ext/hash_with_indifferent_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
expect(@hash.fetch(:missing, :found)).to eq(:found)
end

it "supports slice" do
expect(@hash.slice("foo")).to eq({"foo" => "bar"})
expect(@hash.slice(:foo)).to eq({"foo" => "bar"})

expect(@hash.slice("baz")).to eq({"baz" => "bee"})
expect(@hash.slice(:baz)).to eq({"baz" => "bee"})

expect(@hash.slice("foo", "baz")).to eq({"foo" => "bar", "baz" => "bee"})
expect(@hash.slice(:foo, :baz)).to eq({"foo" => "bar", "baz" => "bee"})

expect(@hash.slice("missing")).to eq({})
expect(@hash.slice(:missing)).to eq({})
end

it "has key checkable by either strings or symbols" do
expect(@hash.key?("foo")).to be true
expect(@hash.key?(:foo)).to be true
Expand Down