From 0dde4b4e3055ca918651f974b055e2cf7447fdba Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 18 Jan 2020 00:42:12 +0900 Subject: Suppress a Ruby's warning when using Ruby 2.6.0+ This PR suppresses the following warning that `deep_merge` method and `deep_update` method are defined twice when using Ruby 2.6.0+. ```console % bundle exec rake (snip) /Users/koic/src/github.com/hahie/hashie/lib/hashie/mash.rb:226: warning: method redefined; discarding old deep_merge /Users/koic/src/github.com/hahie/hashie/lib/hashie/mash.rb:212: warning: previous definition of deep_merge was here /Users/koic/src/github.com/hahie/hashie/lib/hashie/mash.rb:232: warning: method redefined; discarding old deep_update /Users/koic/src/github.com/hahie/hashie/lib/hashie/mash.rb:218: warning: previous definition of deep_update was here ``` --- CHANGELOG.md | 1 + lib/hashie/extensions/ruby_version_check.rb | 6 +++++- lib/hashie/mash.rb | 28 ++++++++++++++-------------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6ddaad..7f71fae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ scheme are considered to be bugs. * [#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). * [#511](https://github.com/hashie/hashie/pull/511): Suppress keyword arguments warning for Ruby 2.7.0 - [@koic](https://github.com/koic). * [#512](https://github.com/hashie/hashie/pull/512): Suppress an integer unification warning for using Ruby 2.4.0+ - [@koic](https://github.com/koic). +* [#513](https://github.com/hashie/hashie/pull/513): Suppress a Ruby's warning when using Ruby 2.6.0+ - [@koic](https://github.com/koic). * Your contribution here. ### Security diff --git a/lib/hashie/extensions/ruby_version_check.rb b/lib/hashie/extensions/ruby_version_check.rb index bcd3436..b287230 100644 --- a/lib/hashie/extensions/ruby_version_check.rb +++ b/lib/hashie/extensions/ruby_version_check.rb @@ -9,7 +9,11 @@ module Hashie module ClassMethods def with_minimum_ruby(version) - yield if RubyVersion.new(RUBY_VERSION) >= RubyVersion.new(version) + yield if with_minimum_ruby?(version) + end + + def with_minimum_ruby?(version) + RubyVersion.new(RUBY_VERSION) >= RubyVersion.new(version) end end end diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 194a6f8..96b24c9 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -207,20 +207,7 @@ module Hashie alias include? key? alias member? key? - # Performs a deep_update on a duplicate of the - # current mash. - def deep_merge(other_hash, &blk) - dup.deep_update(other_hash, &blk) - end - - # Recursively merges this mash with the passed - # in hash, merging each hash in the hierarchy. - def deep_update(other_hash, &blk) - _deep_update(other_hash, &blk) - self - end - - with_minimum_ruby('2.6.0') do + if with_minimum_ruby?('2.6.0') # Performs a deep_update on a duplicate of the # current mash. def deep_merge(*other_hashes, &blk) @@ -235,6 +222,19 @@ module Hashie end self end + else + # Performs a deep_update on a duplicate of the + # current mash. + def deep_merge(other_hash, &blk) + dup.deep_update(other_hash, &blk) + end + + # Recursively merges this mash with the passed + # in hash, merging each hash in the hierarchy. + def deep_update(other_hash, &blk) + _deep_update(other_hash, &blk) + self + end end # Alias these lexically so they get the correctly defined -- cgit v1.2.1