Module: Surrealist::Copier

Defined in:
lib/surrealist/copier.rb

Overview

A helper class for deep copying and wrapping hashes.

Class Method Summary collapse

Class Method Details

.deep_copy(hash, wrapper = {}) ⇒ Hash

Goes through the hash recursively and deeply copies it.

Parameters:

  • hash (Hash)

    the hash to be copied.

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

    the wrapper of the resulting hash.

Returns:

  • (Hash)

    deeply copied hash.



13
14
15
16
17
# File 'lib/surrealist/copier.rb', line 13

def deep_copy(hash, wrapper = {})
  hash.each_with_object(wrapper) do |(key, value), new|
    new[key] = value.is_a?(Hash) ? deep_copy(value) : value
  end
end