Module: Gorillib::Hashlike::Compact

Included in:
Hash
Defined in:
lib/gorillib/hashlike/compact.rb

Instance Method Summary collapse

Instance Method Details

#compactHashlike

returns a compact!ed copy (contains no key/value pairs having nil? values)

Examples:

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

Returns:


15
16
17
# File 'lib/gorillib/hashlike/compact.rb', line 15

def compact
  reject{|key,val| val.nil? }
end

#compact!Hashlike

Removes all key/value pairs having nil? values

Examples:

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

Returns:


28
29
30
# File 'lib/gorillib/hashlike/compact.rb', line 28

def compact!
  delete_if{|key,val| val.nil? }
end

#compact_blankHashlike

returns a compact!ed copy (contains no key/value pairs having blank? values)

Examples:

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

Returns:


41
42
43
# File 'lib/gorillib/hashlike/compact.rb', line 41

def compact_blank
  reject{|key,val| val.blank? }
end

#compact_blank!Hashlike

Removes all key/value pairs having blank? values

Examples:

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

Returns:


54
55
56
# File 'lib/gorillib/hashlike/compact.rb', line 54

def compact_blank!
  delete_if{|key,val| val.blank? }
end