Sha256: 99e95c262252d087c49d449e3d620fe7759f26ea872f421ed4b40c083352c19b

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 KB

Contents

class Object
  def deep_clone
    clone = self.clone
    instance_variables.each do |name|
      value = instance_variable_get name
      clone.instance_variable_set name, value.deep_clone
    end
    clone
  end
end

Hash.class_eval do
  def deep_clone
    clone = super
    clone.clear
    each{|k, v| clone[k.deep_clone] = v.deep_clone}
    clone
  end
end

OpenObject.class_eval do
  def deep_clone
    clone = super
    clone.clear
    each{|k, v| clone[k.deep_clone] = v.deep_clone}
    clone
  end
end

Struct.class_eval do
  def deep_clone
    clone = super
    clone.clear
    each_pair{|k, v| clone[k.deep_clone] = v.deep_clone}
    clone
  end
end

Array.class_eval do
  def deep_clone
    clone = super
    clone.clear
    each{|v| clone << v.deep_clone}
    clone
  end
end

[Class, Proc, Regexp].each do |klass|
  klass.class_eval do
    def clone; self end
    alias_method :deep_clone, :clone
  end
end

[Symbol, TrueClass, FalseClass, Numeric, TrueClass, FalseClass, NilClass].each do |klass|
  klass.send :alias_method, :clone, :self
end

[String, Symbol, Range, Regexp, Time, Date].each do |klass|
  klass.send :alias_method, :deep_clone, :clone
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby_ext-0.5.9 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.8 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.7 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.6 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.5 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.4 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.3 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.2 lib/ruby_ext/core/deep_clone.rb
ruby_ext-0.5.1 lib/ruby_ext/core/deep_clone.rb