Class: Object

Inherits:
BasicObject
Defined in:
lib/wdd-ruby-ext/object.rb

Constant Summary collapse

@@__duping_registry =
{}

Instance Method Summary collapse

Instance Method Details

#deep_dupObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wdd-ruby-ext/object.rb', line 5

def deep_dup
  return @@__duping_registry[object_id] if @@__duping_registry[object_id]
  
  begin
    deep_duping_obj = dup
  rescue TypeError
    return self
  end
  deep_duping_obj.instance_variables.each do |var|
    val = deep_duping_obj.instance_variable_get(var)
    begin
      @@__duping_registry[object_id] = deep_duping_obj
      val = val.deep_dup
    rescue TypeError
      next
    ensure
      @@__duping_registry[object_id] = nil
    end
    deep_duping_obj.instance_variable_set(var, val)
  end
  deep_duping_obj
end