module Magick::RVG::Duplicatable

This is a standard #deep_copy method that is used in most classes. Thanks to Robert Klemme.

Public Instance Methods

deep_copy(h = {}) click to toggle source
# File lib/rvg/misc.rb, line 8
def deep_copy(h = {})
  # Prevent recursion. If we reach the
  # object we started with, stop copying.
  copy = h[__id__]
  unless copy
    h[__id__] = copy = self.class.allocate
    ivars = instance_variables
    ivars.each do |ivar|
      ivalue = instance_variable_get(ivar)
      cvalue = case
        when NilClass === ivalue, Symbol === ivalue, Float === ivalue,
           Fixnum === ivalue, FalseClass === ivalue, TrueClass === ivalue
          ivalue
        when ivalue.respond_to?(:deep_copy)
          ivalue.deep_copy(h)
        when ivalue.respond_to?(:dup)
          ivalue.dup
        else
          ivalue
        end
      copy.instance_variable_set(ivar, cvalue)
    end
    copy.freeze if frozen?
  end
  copy
end