メソッドの再配置
This commit is contained in:
parent
3e8b029d8e
commit
3df439edaf
1 changed files with 26 additions and 26 deletions
52
wc.rb
52
wc.rb
|
@ -2,6 +2,9 @@
|
|||
|
||||
require 'optparse'
|
||||
|
||||
options, input_sources = parse_options
|
||||
input_stream_sources(input_sources, options)
|
||||
|
||||
def parse_options
|
||||
options = {}
|
||||
OptionParser.new do |opts|
|
||||
|
@ -13,6 +16,29 @@ def parse_options
|
|||
[options, ARGV.empty? ? ['-'] : ARGV]
|
||||
end
|
||||
|
||||
def input_stream_sources(input_sources, options)
|
||||
total_stats = [0, 0, 0]
|
||||
max_widths = [0, 0, 0]
|
||||
|
||||
input_sources.each do |source|
|
||||
stats = input_stream(source)
|
||||
total_stats = update_totals(total_stats, stats)
|
||||
max_widths = update_max_widths(max_widths, stats)
|
||||
result = format_result(stats, max_widths, options)
|
||||
print_result(result, source)
|
||||
end
|
||||
|
||||
print_total(total_stats, max_widths, options) if input_sources.size > 1
|
||||
end
|
||||
|
||||
def input_stream(source)
|
||||
if source == '-'
|
||||
count_file_stats(ARGF)
|
||||
else
|
||||
count_file_stats(File.read(source))
|
||||
end
|
||||
end
|
||||
|
||||
def count_file_stats(input)
|
||||
lines = words = bytes = 0
|
||||
input.each_line do |line|
|
||||
|
@ -23,14 +49,6 @@ def count_file_stats(input)
|
|||
[lines, words, bytes]
|
||||
end
|
||||
|
||||
def process_input(source)
|
||||
if source == '-'
|
||||
count_file_stats(ARGF)
|
||||
else
|
||||
count_file_stats(File.read(source))
|
||||
end
|
||||
end
|
||||
|
||||
def update_totals(totals, stats)
|
||||
totals.zip(stats).map { |total, stat| total + stat }
|
||||
end
|
||||
|
@ -55,21 +73,3 @@ def print_total(total_stats, max_widths, options)
|
|||
total_result = format_result(total_stats, max_widths, options)
|
||||
puts "#{total_result} 合計"
|
||||
end
|
||||
|
||||
def process_input_sources(input_sources, options)
|
||||
total_stats = [0, 0, 0]
|
||||
max_widths = [0, 0, 0]
|
||||
|
||||
input_sources.each do |source|
|
||||
stats = process_input(source)
|
||||
total_stats = update_totals(total_stats, stats)
|
||||
max_widths = update_max_widths(max_widths, stats)
|
||||
result = format_result(stats, max_widths, options)
|
||||
print_result(result, source)
|
||||
end
|
||||
|
||||
print_total(total_stats, max_widths, options) if input_sources.size > 1
|
||||
end
|
||||
|
||||
options, input_sources = parse_options
|
||||
process_input_sources(input_sources, options)
|
||||
|
|
Loading…
Reference in a new issue