Class: Hash

Inherits:
Object show all
Defined in:
lib/hoodie/hash.rb,
lib/hoodie/blank.rb

Overview

Add #blank? method to Hash class.

Instance Method Summary collapse

Instance Method Details

#compactHash

Returns a compacted copy (contains no key/value pairs having nil? values)

Examples:

hash = { a: 100, b: nil, c: false, d: '' }
hash.compact # => { a: 100, c: false, d: '' }
hash         # => { a: 100, b: nil, c: false, d: '' }

Returns:



31
32
33
# File 'lib/hoodie/hash.rb', line 31

def compact
  select { |_, value| !value.nil? }
end

#normalize_keysHash

Returns a new hash with all keys downcased and converted to symbols.

Returns:



68
69
70
# File 'lib/hoodie/hash.rb', line 68

def normalize_keys
  transform_keys { |key| key.downcase.to_sym rescue key }
end

#recursive_merge(other) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hoodie/hash.rb', line 113

def recursive_merge(other)
  hash = dup
  other.each do |key, value|
    myval = self[key]
    if value.is_a?(Hash) && myval.is_a?(Hash)
      hash[key] = myval.recursive_merge(value)
    else
      hash[key] = value
    end
  end
  hash
end

#recursively_normalize_keysHash

Returns a new Hash, recursively downcasing and converting all keys to symbols.

Returns:



77
78
79
# File 'lib/hoodie/hash.rb', line 77

def recursively_normalize_keys
  recursively_transform_keys { |key| key.downcase.to_sym rescue key }
end

#recursively_stringify_keyHash

Returns a new Hash, recursively converting all keys to strings.

Returns:



109
110
111
# File 'lib/hoodie/hash.rb', line 109

def recursively_stringify_key
  recursively_transform_keys { |key| key.to_s rescue key }
end

#recursively_symbolize_keysHash

Returns a new Hash, recursively converting all keys to symbols.

Returns:



93
94
95
# File 'lib/hoodie/hash.rb', line 93

def recursively_symbolize_keys
  recursively_transform_keys { |key| key.downcase.to_sym rescue key }
end

#recursively_transform_keys(&block) ⇒ Hash

Returns a new hash, recursively converting all keys by the block operation.

Returns:



59
60
61
# File 'lib/hoodie/hash.rb', line 59

def recursively_transform_keys(&block)
  _recursively_transform_keys_in_object(self, &block)
end

#stringify_keysHash

Returns a new hash with all keys converted to strings.

Returns:



101
102
103
# File 'lib/hoodie/hash.rb', line 101

def stringify_keys
  transform_keys { |key| key.to_s rescue key }
end

#symbolize_keysHash

Returns a new hash with all keys converted to symbols.

Returns:



85
86
87
# File 'lib/hoodie/hash.rb', line 85

def symbolize_keys
  transform_keys { |key| key.to_sym rescue key }
end

#transform_keysHash

Returns a new hash with all keys converted using the block operation.

Examples:

hash = { name: 'Tiggy', age: '15' }

hash.transform_keys{ |key| key.to_s.upcase }
            # => { "AGE" => "15", "NAME" => "Tiggy" }

Returns:



45
46
47
48
49
50
51
52
# File 'lib/hoodie/hash.rb', line 45

def transform_keys
  return enum_for(:transform_keys) unless block_given?
  result = self.class.new
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end