Class: Hash
- Defined in:
- lib/ruby_smart/support/core_ext/ruby/hash.rb,
lib/ruby_smart/support/core_ext/ruby/hash.rb,
lib/ruby_smart/support/core_ext/activesupport/hash.rb,
lib/ruby_smart/support/core_ext/activesupport/hash.rb,
lib/ruby_smart/support/core_ext/activesupport/hash.rb
Instance Method Summary collapse
-
#deep_reject(&blk) ⇒ Object
returns a new Hash with items that the block evaluates to true removed, also to deep Hashes.
-
#deep_reject!(&blk) ⇒ Object
deep reject by provided block deep remove keys that the block evaluates to true.
-
#only!(*keys) ⇒ Hash
Replaces the hash with only the given keys (if exists), but returns the same hash (not the removed keys - this differs to Hash#slice!).
-
#product ⇒ Array
creates a 'product' of all values per existing key as a combination.
-
#to_md5 ⇒ String
returns the md5 of any hash by using the +#inspect+ method.
-
#without!(*keys) ⇒ Hash
removes the given keys from hash and returns those key => value pairs (this differs to Hash#except!).
Instance Method Details
#deep_reject(&blk) ⇒ Object
returns a new Hash with items that the block evaluates to true removed, also to deep Hashes.
48 49 50 |
# File 'lib/ruby_smart/support/core_ext/activesupport/hash.rb', line 48 def deep_reject(&blk) deep_dup.deep_reject!(&blk) end |
#deep_reject!(&blk) ⇒ Object
deep reject by provided block deep remove keys that the block evaluates to true
63 64 65 66 67 68 69 70 71 |
# File 'lib/ruby_smart/support/core_ext/activesupport/hash.rb', line 63 def deep_reject!(&blk) each do |k, v| if blk.(k, v) delete(k) elsif v.is_a?(Hash) v.deep_reject!(&blk) end end end |
#only!(*keys) ⇒ Hash
Replaces the hash with only the given keys (if exists), but returns the same hash (not the removed keys - this differs to Hash#slice!)
19 20 21 22 |
# File 'lib/ruby_smart/support/core_ext/activesupport/hash.rb', line 19 def only!(*keys) slice!(*keys) self end |
#product ⇒ Array
creates a 'product' of all values per existing key as a combination
hash = { first: [:a,:b], second: [:x,:c]}
[:second=>:x, :second=>:c, :second=>:x, :second=>:c]
22 23 24 25 |
# File 'lib/ruby_smart/support/core_ext/ruby/hash.rb', line 22 def product product = values[0].product(*values[1..-1]) product.map{|p| Hash[keys.zip p]} end |