たぶん完成

This commit is contained in:
Rikuoh Tsujitani 2024-07-03 22:17:39 +09:00
parent 65c7fc830b
commit 40c822d7f1
Signed by: riq0h
GPG key ID: 010F09DEA298C717

28
wc.rb
View file

@ -8,30 +8,25 @@ bytes = 0
options = {}
OptionParser.new do |opts|
opts.on('-l') do
options[:lines] = true
end
opts.on('-w') do
options[:words] = true
end
opts.on('-c') do
options[:bytes] = true
end
opts.on('-l') { options[:lines] = true }
opts.on('-w') { options[:words] = true }
opts.on('-c') { options[:bytes] = true }
end.parse!
file_path = ARGV[0]
if file_path.nil?
puts 'ファイルパスがありません。'
input = if !ARGV.empty?
File.read(ARGV[0])
elsif !$stdin.tty?
$stdin.read
else
puts 'ファイルパスまたはパイプラインからの入力がありません。'
exit
end
File.open(file_path) do |file|
file.each_line do |line|
input.each_line do |line|
lines += 1
words += line.split.size
bytes += line.bytesize
end
end
options = { bytes: true, lines: true, words: true } if options.empty?
result = []
@ -39,4 +34,5 @@ result << lines if options[:lines]
result << words if options[:words]
result << bytes if options[:bytes]
puts "#{result.join(' ')} #{file_path}"
file_name = ARGV[0]
puts "#{result.join(' ')} #{file_name}"