Module: Cms::Extensions::Hash

Defined in:
lib/cms/extensions/hash.rb

Instance Method Summary collapse

Instance Method Details

#except(*args) ⇒ Object

Returns a copy of the hash without the keys passed as arguments



5
6
7
# File 'lib/cms/extensions/hash.rb', line 5

def except(*args)
  reject {|k,v| args.include?(k) }
end

#extract_only!(*keys) ⇒ Object

This takes a list of keys and returns a new hash containing the key/values that match the keys passed in. This will also remove the keys from this hash

Note: This behavior is slightly different from ActiveSupport Hash#extract! which can add nil keys if they don’t exist.



14
15
16
17
18
19
# File 'lib/cms/extensions/hash.rb', line 14

def extract_only!(*keys)
  keys.inject({}) do |hash, key|
    hash[key] = delete(key) if has_key?(key)
    hash
  end
end