$stdin.read

This commit is contained in:
Rikuoh Tsujitani 2024-07-16 21:57:49 +09:00
parent b0d547fda0
commit d38889ea48
Signed by: riq0h
GPG key ID: 010F09DEA298C717

13
wc.rb
View file

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