Method: Hash#inject_into_empty

Defined in:
lib/enumerate_hash_values/core_extensions/hash.rb

#inject_into_empty(hash_class = self.class, options = {}) ⇒ Object

Shortcut to inject into an empty hash.

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/enumerate_hash_values/core_extensions/hash.rb', line 67

def inject_into_empty(hash_class = self.class, options = {})
  raise ArgumentError.new("#{hash_class} is not a Hash") unless hash_class.ancestors.include?(Hash)

  # If not explicitly specified, copy traits into the new hash if it is of
  # the same class.
  copy_traits = options.has_key?(:copy_traits) ? options[:copy_traits] : (hash_class == self.class)

  new_hash_options = {}
  new_hash_options[:traits_from] = self if copy_traits
  new_hash = hash_class.new_empty(new_hash_options)

  inject(new_hash) do |hash, key_value_pair|
    yield hash, key_value_pair
  end
end