$stdin.read
This commit is contained in:
parent
b0d547fda0
commit
d38889ea48
1 changed files with 8 additions and 9 deletions
17
wc.rb
17
wc.rb
|
@ -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
|
File.read(source)
|
||||||
puts "wc: #{source}: そのようなファイルやディレクトリはありません"
|
end
|
||||||
exit
|
count_file_stats(input)
|
||||||
end
|
|
||||||
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)
|
||||||
|
|
Loading…
Reference in a new issue