Sha256: 4c4bebe2ce522de1a10495f04c810ce59b662fa2ca9a1fcd6d369a515c77fd87

Contents?: true

Size: 1.2 KB

Versions: 34

Compression:

Stored size: 1.2 KB

Contents

require 'sentry/core_ext/object/duplicable'

#########################################
#  This file was copied from Rails 5.2  #
#########################################

class Object
  # Returns a deep copy of object if it's duplicable. If it's
  # not duplicable, returns +self+.
  #
  #   object = Object.new
  #   dup    = object.deep_dup
  #   dup.instance_variable_set(:@a, 1)
  #
  #   object.instance_variable_defined?(:@a) # => false
  #   dup.instance_variable_defined?(:@a)    # => true
  def deep_dup
    duplicable? ? dup : self
  end
end

class Array
  # Returns a deep copy of array.
  #
  #   array = [1, [2, 3]]
  #   dup   = array.deep_dup
  #   dup[1][2] = 4
  #
  #   array[1][2] # => nil
  #   dup[1][2]   # => 4
  def deep_dup
    map(&:deep_dup)
  end
end

class Hash
  # Returns a deep copy of hash.
  #
  #   hash = { a: { b: 'b' } }
  #   dup  = hash.deep_dup
  #   dup[:a][:c] = 'c'
  #
  #   hash[:a][:c] # => nil
  #   dup[:a][:c]  # => "c"
  def deep_dup
    hash = dup
    each_pair do |key, value|
      if key.frozen? && ::String === key
        hash[key] = value.deep_dup
      else
        hash.delete(key)
        hash[key.deep_dup] = value.deep_dup
      end
    end
    hash
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
sentry-ruby-core-4.6.2 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.6.1 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.6.0 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.6.0.pre.beta.0 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.5.2 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.5.1 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.5.0 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.5.0.pre.beta.1 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.4.2 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.4.1 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.4.0 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.4.0.pre.beta.0 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.3.2 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.3.1 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.3.0 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.2.2 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.2.1 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.2.0 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.1.6 lib/sentry/core_ext/object/deep_dup.rb
sentry-ruby-core-4.1.5 lib/sentry/core_ext/object/deep_dup.rb