Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. require 'yaml'
  2.  
  3. def get_head(informations)
  4. informations[0].keys.join("\t")
  5. end
  6.  
  7. def get_content(informations)
  8. info = ''
  9. informations.each do |information|
  10. info << information.values.join("\t") << "\n"
  11. end
  12. info
  13. end
  14.  
  15. def read_yaml(file)
  16. YAML.load(File.read(file))
  17. end
  18.  
  19. fail ArgumentError, "Usage: YAML_to_TSV input_file (output_file)\n" if \
  20. ARGV.count == 0 || ARGV.count > 2
  21. fail ArgumentError, "The file is not exist\n" unless File.exist? ARGV[0].to_s
  22.  
  23. input = read_yaml ARGV[0]
  24. result = get_head(input) << "\n" << get_content(input)
  25. if ARGV.count != 1
  26. File.open(ARGV[1], 'w') do |file|
  27. file.puts result
  28. end
  29. else
  30. puts result
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement