Class: Hash
Instance Method Summary collapse
-
#defaults(other) ⇒ Object
Return a new hash consisting of
other
reverse-merged into this hash; that is, equal to other.merge(self). -
#defaults!(other) ⇒ Object
Reverse-merge
other
into this hash in-place; that is, merge all key-value pairs inother
whose keys are not already present in self. -
#discard(*keys) ⇒ Object
Return a new hash filtered to exclude any key-value pairs whose keys appear in
keys
. -
#discard!(*keys) ⇒ Object
Filter this hash in-place so any key-value pairs whose keys appear in
keys
are removed. -
#map_values(&block) ⇒ Object
Return a new hash with the same keys as this hash, but with values generated by yielding each key-value pair to
block
. -
#selekt(&block) ⇒ Object
Return a new hash filtered to contain just the key-value pairs for which the block returns true.
-
#slice(*keys) ⇒ Object
Return a new hash filtered to contain only the key-value pairs whose keys appear in
keys
. -
#slice!(*keys) ⇒ Object
Filter this hash in-place so it contains only the key-value pairs whose keys appear in
keys
.
Instance Method Details
#defaults(other) ⇒ Object
Return a new hash consisting of other
reverse-merged into this hash; that is, equal to other.merge(self).
39 40 41 42 |
# File 'lib/babushka/core_patches/hash.rb', line 39 def defaults other Babushka::LogHelpers.deprecated! '2017-09-01', instead: '#merge the other way around' dup.defaults! other end |
#defaults!(other) ⇒ Object
Reverse-merge other
into this hash in-place; that is, merge all key-value pairs in other
whose keys are not already present in self.
32 33 34 35 |
# File 'lib/babushka/core_patches/hash.rb', line 32 def defaults! other Babushka::LogHelpers.deprecated! '2017-09-01', instead: '#merge! the other way around' replace other.merge(self) end |
#discard(*keys) ⇒ Object
Return a new hash filtered to exclude any key-value pairs whose keys appear in keys
.
4 5 6 |
# File 'lib/babushka/core_patches/hash.rb', line 4 def discard *keys dup.discard!(*keys) end |
#discard!(*keys) ⇒ Object
Filter this hash in-place so any key-value pairs whose keys appear in keys
are removed.
10 11 12 13 |
# File 'lib/babushka/core_patches/hash.rb', line 10 def discard! *keys keys.each {|k| delete k } self end |
#map_values(&block) ⇒ Object
Return a new hash with the same keys as this hash, but with values generated by yielding each key-value pair to block
.
46 47 48 49 50 51 |
# File 'lib/babushka/core_patches/hash.rb', line 46 def map_values &block keys.inject({}) {|acc,k| acc[k] = yield(k, self[k]) acc } end |
#selekt(&block) ⇒ Object
Return a new hash filtered to contain just the key-value pairs for which the block returns true. That is, like Hash#select, but returning a hash instead of an array of tuples.
56 57 58 59 60 61 62 63 |
# File 'lib/babushka/core_patches/hash.rb', line 56 def selekt &block Babushka::LogHelpers.deprecated! '2017-09-01', instead: '#select' hsh = {} each_pair {|k,v| hsh[k] = v if yield(k,v) } hsh end |
#slice(*keys) ⇒ Object
Return a new hash filtered to contain only the key-value pairs whose keys appear in keys
.
17 18 19 |
# File 'lib/babushka/core_patches/hash.rb', line 17 def slice *keys dup.slice!(*keys) end |
#slice!(*keys) ⇒ Object
Filter this hash in-place so it contains only the key-value pairs whose keys appear in keys
.
23 24 25 26 27 28 |
# File 'lib/babushka/core_patches/hash.rb', line 23 def slice! *keys keys.inject({}) {|acc,key| acc[key] = self.delete(key) if self.has_key?(key) acc } end |