Module: TTY::Option::DeepDup Private

Defined in:
lib/tty/option/deep_dup.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Responsible for deep copying an object

Constant Summary collapse

NONDUPLICATABLE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  Symbol, TrueClass, FalseClass, NilClass, Numeric, Method, UnboundMethod
].freeze

Class Method Summary collapse

Class Method Details

.deep_dup(object, cache = {}) ⇒ Object

Deep copy an object

Examples:

DeepDeup.deep_dup({foo: {bar: [1, 2]}})

Parameters:

  • object (Object)

    the object to deep copy

  • cache (Hash) (defaults to: {})

    the cache of copied objects

Returns:

  • (Object)


26
27
28
29
30
31
32
33
34
# File 'lib/tty/option/deep_dup.rb', line 26

def self.deep_dup(object, cache = {})
  cache[object.object_id] ||=
    case object
    when *NONDUPLICATABLE then object
    when Array then deep_dup_array(object, cache)
    when Hash  then deep_dup_hash(object, cache)
    else object.dup
    end
end