Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/subelsky_power_tools/ext/hash.rb
Overview
Instance Method Summary collapse
-
#except(*less_keys) ⇒ Object
Returns a new hash less the given keys.
-
#except!(*rejected) ⇒ Object
Replaces hash with new hash less the given keys.
-
#only(*keep_keys) ⇒ Object
Returns a new hash with only the given keys.
-
#only!(*keep_keys) ⇒ Object
Replaces hash with a new hash having only the given keys.
Instance Method Details
#except(*less_keys) ⇒ Object
Returns a new hash less the given keys.
7 8 9 10 11 |
# 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) ⇒ Object
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.
21 22 23 24 25 |
# 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) ⇒ Object
Returns a new hash with only the given keys.
h = {:a=>1, :b=>2}
h.only(:a) #=> {:a=>1}
32 33 34 35 36 37 38 |
# 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) ⇒ Object
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.
48 49 50 51 52 53 |
# 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 |