Module: Useful::RubyExtensions::Hash::ClassMethods
- Defined in:
- lib/useful/ruby_extensions/hash.rb
Instance Method Summary collapse
-
#except(hash, *keys) ⇒ Object
Return the hash with only keys NOT in *keys.
- #from_json(string) ⇒ Object
-
#nillify(hash) ⇒ Object
takes any empty values and makes them nil.
-
#only(hash, *keys) ⇒ Object
Return the hash with only keys in *keys.
Instance Method Details
#except(hash, *keys) ⇒ Object
Return the hash with only keys NOT in *keys
23 24 25 26 27 |
# File 'lib/useful/ruby_extensions/hash.rb', line 23 def except(hash, *keys) s_keys = keys.flatten.collect{|k| k.to_s} hash.delete_if{ |k,v| keys.flatten.include?(k) || s_keys.include?(k) } hash end |
#from_json(string) ⇒ Object
11 12 13 |
# File 'lib/useful/ruby_extensions/hash.rb', line 11 def from_json(string) JSON.parse(string) end |
#nillify(hash) ⇒ Object
takes any empty values and makes them nil
30 31 32 33 34 35 36 37 |
# File 'lib/useful/ruby_extensions/hash.rb', line 30 def nillify(hash) hash.each do |key,value| if !value.nil? && ( (value.respond_to?('empty?') && value.empty?) || (value.respond_to?('to_s') && value.to_s.empty?) ) hash[key] = nil end end hash end |
#only(hash, *keys) ⇒ Object
Return the hash with only keys in *keys
16 17 18 19 20 |
# File 'lib/useful/ruby_extensions/hash.rb', line 16 def only(hash, *keys) s_keys = keys.flatten.collect{|k| k.to_s} hash.delete_if{ |k,v| !keys.flatten.include?(k) && !s_keys.include?(k) } hash end |