| Module | Cloneable |
| In: |
lib/more/facets/cloneable.rb
|
Standard basis for adding deep dup and clone to a class. Provides a class with deep cloneablity via the standard dup and clone methods.
# File lib/more/facets/cloneable.rb, line 40
40: def initialize_copy(sibling)
41: #first duplicate my superclass' state. Note that if it's duplicating
42: #instance variables, this will be overwritten, but this is important
43: #because we could be dealing with a C extension with state hidden from
44: #the Ruby interpreter
45: super
46:
47: #we want to know if we're being dup'ed or clone'd, because we want to
48: #preserve the state of our internals the same way our state is being
49: #preserved. (If we can't figure it out, we'll just use #dup.)
50: operation=caller.find{|x| x !~ /'initialize_copy'/}.
51: match(/`(dup|clone)'/)[1] or :dup
52:
53: sibling.instance_variables.each do |ivar|
54: value = sibling.instance_variable_get(ivar)
55:
56: #set my instance variable to be a #dup or #clone
57: #or my sibling, depending on what's happening to me right now
58: instance_variable_set(ivar, value.send(operation))
59: end
60: end