Module: SleepingKingStudios::Tools::HashTools

Extended by:
HashTools
Included in:
HashTools
Defined in:
lib/sleeping_king_studios/tools/hash_tools.rb

Overview

Tools for working with hash-like enumerable objects.

Constant Summary collapse

HASH_METHODS =
[:[], :count, :each, :each_key, :each_pair].freeze

Instance Method Summary collapse

Instance Method Details

#convert_keys_to_strings(hsh) ⇒ Hash Also known as: stringify_keys

Returns a copy of the hash with the keys converted to strings.

Parameters:

  • hsh (Hash)

    The hash to convert.

Returns:

  • (Hash)

    The converted copy of the hash.



19
20
21
22
23
24
25
26
27
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 19

def convert_keys_to_strings hsh
  require_hash! hsh

  hsh.each.with_object({}) do |(key, value), cpy|
    sym = key.to_s

    cpy[sym] = convert_value_to_stringified_hash(value)
  end # each
end

#convert_keys_to_symbols(hsh) ⇒ Hash Also known as: symbolize_keys

Returns a copy of the hash with the keys converted to symbols.

Parameters:

  • hsh (Hash)

    The hash to convert.

Returns:

  • (Hash)

    The converted copy of the hash.



35
36
37
38
39
40
41
42
43
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 35

def convert_keys_to_symbols hsh
  require_hash! hsh

  hsh.each.with_object({}) do |(key, value), cpy|
    sym = key.to_s.intern

    cpy[sym] = convert_value_to_symbolic_hash(value)
  end # each
end

#deep_dup(hsh) ⇒ Hash

Creates a deep copy of the object by returning a new Hash with deep copies of each key and value.

Parameters:

  • hsh (Hash<Object>)

    The hash to copy.

Returns:

  • (Hash)

    The copy of the hash.



52
53
54
55
56
57
58
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 52

def deep_dup hsh
  require_hash! hsh

  hsh.each.with_object(Hash.new) do |(key, value), copy|
    copy[ObjectTools.deep_dup key] = ObjectTools.deep_dup(value)
  end # each
end

#deep_freeze(hsh) ⇒ Object

Freezes the hash and performs a deep freeze on each hash key and value.

Parameters:

  • hsh (Hash)

    The object to freeze.



64
65
66
67
68
69
70
71
72
73
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 64

def deep_freeze hsh
  require_hash! hsh

  hsh.freeze

  hsh.each do |key, value|
    ObjectTools.deep_freeze key
    ObjectTools.deep_freeze value
  end # each
end

#hash?(hsh) ⇒ Boolean

Returns true if the object is or appears to be a Hash.

Parameters:

  • hsh (Object)

    The object to test.

Returns:

  • (Boolean)

    True if the object is a Hash, otherwise false.



80
81
82
83
84
85
86
87
88
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 80

def hash? hsh
  return true if Hash === hsh

  HASH_METHODS.each do |method_name|
    return false unless hsh.respond_to?(method_name)
  end # each

  true
end

#immutable?(hsh) ⇒ Boolean

Returns true if the hash is immutable, i.e. if the hash is frozen and each hash key and hash value are immutable.

Parameters:

  • hsh (Hash)

    The hash to test.

Returns:

  • (Boolean)

    True if the hash is immutable, otherwise false.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 96

def immutable? hsh
  require_hash! hsh

  return false unless hsh.frozen?

  hsh.each do |key, value|
    return false unless ObjectTools.immutable?(key) && ObjectTools.immutable?(value)
  end # each

  true
end

#mutable?(hsh) ⇒ Boolean

Returns true if the hash is mutable.

Parameters:

  • hsh (Array)

    The hash to test.

Returns:

  • (Boolean)

    True if the hash is mutable, otherwise false.

See Also:



115
116
117
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 115

def mutable? hsh
  !immutable?(hsh)
end