diff options
author | Bobby McDonald <bobbymcwho@gmail.com> | 2020-01-12 21:22:14 -0500 |
---|---|---|
committer | Bobby McDonald <bobbymcwho@gmail.com> | 2020-01-13 15:52:04 -0500 |
commit | 6f79eb82f66952451dd972fbee08df169e2cfba6 (patch) | |
tree | 16d8e19f6f07ad6d2e8662ac270d8f5de5affe02 | |
parent | a1fa33d4259079f434b6d0820562e670d446582f (diff) | |
download | hashie-6f79eb82f66952451dd972fbee08df169e2cfba6.tar.gz |
Only define compact on ruby >= 2.4
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/mash.rb | 12 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 42 |
3 files changed, 28 insertions, 27 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 1675557..665e6c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ scheme are considered to be bugs. * [#467](https://github.com/intridea/hashie/pull/467): Fixed `DeepMerge#deep_merge` mutating nested values within the receiver - [@michaelherold](https://github.com/michaelherold). * [#505](https://github.com/hashie/hashie/pull/505): Ensure that `Hashie::Array`s are not deconverted within `Hashie::Mash`es to make `Mash#dig` work properly - [@michaelherold](https://github.com/michaelherold). * [#507](https://github.com/hashie/hashie/pull/507): Suppress `Psych.safe_load` arg warn when using Psych 3.1.0+ - [@koic](https://github.com/koic). +* [#510](https://github.com/hashie/hashie/pull/510): Ensure that `Hashie::Mash#compact` is only defined on Ruby version >= 2.4.0 - [@bobbymcwho](https://github.com/bobbymcwho). * Your contribution here. ### Security diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 97d6fa6..658aded 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -173,12 +173,6 @@ module Hashie super(*keys.map { |key| convert_key(key) }) end - # Returns a new instance of the class it was called on, with nil values - # removed. - def compact - self.class.new(super) - end - # Returns a new instance of the class it was called on, using its keys as # values, and its values as keys. The new values and keys will always be # strings. @@ -340,6 +334,12 @@ module Hashie def transform_values(&blk) self.class.new(super(&blk)) end + + # Returns a new instance of the class it was called on, with nil values + # removed. + def compact + self.class.new(super) + end end with_minimum_ruby('2.5.0') do diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index 4629124..603adc0 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -882,27 +882,6 @@ describe Hashie::Mash do end end - describe '#compact' do - subject(:mash) { described_class.new(a: 1, b: nil) } - - it 'returns a Hashie::Mash' do - expect(mash.compact).to be_kind_of(described_class) - end - - it 'removes keys with nil values' do - expect(mash.compact).to eq('a' => 1) - end - - context 'when using with subclass' do - let(:subclass) { Class.new(Hashie::Mash) } - subject(:sub_mash) { subclass.new(a: 1, b: nil) } - - it 'creates an instance of subclass' do - expect(sub_mash.compact).to be_kind_of(subclass) - end - end - end - describe '#invert' do subject(:mash) { described_class.new(a: 'apple', b: 4) } @@ -1027,6 +1006,27 @@ describe Hashie::Mash do end end end + + describe '#compact' do + subject(:mash) { described_class.new(a: 1, b: nil) } + + it 'returns a Hashie::Mash' do + expect(mash.compact).to be_kind_of(described_class) + end + + it 'removes keys with nil values' do + expect(mash.compact).to eq('a' => 1) + end + + context 'when using with subclass' do + let(:subclass) { Class.new(Hashie::Mash) } + subject(:sub_mash) { subclass.new(a: 1, b: nil) } + + it 'creates an instance of subclass' do + expect(sub_mash.compact).to be_kind_of(subclass) + end + end + end end with_minimum_ruby('2.5.0') do |