Kouhei Sutou
null+****@clear*****
Mon Mar 17 14:26:09 JST 2014
Kouhei Sutou 2014-03-17 14:26:09 +0900 (Mon, 17 Mar 2014) New Revision: 8b4aedb11d7b790cea4bb8161a6d0b010ae79d1b https://github.com/droonga/grn2drn/commit/8b4aedb11d7b790cea4bb8161a6d0b010ae79d1b Message: Clean opening input/output code Modified files: bin/grn2drn Modified: bin/grn2drn (+20 -10) =================================================================== --- bin/grn2drn 2014-03-17 14:23:46 +0900 (dc9f647) +++ bin/grn2drn 2014-03-17 14:26:09 +0900 (51630cf) @@ -69,20 +69,30 @@ converter = Grn2Drn::CommandConverter.new(convert_options) source_file = args[0] result_file = args[1] -if source_file.nil? - input = STDIN -else - input = File.open(source_file) +def open_input(source_file) + if source_file.nil? + yield(STDIN) + else + File.open(source_file) do |input| + yield(input) + end + end end -if result_file.nil? - converter.convert(input) do |command| - puts(JSON.generate(command)) +def open_output(result_file) + if result_file.nil? + yield($stdout) + else + File.open("w", result_file) do |output| + yield(output) + end end -else - File.open("w", result_file) do |file| +end + +open_input(source_file) do |input| + open_output(result_file) do |output| converter.convert(input) do |command| - file.puts(JSON.generate(command)) + output.puts(JSON.generate(command)) end end end -------------- next part -------------- HTML����������������������������... Download