Module: Nanoc3::HashExtensions

Included in:
Hash
Defined in:
lib/nanoc3/base/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#checksumString

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

Calculates the checksum for this hash. Any change to this hash will result in a different checksum.

Returns:

  • (String)

    The checksum for this hash



54
55
56
57
# File 'lib/nanoc3/base/core_ext/hash.rb', line 54

def checksum
  array = self.to_a.sort_by { |kv| kv[0].to_s }
  array.checksum
end

#freeze_recursivelyvoid

This method returns an undefined value.

Freezes the contents of the hash, as well as all hash values. The hash values will be frozen using #freeze_recursively if they respond to that message, or #freeze if they do not.

See Also:

  • Array#freeze_recursively

Since:

  • 3.2.0



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nanoc3/base/core_ext/hash.rb', line 36

def freeze_recursively
  return if self.frozen?
  freeze
  each_pair do |key, value|
    if value.respond_to?(:freeze_recursively)
      value.freeze_recursively
    else
      value.freeze
    end
  end
end

#stringify_keysHash

Returns a new hash where all keys are recursively converted to strings by calling ArrayExtensions#stringify_keys or #stringify_keys.

Returns:

  • (Hash)

    The converted hash



21
22
23
24
25
# File 'lib/nanoc3/base/core_ext/hash.rb', line 21

def stringify_keys
  inject({}) do |hash, (key, value)|
    hash.merge(key.to_s => value.respond_to?(:stringify_keys) ? value.stringify_keys : value)
  end
end

#symbolize_keysHash

Returns a new hash where all keys are recursively converted to symbols by calling ArrayExtensions#symbolize_keys or #symbolize_keys.

Returns:

  • (Hash)

    The converted hash



10
11
12
13
14
# File 'lib/nanoc3/base/core_ext/hash.rb', line 10

def symbolize_keys
  inject({}) do |hash, (key, value)|
    hash.merge(key.to_sym => value.respond_to?(:symbolize_keys) ? value.symbolize_keys : value)
  end
end