Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/mimi/core/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#deep_dupHash

Duplicates a Hash with all nested values

Returns:

  • (Hash)

    a new Hash



120
121
122
123
124
125
# File 'lib/mimi/core/core_ext.rb', line 120

def deep_dup
  map do |k, v|
    v = v.respond_to?(:deep_dup) ? v.deep_dup : v.dup
    [k, v]
  end.to_h
end

#deep_merge(right) ⇒ Hash

Returns a new Hash.

Parameters:

  • right (Hash)

    the right (other) Hash

Returns:

  • (Hash)

    a new Hash

See Also:



109
110
111
# File 'lib/mimi/core/core_ext.rb', line 109

def deep_merge(right)
  deep_dup.deep_merge!(right)
end

#deep_merge!(right) ⇒ Hash

Deep merges self (left) Hash with another Hash

On keys existing in both Hashes:

  • merges Hash values by merging left Hash with the right Hash
  • merges Array values by union operator
  • for other values overwrites left Hash value with the right Hash value

Parameters:

  • right (Hash)

    the right (other) Hash

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mimi/core/core_ext.rb', line 86

def deep_merge!(right)
  right.each do |k ,v|
    unless self.key?(k)
      self[k] = v
      next
    end
    if self[k].is_a?(Hash) && v.is_a?(Hash)
      self[k].deep_merge!(v)
    elsif self[k].is_a?(Array) && v.is_a?(Array)
      self[k] = self[k] | v
    else
      # unmergeable values, overwrite
      self[k] = v
    end
  end
  self
end

#deep_stringify_keysHash

Stringifies Hash keys including all nested Hashes

Returns:

  • (Hash)

    a new Hash



201
202
203
204
205
206
207
# File 'lib/mimi/core/core_ext.rb', line 201

def deep_stringify_keys
  map do |k, v|
    k = k.respond_to?(:to_s) ? k.to_s : k
    v = v.respond_to?(:deep_stringify_keys) ? v.deep_stringify_keys : v
    [k, v]
  end.to_h
end

#deep_stringify_keys!Hash

Modifies the Hash stringifying its keys including all nested Hashes

Returns:



213
214
215
# File 'lib/mimi/core/core_ext.rb', line 213

def deep_stringify_keys!
  replace(deep_stringify_keys)
end

#deep_symbolize_keysHash

Symbolizes Hash keys including all nested Hashes

Returns:

  • (Hash)

    a new Hash



156
157
158
159
160
161
162
# File 'lib/mimi/core/core_ext.rb', line 156

def deep_symbolize_keys
  map do |k, v|
    k = k.respond_to?(:to_sym) ? k.to_sym : k
    v = v.respond_to?(:deep_symbolize_keys) ? v.deep_symbolize_keys : v
    [k, v]
  end.to_h
end

#deep_symbolize_keys!Hash

Modifies the Hash symbolizing its keys including all nested Hashes

Returns:



168
169
170
# File 'lib/mimi/core/core_ext.rb', line 168

def deep_symbolize_keys!
  replace(deep_symbolize_keys)
end

#except(*keys) ⇒ Hash

Returns a Hash with given keys excluded, if present

Examples:

h = { a: 1, b: 2, :c 3 }
h.except(:a, :b, :d) # => { c: 3 }

Parameters:

  • *keys (*)

    list of keys

Returns:

  • (Hash)

    a new Hash



50
51
52
# File 'lib/mimi/core/core_ext.rb', line 50

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Hash

Modifies the Hash excluding given keys, if present

Examples:

h = { a: 1, b: 2, :c 3 }
h.except!(:a, :b, :d)
h # => { c: 3 }

Parameters:

  • *keys (*)

    list of keys

Returns:



64
65
66
67
68
69
70
71
# File 'lib/mimi/core/core_ext.rb', line 64

def except!(*keys)
  if keys.size == 1 && keys.first.is_a?(Array)
    raise ArgumentError, 'Hash#except!() expects keys as list of arguments,' \
      ' not an Array as first argument'
  end
  reject! { |k, _| keys.include?(k) }
  self
end

#only(*keys) ⇒ Hash

Returns a Hash with only given keys, if present

Examples:

h = { a: 1, b: 2, :c 3 }
h.only(:a, :b, :d) # => { a: 1, b: 2 }

Parameters:

  • *keys (*)

    list of keys

Returns:

  • (Hash)

    a new Hash



15
16
17
# File 'lib/mimi/core/core_ext.rb', line 15

def only(*keys)
  dup.only!(*keys)
end

#only!(*keys) ⇒ Hash

Modifies the Hash keeping only given keys, if present

Examples:

h = { a: 1, b: 2, :c 3 }
h.only!(:a, :b, :d)
h # => { a: 1, b: 2 }

Parameters:

  • *keys (*)

    list of keys

Returns:



29
30
31
32
33
34
35
36
# File 'lib/mimi/core/core_ext.rb', line 29

def only!(*keys)
  if keys.size == 1 && keys.first.is_a?(Array)
    raise ArgumentError, 'Hash#only!() expects keys as list of arguments,' \
      ' not an Array as first argument'
  end
  select! { |k, _| keys.include?(k) }
  self
end

#stringify_keysHash

Stringifies Hash keys

Returns:

  • (Hash)

    a new Hash



179
180
181
182
183
184
# File 'lib/mimi/core/core_ext.rb', line 179

def stringify_keys
  map do |k, v|
    k = k.respond_to?(:to_s) ? k.to_s : k
    [k, v]
  end.to_h
end

#stringify_keys!Hash

Modifies the Hash stringifying its keys

Returns:



190
191
192
# File 'lib/mimi/core/core_ext.rb', line 190

def stringify_keys!
  replace(stringify_keys)
end

#symbolize_keysHash

Symbolizes Hash keys

Returns:

  • (Hash)

    a new Hash



134
135
136
137
138
139
# File 'lib/mimi/core/core_ext.rb', line 134

def symbolize_keys
  map do |k, v|
    k = k.respond_to?(:to_sym) ? k.to_sym : k
    [k, v]
  end.to_h
end

#symbolize_keys!Hash

Modifies the Hash symbolizing its keys

Returns:



145
146
147
# File 'lib/mimi/core/core_ext.rb', line 145

def symbolize_keys!
  replace(symbolize_keys)
end