From 4bd6a622df2e129f0d1042a9ebcd100b87193275 Mon Sep 17 00:00:00 2001 From: naganosinya Date: Tue, 8 Feb 2022 15:34:52 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=E8=A1=8C=E6=95=B0=E3=83=BB=E5=8D=98?= =?UTF-8?q?=E8=AA=9E=E6=95=B0=E3=83=BB=E3=83=90=E3=82=A4=E3=83=88=E6=95=B0?= =?UTF-8?q?=E3=82=92=E8=A1=A8=E7=A4=BA=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.wc/wc.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 06.wc/wc.rb diff --git a/06.wc/wc.rb b/06.wc/wc.rb new file mode 100755 index 0000000000..d16f33ae79 --- /dev/null +++ b/06.wc/wc.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'pry' +require 'optparse' + +def main + ARGV.each do |file| + print "#{File.open(file).read.count("\n")} " + print "#{File.read(file).split(/\s+/).size} " + print "#{FileTest.size(file)} " + puts file + end +end + +main From bc6554c7a6beb0cef52712136633820769a9c70c Mon Sep 17 00:00:00 2001 From: naganosinya Date: Wed, 9 Feb 2022 16:53:03 +0900 Subject: [PATCH 2/5] =?UTF-8?q?-l=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=80=81=E6=A8=99=E6=BA=96=E5=85=A5=E5=8A=9B=E3=81=AB?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.wc/wc.rb | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/06.wc/wc.rb b/06.wc/wc.rb index d16f33ae79..809174616e 100755 --- a/06.wc/wc.rb +++ b/06.wc/wc.rb @@ -4,12 +4,42 @@ require 'pry' require 'optparse' +opt = OptionParser.new +@option = {} +opt.on('-l') { |v| @option[:l] = v } +opt.parse!(ARGV) + +def display_file_info(file) + count_lines = File.read(file).lines.count + count_words = File.read(file).split(/\s+/).size + count_bytes = File.size(file) + + if @option[:l] == true + puts "#{count_lines} #{file}" + else + puts "#{count_lines} #{count_words} #{count_bytes} #{file}" + end +end + +def defile_file(argv) + argv.each do |file| + display_file_info(file) + end +end + +def display_input_info(content) + if @option[:l] == true + puts content.lines.count.to_s + else + puts "#{content.to_s.lines.count} #{content.split(/\s+/).size} #{content.size}" + end +end + def main - ARGV.each do |file| - print "#{File.open(file).read.count("\n")} " - print "#{File.read(file).split(/\s+/).size} " - print "#{FileTest.size(file)} " - puts file + if ARGV.size.zero? + display_input_info($stdin.read) + else + defile_file(ARGV) end end From becbeec1e7d04905e32d7d2aeab176bd7a9ffb71 Mon Sep 17 00:00:00 2001 From: naganosinya Date: Thu, 17 Feb 2022 12:32:08 +0900 Subject: [PATCH 3/5] =?UTF-8?q?total=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 06.wc/wc.rb | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 560a91d84e..43eb94a603 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ /test/tmp/ /test/version_tmp/ /tmp/ +.idea/ # Used by dotenv library to load environment variables. # .env diff --git a/06.wc/wc.rb b/06.wc/wc.rb index 809174616e..ac5ba6d081 100755 --- a/06.wc/wc.rb +++ b/06.wc/wc.rb @@ -14,10 +14,11 @@ def display_file_info(file) count_words = File.read(file).split(/\s+/).size count_bytes = File.size(file) - if @option[:l] == true - puts "#{count_lines} #{file}" + print count_lines + if @option[:l] + puts file else - puts "#{count_lines} #{count_words} #{count_bytes} #{file}" + print " #{count_words} #{count_bytes} #{file}\n" end end @@ -27,8 +28,22 @@ def defile_file(argv) end end +def display_total(argv) + total_line = [] + total_words = [] + total_bytes = [] + + argv.each do |file| + total_line << File.read(file).lines.count + total_words << File.read(file).split(/\s+/).size + total_bytes << File.size(file) + end + + puts "#{total_line.sum} #{total_words.sum} #{total_bytes.sum} total" +end + def display_input_info(content) - if @option[:l] == true + if @option[:l] puts content.lines.count.to_s else puts "#{content.to_s.lines.count} #{content.split(/\s+/).size} #{content.size}" @@ -40,6 +55,7 @@ def main display_input_info($stdin.read) else defile_file(ARGV) + display_total(ARGV) if ARGV.size >= 2 end end From 2270b4e70594254029abb0c1a6f2ec5cb41458c3 Mon Sep 17 00:00:00 2001 From: naganosinya Date: Thu, 17 Feb 2022 12:36:38 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=E3=82=B9=E3=83=9A=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/06.wc/wc.rb b/06.wc/wc.rb index ac5ba6d081..850b0ef822 100755 --- a/06.wc/wc.rb +++ b/06.wc/wc.rb @@ -16,7 +16,7 @@ def display_file_info(file) print count_lines if @option[:l] - puts file + puts " #{file}" else print " #{count_words} #{count_bytes} #{file}\n" end From ea6ca483738ddf3d6e7948ffd3141617c8d8180f Mon Sep 17 00:00:00 2001 From: naganosinya Date: Tue, 1 Mar 2022 15:42:53 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=E5=85=B1=E9=80=9A=E9=83=A8=E5=88=86?= =?UTF-8?q?=E3=81=AE=E6=8A=9C=E3=81=8D=E5=87=BA=E3=81=97=E3=81=A8=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.wc/wc.rb | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/06.wc/wc.rb b/06.wc/wc.rb index 850b0ef822..2f5ffca120 100755 --- a/06.wc/wc.rb +++ b/06.wc/wc.rb @@ -9,17 +9,22 @@ opt.on('-l') { |v| @option[:l] = v } opt.parse!(ARGV) +def cal_lines_count(file) + file.lines.count +end + +def cal_words_count(file) + file.split(/\s+/).size +end + def display_file_info(file) - count_lines = File.read(file).lines.count - count_words = File.read(file).split(/\s+/).size - count_bytes = File.size(file) + lines_count = cal_lines_count(File.read(file)) + words_count = cal_words_count(File.read(file)) + bytes_count = File.size(file) - print count_lines - if @option[:l] - puts " #{file}" - else - print " #{count_words} #{count_bytes} #{file}\n" - end + print lines_count + print " #{words_count} #{bytes_count}" unless @option[:l] + print " #{file}\n" end def defile_file(argv) @@ -34,20 +39,21 @@ def display_total(argv) total_bytes = [] argv.each do |file| - total_line << File.read(file).lines.count - total_words << File.read(file).split(/\s+/).size + total_line << cal_lines_count(File.read(file)) + total_words << cal_words_count(File.read(file)) total_bytes << File.size(file) end - puts "#{total_line.sum} #{total_words.sum} #{total_bytes.sum} total" + print total_line.sum + print " #{total_words.sum} #{total_bytes.sum}" unless @option[:l] + print ' total' end def display_input_info(content) - if @option[:l] - puts content.lines.count.to_s - else - puts "#{content.to_s.lines.count} #{content.split(/\s+/).size} #{content.size}" - end + line_count = cal_lines_count(content) + + print line_count + print " #{cal_words_count(content)} #{content.size}" unless @option[:l] end def main