Skip to content

Instantly share code, notes, and snippets.

@cattaka
Created August 16, 2016 03:49
Show Gist options
  • Save cattaka/51b126cf8aa8688fcc85bf07324731c4 to your computer and use it in GitHub Desktop.
Save cattaka/51b126cf8aa8688fcc85bf07324731c4 to your computer and use it in GitHub Desktop.
def dump_values(obj)
results = ""
obj.keys.each do |key|
results += key.to_s + "\t" + obj[key].to_s + "\n"
end
results
end
def dump_tsv(rows)
keys = rows.map do |row|
if (row.is_a?(ActiveRecord::Base))
row.attributes.keys
else
row.keys
end
end.flatten.uniq
data = rows.map do |row|
keys.map{ |key| row[key].to_s.gsub(/(\n)/, "\\n").gsub(/(\t)/, "\\t") }.join("\t")
end.join("\n")
keys.join("\t") + "\n" + data
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment