Sha256: 75c931fbc0823e94aa18df3dbe3ea4a3c8129372e5fd7f52800ab74cbe05263e

Contents?: true

Size: 833 Bytes

Versions: 1

Compression:

Stored size: 833 Bytes

Contents

Class.class_eval do
  def deep_clone
    self
  end
end

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

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

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

Array.class_eval do
  def deep_clone
    clone = []
    each{|v| clone << v.deep_clone}
    clone
  end
end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-ext-0.4.2 lib/ruby_ext/deep_clone.rb