Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/chinese/core_ext/hash.rb
Instance Method Summary (collapse)
-
- (Object) delete_keys(*keys)
Returns a copy of self with *keys removed.
-
- (Object) delete_keys!(*keys)
Remove *keys from self.
-
- (Hash) slice(*keys)
Creates a sub-hash from
selfwith the keys fromkeys. - - (Object) slice!(*keys)
Instance Method Details
- (Object) delete_keys(*keys)
Returns a copy of self with *keys removed.
6 7 8 9 10 11 12 13 |
# File 'lib/chinese/core_ext/hash.rb', line 6 def delete_keys(*keys) hash = self.dup keys.each do |key| hash.delete(key) end hash end |
- (Object) delete_keys!(*keys)
Remove *keys from self
16 17 18 19 20 |
# File 'lib/chinese/core_ext/hash.rb', line 16 def delete_keys!(*keys) keys.each do |key| self.delete(key) end end |
- (Hash) slice(*keys)
Note:
keys in keys not present in self are silently ignored.
Creates a sub-hash from self with the keys from keys
25 26 27 |
# File 'lib/chinese/core_ext/hash.rb', line 25 def slice(*keys) self.select { |k,v| keys.include?(k) } end |
- (Object) slice!(*keys)
29 30 31 32 33 34 |
# File 'lib/chinese/core_ext/hash.rb', line 29 def slice!(*keys) sub_hash = self.select { |k,v| keys.include?(k) } # Remove 'keys' form self: self.delete_keys!(*sub_hash.keys) sub_hash end |