Sha256: c00cd1b38841a62769d54f67b25c24abdd19096cefb7b317d35aa534fc6d6122

Contents?: true

Size: 986 Bytes

Versions: 102

Compression:

Stored size: 986 Bytes

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 { |it| it.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
    each_with_object(dup) do |(key, value), hash|
      hash[key.deep_dup] = value.deep_dup
    end
  end
end

Version data entries

102 entries across 97 versions & 12 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.11.2 lib/active_support/core_ext/object/deep_dup.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/object/deep_dup.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.11.1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.11 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.10 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.10.rc1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.9 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.9.rc2 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.9.rc1 lib/active_support/core_ext/object/deep_dup.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.8 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.8.rc1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.7.1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.7 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.1.16 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.1.16.rc1 lib/active_support/core_ext/object/deep_dup.rb
activesupport-4.2.7.rc1 lib/active_support/core_ext/object/deep_dup.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/core_ext/object/deep_dup.rb