Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def flatten_hash(hash, results = {}, parent_key = '')
- return results unless hash.kind_of?(Hash)
- hash.keys.each do |key|
- # current_key = "#{parent_key}[#{key}]" # uncomment this if you want to keep parent key as a partof key for flattened hash (csv column name)
- current_key = key
- if hash[key].kind_of?(Hash)
- results = flatten_hash(hash[key], results, current_key)
- else
- if hash[key].kind_of?(Array)
- results[current_key] = hash[key].reject(&:empty?).join("; ")
- else
- results[current_key] = hash[key]
- end
- end
- end
- results
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement