Skip to content

Commit

Permalink
Add #except under Ruby 3
Browse files Browse the repository at this point in the history
Ruby 3 adds the Hash#except method
  • Loading branch information
jackjennings committed Apr 7, 2021
1 parent ae55d8e commit f688fd1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,7 @@ language: ruby
cache: bundler

rvm:
- 3.0
- 2.6
- 2.5
- 2.4
Expand Down
7 changes: 7 additions & 0 deletions lib/hashie/extensions/indifferent_access.rb
Expand Up @@ -162,6 +162,13 @@ def slice(*keys)
end
end

with_minimum_ruby('3.0.0') do
def except(*keys)
string_keys = keys.map { |key| convert_key(key) }
slice(*self.keys - string_keys)
end
end

protected

def hash_lacking_indifference?(other)
Expand Down
7 changes: 7 additions & 0 deletions lib/hashie/mash.rb
Expand Up @@ -351,6 +351,13 @@ def transform_keys(&blk)
end
end

with_minimum_ruby('3.0.0') do
def except(*keys)
string_keys = keys.map { |key| convert_key(key) }
slice(*self.keys - string_keys)
end
end

protected

def method_name_and_suffix(method_name)
Expand Down
12 changes: 12 additions & 0 deletions spec/hashie/extensions/indifferent_access_spec.rb
Expand Up @@ -357,6 +357,18 @@ def indifferent_writer(key, value)
end
end
end

with_minimum_ruby('3.0.0') do
describe '#except' do
let(:h) { subject.build(foo: 'bar', baz: 'qux') }

it 'indifferently excepts keys from the hash' do
sliced_h = { 'baz' => 'qux' }
expect(h.except('foo')).to eq sliced_h
expect(h.except(:foo)).to eq sliced_h
end
end
end
end

describe 'with merge initializer' do
Expand Down
13 changes: 13 additions & 0 deletions spec/hashie/mash_spec.rb
Expand Up @@ -1097,4 +1097,17 @@ class SubMash < Hashie::Mash
end
end
end

with_minimum_ruby('3.0.0') do
context '#except' do
subject(:mash) { described_class.new(a: 'A', b: 'B') }
it 'return a Hashie::Mash' do
expect(mash.except(:b)).to be_kind_of(described_class)
end

it 'excludes keys' do
expect(mash.except(:b)).to eq('a' => 'A')
end
end
end
end

0 comments on commit f688fd1

Please sign in to comment.