| Module | Magick::RVG::Duplicatable |
| In: |
lib/rvg/misc.rb
|
This is a standard deep_copy method that is used in most classes. Thanks to Robert Klemme.
# File lib/rvg/misc.rb, line 9
9: def deep_copy(h = {})
10: # Prevent recursion. If we reach the
11: # object we started with, stop copying.
12: copy = h[__id__]
13: unless copy
14: h[__id__] = copy = self.class.allocate
15: ivars = instance_variables
16: ivars.each do |ivar|
17: ivalue = instance_variable_get(ivar)
18: cvalue = case
19: when NilClass === ivalue, Symbol === ivalue, Float === ivalue,
20: Fixnum === ivalue, FalseClass === ivalue, TrueClass === ivalue
21: ivalue
22: when ivalue.respond_to?(:deep_copy)
23: ivalue.deep_copy(h)
24: when ivalue.respond_to?(:dup)
25: ivalue.dup
26: else
27: ivalue
28: end
29: copy.instance_variable_set(ivar, cvalue)
30: end
31: copy.freeze if frozen?
32: end
33: return copy
34: end