Module: Halogen::HashUtil

Extended by:
HashUtil
Included in:
HashUtil
Defined in:
lib/halogen/hash_util.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#stringify_keys!(hash) ⇒ Hash

Transform hash keys into strings if necessary

Parameters:

  • hash (Hash)

Returns:

  • (Hash)

11
12
13
# File 'lib/halogen/hash_util.rb', line 11

def stringify_keys!(hash)
  transform_keys!(hash, &:to_s)
end

#symbolize_keys!(hash) ⇒ Hash

Transform hash keys into symbols if necessary

Parameters:

  • hash (Hash)

Returns:

  • (Hash)

21
22
23
# File 'lib/halogen/hash_util.rb', line 21

def symbolize_keys!(hash)
  transform_keys!(hash, &:to_sym)
end

#transform_keys!(hash) ⇒ Hash

Transform hash keys according to block

Parameters:

  • hash (Hash)

Returns:

  • (Hash)

31
32
33
34
35
# File 'lib/halogen/hash_util.rb', line 31

def transform_keys!(hash)
  hash.keys.each { |key| hash[yield(key)] = hash.delete(key) }

  hash
end