fjord/wc.rb
2024-07-01 09:32:50 +09:00

24 lines
480 B
Ruby

def wc(file_path)
lines = 0
words = 0
bytes = 0
File.open(file_path) do |file|
file.each_line do |line|
lines += 1
words += line.split.size
bytes += line.bytesize
end
end
[lines, words, bytes]
end
file_path = ARGV[0]
if file_path.nil?
puts 'ファイルパスがありません。'
exit
end
lines, words, bytes = wc(file_path)
puts "行数:#{lines}\n単語数:#{words}\nバイト数:#{bytes}\nファイルパス:#{file_path}"