From 6f79eb82f66952451dd972fbee08df169e2cfba6 Mon Sep 17 00:00:00 2001 From: Bobby McDonald Date: Sun, 12 Jan 2020 21:22:14 -0500 Subject: Only define compact on ruby >= 2.4 --- CHANGELOG.md | 1 + lib/hashie/mash.rb | 12 ++++++------ 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 -- cgit v1.2.1