Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#compact(option = nil) ⇒ Object

Returns a copy of self with all nil, empty and blank elements removed.

You can also pass option to use

  • :keys - Only remove blank keys from a hash

  • :values - Only remove blank values from a hash

  • :either - Remove blank values or keys from a hash

  • :both - Remove blank values and keys from a hash if both are blank

    => 2.compact #=> => 2 => 2, nil => ''.compact #=> => 2 => 2, nil => ' '.compact #=> => 2

    => 2, nil => 1, 2 => nil.compact(:keys) #=> => 2, 2 => nil => 2, nil => 1, 2 => nil.compact(:values) #=> => 2, nil => 1 => 2, nil => 1, 2 => nil.compact(:either) #=> => 2 => 2, nil => 1, 2 => nil.compact(:both) #=> => 2, nil => 1, 2 => nil



21
22
23
24
25
26
27
28
# File 'lib/rcore_ext/hash/compact.rb', line 21

def compact(option = nil)
  @option = option
  result = {}
  each_pair do |key, value|
    result[key] = value unless check_for_blank?(key, value)      
  end
  result
end

#compact!(option = nil) ⇒ Object

Removes nil, empty and blank elements from the hash. Returns nil if no changes were made, otherwise returns hash.

You can also pass option to use

  • :keys - Only remove blank keys from a hash

  • :values - Only remove blank values from a hash

  • :either - Remove blank values or keys from a hash

  • :both - Remove blank values and keys from a hash if both are blank

    => 2.compact! #=> nil => 2, nil => ''.compact! #=> => 2 => 2, nil => ' '.compact! #=> => 2

    => 2, nil => 1, 2 => nil.compact!(:keys) #=> => 2, 2 => nil => 2, nil => 1, 2 => nil.compact!(:values) #=> => 2, nil => 1 => 2, nil => 1, 2 => nil.compact!(:either) #=> => 2 => 2, nil => 1, 2 => nil.compact!(:both) #=> => 2, nil => 1, 2 => nil



47
48
49
50
51
52
53
# File 'lib/rcore_ext/hash/compact.rb', line 47

def compact!(option = nil)
  @option = option; changed = false
  each_pair do |key, value|
    (self.delete(key); changed = true) if check_for_blank?(key, value)      
  end
  changed ? self : nil
end

#deep_compact(option = nil) ⇒ Object

Returns a copy of self with all nil, empty and blank nested elements removed.

You can also pass option to use

  • :keys - Only remove blank keys from a hash

  • :values - Only remove blank values from a hash

  • :either - Remove blank values or keys from a hash

  • :both - Remove blank values and keys from a hash if both are blank

    => 2.deep_compact #=> => 2 => 2, nil => {nil => ''}.deep_compact #=> => 2 => 2, nil => {nil => ' '}.deep_compact #=> => 2

    => 2, {nil => 1 => => nil, nil => 1}.deep_compact(:keys) #=> => 2 => 2, {nil => 1 => => nil, nil => 1}.deep_compact(:values) #=> => 2, {nil => 1 => => 1} => 2, {nil => 1 => => nil, nil => 1}.deep_compact(:either) #=> => 2 => 2, {nil => 1 => => nil, nil => 1}.deep_compact(:both) #=> => 2, {nil => 1 => => nil, nil => 1}



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rcore_ext/hash/compact.rb', line 72

def deep_compact(option = nil)
  @option = option
  result = {}
  each_pair do |key, value|
    key   = key.deep_compact(option)    if key.is_a?(Hash)
    value = value.deep_compact(option)  if value.is_a?(Hash)

    result[key] = value unless check_for_blank?(key, value)
  end
  result
end

#deep_compact!(option = nil) ⇒ Object

Removes nil, empty and blank nested elements from the hash. Returns nil if no changes were made, otherwise returns hash.

You can also pass option to use

  • :keys - Only remove blank keys from a hash

  • :values - Only remove blank values from a hash

  • :either - Remove blank values or keys from a hash

  • :both - Remove blank values and keys from a hash if both are blank

    => 2.deep_compact! #=> => 2 => 2, nil => {nil => ''}.deep_compact! #=> => 2 => 2, nil => {nil => ' '}.deep_compact! #=> => 2

    => 2, {nil => 1 => => nil, nil => 1}.deep_compact!(:keys) #=> => 2 => 2, {nil => 1 => => nil, nil => 1}.deep_compact!(:values) #=> => 2, {nil => 1 => => 1} => 2, {nil => 1 => => nil, nil => 1}.deep_compact!(:either) #=> => 2 => 2, {nil => 1 => => nil, nil => 1}.deep_compact!(:both) #=> nil



101
102
103
104
105
106
107
108
109
110
# File 'lib/rcore_ext/hash/compact.rb', line 101

def deep_compact!(option = nil)
  @option = option; changed = false
  each_pair do |key, value|
    (changed = true unless key.deep_compact!(option).nil?)   if key.is_a?(Hash)
    (changed = true unless value.deep_compact!(option).nil?) if value.is_a?(Hash)

    (self.delete(key); changed = true) if check_for_blank?(key, value)
  end
  changed ? self : nil
end