Sha256: 8583e8eb06486274e4daf2b657c284284cc4ecd459c9fd73ffe33f8a12b7ee82

Contents?: true

Size: 1.08 KB

Versions: 65

Compression:

Stored size: 1.08 KB

Contents

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 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

65 entries across 63 versions & 9 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/activesupport-5.0.7.1/lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.7.2 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.7.1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.7 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.6 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.6.rc1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.5 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.5.rc2 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.5.rc1 lib/active_support/core_ext/object/deep_dup.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.4 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.4.rc1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-5.0.3 lib/active_support/core_ext/object/deep_dup.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb
lazy_record-0.2.1 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb
lazy_record-0.2.0 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb
lazy_record-0.1.9 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb
lazy_record-0.1.8 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb
lazy_record-0.1.7 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/object/deep_dup.rb