fork(1) download
  1. DEFAULT_NUM = 10
  2.  
  3.  
  4. def main(argv)
  5. unless argv.length <= 2
  6. print "Usage: ruby #{__FILE__} file-name [num]"
  7. exit 1
  8. end
  9.  
  10. file_name = argv[0]
  11. num = if argv[1].nil?
  12. DEFAULT_NUM
  13. else
  14. argv[1].to_i
  15. end
  16.  
  17. unless num >= 1
  18. print "Invalid number"
  19. exit 1
  20. end
  21.  
  22. write_file_by_n file_name, num
  23. end
  24.  
  25.  
  26. def write_file_by_n(input_file_name, num)
  27. File.open(input_file_name, 'r').each_line.each_slice(num).with_index do
  28. |lines, index|
  29.  
  30. output_file_name = format "%s-%d", input_file_name, index + 1
  31. File.open(output_file_name, 'w') do |out_file|
  32. lines.each do |line|
  33. out_file.puts line
  34. end
  35. end
  36. end
  37. end
  38.  
  39.  
  40. if __FILE__ == $0
  41. main ARGV
  42. end
Runtime error #stdin #stdout #stderr 0.02s 7456KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog.rb:27:in `initialize': can't convert nil into String (TypeError)
	from prog.rb:27:in `open'
	from prog.rb:27:in `write_file_by_n'
	from prog.rb:22:in `main'
	from prog.rb:41:in `<main>'