diff options
author | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2019-11-18 08:58:27 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-18 08:58:27 -0500 |
commit | 4f014e7fedad93fa2070166051947070cc8ac01c (patch) | |
tree | eaae01e45f8210a4d53753bb39a4f002f869b4d1 /lib/hashie/utils.rb | |
parent | 641bafcb44bd164c33ec6a9817ae6b79c3741491 (diff) | |
parent | 72d969260fadea6047a29eb00099b8e88f689fb9 (diff) | |
download | hashie-4f014e7fedad93fa2070166051947070cc8ac01c.tar.gz |
Merge pull request #467 from michaelherold/deep-merge-bug
Prevent deep_merge from mutating nested hashes
Diffstat (limited to 'lib/hashie/utils.rb')
-rw-r--r-- | lib/hashie/utils.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/hashie/utils.rb b/lib/hashie/utils.rb index d8e05fe..5b55b9a 100644 --- a/lib/hashie/utils.rb +++ b/lib/hashie/utils.rb @@ -12,5 +12,33 @@ module Hashie "defined in #{bound_method.owner}" end end + + # Duplicates a value or returns the value when it is not duplicable + # + # @api public + # + # @param value [Object] the value to safely duplicate + # @return [Object] the duplicated value + def self.safe_dup(value) + case value + when Complex, FalseClass, NilClass, Rational, Method, Symbol, TrueClass, *integer_classes + value + else + value.dup + end + end + + # Lists the classes Ruby uses for integers + # + # @api private + # @return [Array<Class>] + def self.integer_classes + @integer_classes ||= + if const_defined?(:Fixnum) + [Fixnum, Bignum] # rubocop:disable Lint/UnifiedInteger + else + [Integer] + end + end end end |