Module: Kernel

Defined in:
lib/magnum-pi/core_ext/deep_clone.rb

Instance Method Summary collapse

Instance Method Details

#deep_clone(cache = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/magnum-pi/core_ext/deep_clone.rb', line 3

def deep_clone(cache = {})
  return cache[self] if cache.key?(self)

  begin
    copy = clone
  rescue
    return cache[self] = self
  end

  cache[self] = copy

  copy.instance_variables.each do |name|
    begin
      var = instance_variable_get(name).deep_clone(cache)
      copy.instance_variable_set name, var
    rescue TypeError
    end
  end

  copy
end