diff --git a/wc.rb b/wc.rb index 25aef9d..f0a3474 100644 --- a/wc.rb +++ b/wc.rb @@ -10,7 +10,7 @@ def parse_options opts.on('-c') { options[:bytes] = true } end.parse! options = { bytes: true, lines: true, words: true } if options.empty? - [options, ARGV.empty? ? [ARGF] : ARGV] + [options, ARGV.empty? ? ['-'] : ARGV] end def count_file_stats(input) @@ -24,13 +24,12 @@ def count_file_stats(input) end def process_input(source) - if source == ARGF || File.exist?(source) - input = source == ARGF ? ARGF.read : File.read(source) - count_file_stats(input) - else - puts "wc: #{source}: そのようなファイルやディレクトリはありません" - exit - end + input = if source == '-' + $stdin.read + else + File.read(source) + end + count_file_stats(input) end def update_totals(totals, stats) @@ -50,7 +49,7 @@ def format_result(stats, max_widths, options) end def print_result(result, source) - puts "#{result} #{source == ARGF ? '' : source}" + puts "#{result} #{source == '-' ? '' : source}" end def print_total(total_stats, max_widths, options)