Class: Object

Inherits:
BasicObject
Defined in:
lib/rubymta/deepclone.rb

Instance Method Summary collapse

Instance Method Details

#deepcloneObject

deepclone not only clones the target object, but all objects inside of it, i.e., if you clone a hash of other objects, those other objects will also be cloned.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubymta/deepclone.rb', line 5

def deepclone
  case
  when self.class==Hash
    hash = {}
    self.each { |k,v| hash[k] = v.deepclone }
    hash
  when self.class==Array
    array = []
    self.each { |v| array << v.deepclone }
    array
  else
    if defined?(self.class.new)
      self.class.new(self)
    else
      self
    end
  end
end