Class: Object

Inherits:
BasicObject
Includes:
AndAnd::ObjectGoodies
Defined in:
lib/patches/andand.rb,
lib/patches/object_ext.rb

Instance Method Summary collapse

Methods included from AndAnd::ObjectGoodies

#andand, #me

Instance Method Details

#deep_copyObject

Make a deep copy of an object as safely as possible. This was originally a simple Marshal::load(Marshal::dump(self)) but it turns out in Migratrix that we frequently need to copy IO streams and lambdas, neither of which can be marshaled. Then it was a simple dup, but some singleton types like Fixnum cannot be dup’ed. So now we have this monstrosity.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/patches/object_ext.rb', line 8

def deep_copy
  if is_a?(Array)
    map(&:deep_copy)
  elsif is_a?(Hash)
    Hash[to_a.map {|k,v| [k, v.deep_copy]}]
  else
    begin
      Marshal::load(Marshal::dump(self))
    rescue TypeError
      dup
    end
  end
end