class Hash

github.com/rubyworks/facets/blob/master/lib/core/facets/hash/except.rb github.com/rubyworks/facets/blob/master/lib/core/facets/hash/slice.rb

Public Instance Methods

except(*less_keys) click to toggle source

Returns a new hash less the given keys.

# File lib/subelsky_power_tools/ext/hash.rb, line 7
def except(*less_keys)
  hash = dup
  less_keys.each{ |k| hash.delete(k) }
  hash
end
except!(*rejected) click to toggle source

Replaces hash with new hash less the given keys. This returns the hash of keys removed.

h = {:a=>1, :b=>2, :c=>3}
h.except!(:a)  #=> {:a=>1}
h              #=> {:b=>2,:c=>3}

Returns a Hash of the removed pairs.

# File lib/subelsky_power_tools/ext/hash.rb, line 21
def except!(*rejected)
  removed = {}
  rejected.each{ |k| removed[k] = delete(k) }
  removed
end
only(*keep_keys) click to toggle source

Returns a new hash with only the given keys.

h = {:a=>1, :b=>2}
h.only(:a)  #=> {:a=>1}
# File lib/subelsky_power_tools/ext/hash.rb, line 32
def only(*keep_keys)
  hash = {}
  keep_keys.each do |key|
    hash[key] = fetch(key)
  end
  hash
end
only!(*keep_keys) click to toggle source

Replaces hash with a new hash having only the given keys. This return the hash of keys removed.

h = {:a=>1, :b=>2}
h.only!(:a)  #=> {:b=>2}
h             #=> {:a=>1}

Returns a Hash of the removed pairs.

# File lib/subelsky_power_tools/ext/hash.rb, line 48
def only!(*keep_keys)
  rejected = keys - keep_keys
  removed = {}
  rejected.each{ |k| removed[k] = delete(k) }
  removed
end