Module: Tins::DeepDup

Included in:
Object
Defined in:
lib/tins/deep_dup.rb

Overview

DeepDup module provides a method to deeply duplicate objects in Ruby.

This module extends the Object class with a deep_dup method that creates a recursive copy of an object and all its nested objects.

Instance Method Summary collapse

Instance Method Details

#deep_dupObject

Duplicates an object deeply by marshaling and unmarshaling it. For objects that can’t be marshaled, it returns the object itself.

it can’t be marshaled

Returns:

  • (Object)

    a deep duplicate of the object or the object itself if



12
13
14
15
16
# File 'lib/tins/deep_dup.rb', line 12

def deep_dup
  Marshal.load(Marshal.dump(self))
rescue TypeError
  return self
end