summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby McDonald <BobbyMcWho@users.noreply.github.com>2020-01-13 16:22:07 -0500
committerGitHub <noreply@github.com>2020-01-13 16:22:07 -0500
commitd2071d82a2ceb2513bc0ff9c827ab3bc2a80b072 (patch)
tree16d8e19f6f07ad6d2e8662ac270d8f5de5affe02
parenta1fa33d4259079f434b6d0820562e670d446582f (diff)
parent6f79eb82f66952451dd972fbee08df169e2cfba6 (diff)
downloadhashie-d2071d82a2ceb2513bc0ff9c827ab3bc2a80b072.tar.gz
Merge pull request #510 from BobbyMcWho/fix-compact-definition
Only define compact on ruby >= 2.4
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/hashie/mash.rb12
-rw-r--r--spec/hashie/mash_spec.rb42
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