Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. def flatten_hash(hash, results = {}, parent_key = '')
  2. return results unless hash.kind_of?(Hash)
  3.  
  4. hash.keys.each do |key|
  5. # current_key = "#{parent_key}[#{key}]" # uncomment this if you want to keep parent key as a partof key for flattened hash (csv column name)
  6. current_key = key
  7. if hash[key].kind_of?(Hash)
  8. results = flatten_hash(hash[key], results, current_key)
  9. else
  10. if hash[key].kind_of?(Array)
  11. results[current_key] = hash[key].reject(&:empty?).join("; ")
  12. else
  13. results[current_key] = hash[key]
  14. end
  15. end
  16. end
  17.  
  18. results
  19. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement