Inversion::

DataUtilities

module

A collection of data-manipulation functions.

Public Instance Methods

anchor
deep_copy( obj )

Recursively copy the specified obj and return the result.

# File lib/inversion/mixins.rb, line 209
def deep_copy( obj )
        # self.log.debug "Deep copying: %p" % [ obj ]

        # Handle mocks during testing
        return obj if obj.class.name == 'RSpec::Mocks::Mock'

        return case obj
                when NilClass, Numeric, TrueClass, FalseClass, Symbol,
                     Module, Encoding, IO, Tempfile
                        obj

                when Array
                        obj.map {|o| deep_copy(o) }

                when Hash
                        newhash = {}
                        newhash.default_proc = obj.default_proc if obj.default_proc
                        obj.each do |k,v|
                                newhash[ deep_copy(k) ] = deep_copy( v )
                        end
                        newhash

                else
                        obj.clone
                end
end