Class: Object

Inherits:
BasicObject
Defined in:
lib/api/object_additions.rb

Overview

Instance Method Summary collapse

Instance Method Details

#deep_clone(cache = {}) ⇒ Object

def deep_clone

  return @deep_cloning_obj if @deep_cloning

  @deep_cloning_obj = clone
  @deep_cloning_obj.instance_variables.each do |var|
    val = @deep_cloning_obj.instance_variable_get(var)
    begin
      @deep_cloning = true
      val = val.deep_clone
    rescue TypeError
      next
    ensure
      @deep_cloning = false
    end
    @deep_cloning_obj.instance_variable_set(var, val)
  end
  deep_cloning_obj = @deep_cloning_obj
  @deep_cloning_obj = nil
  deep_cloning_obj
end


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/api/object_additions.rb', line 26

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

  copy = clone()
  cache[self] = copy

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

  return copy
end