Skip to content

Instantly share code, notes, and snippets.

@amonmoce
Last active September 10, 2016 15:47
Show Gist options
  • Save amonmoce/b57909174cde0f0ba8d0 to your computer and use it in GitHub Desktop.
Save amonmoce/b57909174cde0f0ba8d0 to your computer and use it in GitHub Desktop.
A Ruby code that convert yaml file into tsv file ... like $ruby yaml_to_tsv.rb in_filename.yml out_filename.tsv; Rubocop tested no offense
# Check the output file
if ARGV[1].nil?
print 'Insert the name of your output file (tsv file) '
outname = $stdin.gets.chomp
else
outname = ARGV[1]
end
# Deserialize
require 'yaml'
survey = YAML.load(File.read(ARGV[0]))
# Create the TSV file
first_hash = survey[0]
keys_array = first_hash.keys
line = ''
keys_array.each { |key| line.concat(key + "\t") }
File.open(outname, 'w') do |file|
file.puts line
survey.each do |record|
record.each_value { |value| file << value + "\t" }
file << "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment