Skip to content

Instantly share code, notes, and snippets.

@CliveIMPISA
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CliveIMPISA/0e8ee17a37846c925a93 to your computer and use it in GitHub Desktop.
Save CliveIMPISA/0e8ee17a37846c925a93 to your computer and use it in GitHub Desktop.
YAML_to_TSV.rb
#!/usr/bin/env ruby
require 'yaml'
# Reading file from command line input and declaration of variables
stu_info = YAML.load(File.read(ARGV[0]))
flag = false
keyarr = []
valuearr = []
wholevalue = []
# Method reads the key of only one hash element in the stu_info array.
# Every value of each hash in the array in copied to a temporary array
# That temp array is then pushed to an array of array called wholevalue
stu_info.each do |unique|
unique.each do |a, b|
keyarr.push a if flag == false
valuearr.push b
end
wholevalue.push valuearr
valuearr = []
flag = true
end
# Function places the keyarr at the beginning of wholevalue array
wholevalue.unshift(keyarr)
# Data from wholevalue is written to a file
if ARGV[1]
File.open(ARGV[1], 'w') do |file|
wholevalue.each do |line|
file.puts line.join("\t")
end
end
else
File.open('Default.tsv', 'w') do |file|
wholevalue.each do |line|
file.puts line.join("\t")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment