Class: Cistern::Hash
- Inherits:
-
Object
- Object
- Cistern::Hash
- Defined in:
- lib/cistern/hash.rb
Class Method Summary collapse
-
.except(hash, *keys) ⇒ Hash
Copy of #hash containing all keys except #keys.
-
.except!(hash, *keys) ⇒ Hash
Remove all keys not specified in #keys from #hash in place.
-
.slice(hash, *keys) ⇒ Hash
Copy of #hash containing only #keys.
-
.stringify_keys(object) ⇒ Hash
Copy #hash and convert all keys to strings recursively.
Class Method Details
.except(hash, *keys) ⇒ Hash
Returns copy of #hash containing all keys except #keys.
14 15 16 |
# File 'lib/cistern/hash.rb', line 14 def self.except(hash, *keys) Cistern::Hash.except!(hash.dup, *keys) end |
.except!(hash, *keys) ⇒ Hash
Remove all keys not specified in #keys from #hash in place
24 25 26 27 |
# File 'lib/cistern/hash.rb', line 24 def self.except!(hash, *keys) keys.each { |key| hash.delete(key) } hash end |
.slice(hash, *keys) ⇒ Hash
Returns copy of #hash containing only #keys.
7 8 9 |
# File 'lib/cistern/hash.rb', line 7 def self.slice(hash, *keys) keys.each_with_object({}) { |e, a| a[e] = hash[e] if hash.key?(e) } end |
.stringify_keys(object) ⇒ Hash
Copy #hash and convert all keys to strings recursively.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cistern/hash.rb', line 34 def self.stringify_keys(object) case object when Hash object.each_with_object({}) { |(k, v), a| a[k.to_s] = stringify_keys(v) } when Array object.map { |v| stringify_keys(v) } else object end end |