Sha256: 19ad8656d709407aa0fd80d27656b30e6b401a8aaa729e59d8d14c7e696b1fdc

Contents?: true

Size: 1.11 KB

Versions: 48

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/object/duplicable"

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 ::String === key || ::Symbol === 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

48 entries across 45 versions & 9 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.8.7/lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.8.7 lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.8.6 lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.8.5 lib/active_support/core_ext/object/deep_dup.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/core_ext/object/deep_dup.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/object/deep_dup.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/object/deep_dup.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.8.4 lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.8.1 lib/active_support/core_ext/object/deep_dup.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/core_ext/object/deep_dup.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.8 lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.7.2 lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.7.1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.7 lib/active_support/core_ext/object/deep_dup.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/core_ext/object/deep_dup.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/core_ext/object/deep_dup.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/core_ext/object/deep_dup.rb
activesupport-7.0.6 lib/active_support/core_ext/object/deep_dup.rb