Sha256: 7f0c4a72be5157ff39d30641bbd8579fb1d00b293c5bde8270ae148af6376d40

Contents?: true

Size: 972 Bytes

Versions: 10

Compression:

Stored size: 972 Bytes

Contents

require 'droiuby/support/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

10 entries across 10 versions & 1 rubygems

Version Path
droiuby-0.3.6 lib/droiuby/support/object/deep_dup.rb
droiuby-0.3.5 lib/droiuby/support/object/deep_dup.rb
droiuby-0.3.4 lib/droiuby/support/object/deep_dup.rb
droiuby-0.3.3 lib/droiuby/support/object/deep_dup.rb
droiuby-0.3.2 lib/droiuby/support/object/deep_dup.rb
droiuby-0.3.1 lib/droiuby/support/object/deep_dup.rb
droiuby-0.3.0 lib/droiuby/support/object/deep_dup.rb
droiuby-0.2.9 lib/droiuby/support/object/deep_dup.rb
droiuby-0.2.8 lib/droiuby/support/object/deep_dup.rb
droiuby-0.2.7 lib/droiuby/support/object/deep_dup.rb