From eeb2006675a31a68b2744dbe1765273444fa2886 Mon Sep 17 00:00:00 2001 From: Rikuoh Tsujitani Date: Mon, 1 Jul 2024 09:32:50 +0900 Subject: [PATCH] wc --- wc.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 wc.rb diff --git a/wc.rb b/wc.rb new file mode 100644 index 0000000..7616467 --- /dev/null +++ b/wc.rb @@ -0,0 +1,24 @@ +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}"