Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/cotcube-helpers/hash_ext.rb
Overview
Monkey patching the Ruby Core class Hash
Instance Method Summary collapse
- #deep_dup ⇒ Object
- #keys_to_sym! ⇒ Object
-
#reduce_group(&block) ⇒ Object
a group_hash was created from an array by running group_by to reduce a group_hash, the given block is applied to each array of the hash if its not an array value, the block will auto-yield nil.
Instance Method Details
#deep_dup ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cotcube-helpers/hash_ext.rb', line 33 def deep_dup map do |k,v| case v when Hash, Array [k, v.deep_dup] else [k, v.dup] end end.to_h end |
#keys_to_sym! ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/cotcube-helpers/hash_ext.rb', line 5 def keys_to_sym! self.keys.each do |key| case self[key].class.to_s when 'Hash' self[key].keys_to_sym! when 'Array' self[key].map { |el| el.is_a?(Hash) ? el.keys_to_sym! : el } end self["#{key}".to_sym] = delete(key) end self end |
#reduce_group(&block) ⇒ Object
a group_hash was created from an array by running group_by to reduce a group_hash, the given block is applied to each array of the hash if its not an array value, the block will auto-yield nil
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cotcube-helpers/hash_ext.rb', line 21 def reduce_group(&block) raise ArgumentError, 'No block given' unless block_given? map do |key,value| case value when Array [key, (block.call(value) rescue nil) ] else [key, nil] end end.to_h end |