Module: HashExtensions
- Defined in:
 - lib/hash_extensions.rb
 
Overview
This code is by Avdi Grim. You can find the origin at his blog post devblog.avdi.org/2009/11/20/hash-transforms-in-ruby/
Instance Method Summary collapse
- 
  
    
      #deep_stringify_keys  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Convert keys to strings, recursively.
 - 
  
    
      #stringify_keys  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Convert all keys in the hash to strings.
 - 
  
    
      #transform_hash(options = {}, &block)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    
Transform all elements of a hash.
 
Instance Method Details
#deep_stringify_keys ⇒ Object
Convert keys to strings, recursively
      38 39 40 41 42  | 
    
      # File 'lib/hash_extensions.rb', line 38 def deep_stringify_keys transform_hash(:deep => true) do |hash, key, value| hash[key.to_s] = value end end  | 
  
#stringify_keys ⇒ Object
Convert all keys in the hash to strings
      31 32 33 34 35  | 
    
      # File 'lib/hash_extensions.rb', line 31 def stringify_keys transform_hash do |hash, key, value| hash[key.to_s] = value end end  | 
  
#transform_hash(options = {}, &block) ⇒ Hash
Transform all elements of a hash
rubocop:disable CaseEquality, ElseAlignment, EndAlignment, IndentationWidth, BlockAlignment, EachWithObject
      15 16 17 18 19 20 21 22 23 24 25  | 
    
      # File 'lib/hash_extensions.rb', line 15 def transform_hash( = {}, &block) inject({}) do |result, (key, value)| value = if [:deep] && Hash === value transform_hash(value, , &block) else value end block.call(result, key, value) result end end  |