Recursively Symbolize Keys in Ruby


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Hash
  2. def recursively_symbolize_keys!
  3. self.symbolize_keys!
  4. self.values.each do |v|
  5. if v.is_a? Hash
  6. v.recursively_symbolize_keys!
  7. elsif v.is_a? Array
  8. v.recursively_symbolize_keys!
  9. end
  10. end
  11. self
  12. end
  13. end
  14.  
  15. class Array
  16. def recursively_symbolize_keys!
  17. self.each do |item|
  18. if item.is_a? Hash
  19. item.recursively_symbolize_keys!
  20. elsif item.is_a? Array
  21. item.recursively_symbolize_keys!
  22. end
  23. end
  24. end
  25. end

URL: recursively-symbolize-keys-in-ruby

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.