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 10 10: def deep_copy(h = {}) 11: # Prevent recursion. If we reach the 12: # object we started with, stop copying. 13: copy = h[__id__] 14: unless copy 15: h[__id__] = copy = self.class.allocate 16: ivars = instance_variables 17: ivars.each do |ivar| 18: ivalue = instance_variable_get(ivar) 19: cvalue = case 20: when NilClass === ivalue, Symbol === ivalue, Float === ivalue, 21: Fixnum === ivalue, FalseClass === ivalue, TrueClass === ivalue 22: ivalue 23: when ivalue.respond_to?(:deep_copy) 24: ivalue.deep_copy(h) 25: when ivalue.respond_to?(:dup) 26: ivalue.dup 27: else 28: ivalue 29: end 30: copy.instance_variable_set(ivar, cvalue) 31: end 32: copy.freeze if frozen? 33: end 34: return copy 35: end