Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/howzit/hash.rb

Overview

Hash helpers

Instance Method Summary collapse

Instance Method Details

#deep_freezeHash

Freeze all values in a hash

Returns:

  • (Hash)

    Hash with all values frozen



10
11
12
13
14
15
16
17
# File 'lib/howzit/hash.rb', line 10

def deep_freeze
  chilled = {}
  each do |k, v|
    chilled[k] = v.is_a?(Hash) ? v.deep_freeze : v.freeze
  end

  chilled.freeze
end

#deep_freeze!Object

Deep freeze a hash in place (destructive)



22
23
24
# File 'lib/howzit/hash.rb', line 22

def deep_freeze!
  replace deep_thaw.deep_freeze
end

#deep_thawHash

Unfreeze nested hash values

Returns:

  • (Hash)

    Hash with all values unfrozen



31
32
33
34
35
36
37
38
# File 'lib/howzit/hash.rb', line 31

def deep_thaw
  chilled = {}
  each do |k, v|
    chilled[k] = v.is_a?(Hash) ? v.deep_thaw : v.dup
  end

  chilled.dup
end

#deep_thaw!Object

Unfreeze nested hash values in place (destructive)



43
44
45
# File 'lib/howzit/hash.rb', line 43

def deep_thaw!
  replace deep_thaw
end

#stringify_keysHash

Turn all keys into string

Returns:

  • (Hash)

    hash with all keys as strings



51
52
53
# File 'lib/howzit/hash.rb', line 51

def stringify_keys
  each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v.is_a?(Hash) ? v.stringify_keys : v }
end

#stringify_keys!Object

Turn all keys into strings in place (destructive)



58
59
60
# File 'lib/howzit/hash.rb', line 58

def stringify_keys!
  replace stringify_keys
end

#symbolize_keysHash

Turn all keys into symbols

Returns:

  • (Hash)

    hash with all keys as symbols



66
67
68
# File 'lib/howzit/hash.rb', line 66

def symbolize_keys
  each_with_object({}) { |(k, v), hsh| hsh[k.to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v }
end

#symbolize_keys!Object

Turn all keys into symbols in place (destructive)



73
74
75
# File 'lib/howzit/hash.rb', line 73

def symbolize_keys!
  replace symbolize_keys
end