module HashUtils
Public Instance Methods
compact()
click to toggle source
# File lib/buzztools/extend_hash.rb, line 72 def compact self.reject {|k,v| v==nil} end
filter_exclude(aKeys,aHash=nil)
click to toggle source
# File lib/buzztools/extend_hash.rb, line 35 def filter_exclude(aKeys,aHash=nil) aHash ||= self filter_exclude!(aKeys,aHash.dup) end
filter_exclude!(aKeys,aHash=nil)
click to toggle source
# File lib/buzztools/extend_hash.rb, line 23 def filter_exclude!(aKeys,aHash=nil) aHash ||= self if aKeys.is_a? Regexp return aHash.delete_if {|k,v| k =~ aKeys } else aKeys = [aKeys] unless aKeys.is_a? Array return aHash if aKeys.empty? return aHash.delete_if {|key, value| ((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))} end end
filter_include(aKeys,aHash=nil)
click to toggle source
# File lib/buzztools/extend_hash.rb, line 18 def filter_include(aKeys,aHash=nil) aHash ||= self filter_include!(aKeys,aHash.dup) end
filter_include!(aKeys,aHash=nil)
click to toggle source
# File lib/buzztools/extend_hash.rb, line 5 def filter_include!(aKeys,aHash=nil) aHash ||= self if aKeys.is_a? Regexp return aHash.delete_if {|k,v| not k =~ aKeys } else aKeys = [aKeys] unless aKeys.is_a? Array return aHash.clear if aKeys.empty? return aHash.delete_if {|key, value| !((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))} return aHash # last resort end end
has_values_for?(aKeys,aHash=nil)
click to toggle source
# File lib/buzztools/extend_hash.rb, line 40 def has_values_for?(aKeys,aHash=nil) aHash ||= self # check all keys exist in aHash and their values are not nil aKeys.all? { |k,v| aHash[k] } end
symbolize_keys()
click to toggle source
# File lib/buzztools/extend_hash.rb, line 66 def symbolize_keys result = {} self.each { |k,v| k.is_a?(String) ? result[k.to_sym] = v : result[k] = v } return result end
without_key(aKey) { |h| ... }
click to toggle source
give a block to execute without the given key in this hash It will be replaced after the block (guaranteed by ensure) eg. hash.without_key(:blah) do |aHash|
puts aHash.inspect
end
# File lib/buzztools/extend_hash.rb, line 52 def without_key(aKey) temp = nil h = self begin if h.include?(aKey) temp = [aKey,h.delete(aKey)] end result = yield(h) ensure h[temp[0]] = temp[1] if temp end return result end