From 1d984890769dba248818cedfcf880d22bd4c5a99 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 30 Jun 2024 16:39:45 +0900 Subject: [PATCH 01/41] =?UTF-8?q?wc=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=81=AE=E5=85=A5=E5=8A=9B=E3=81=A8=E5=87=BA=E5=8A=9B=E3=82=92?= =?UTF-8?q?=E8=A8=98=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 99.wc_object/wc.rb | 7 +++++++ 2 files changed, 47 insertions(+) create mode 100644 05.wc/wc.rb create mode 100644 99.wc_object/wc.rb diff --git a/05.wc/wc.rb b/05.wc/wc.rb new file mode 100644 index 0000000000..7a47d611b8 --- /dev/null +++ b/05.wc/wc.rb @@ -0,0 +1,40 @@ +# ! /usr/bin/env ruby +# frozen_string_literal: true + +require 'optparse' + +def main + argv_options = parse_and_remove_options(ARGV) + while (input_text = ARGF.gets(nil)) + wc_info = [] + + wc_info = { line: count_line(input_text) } if argv_options[:l] + wc_info = { word: count_word(input_text) } if argv_options[:w] + wc_info = { bytesize: input_text.size } if argv_options[:c] + + puts "#{wc_info[:line] || ''} #{wc_info[:word] || ''} #{wc_info[:bytesize] || ''}" + end +end + +def count_line(line); end +def count_word(line); end + +def parse_and_remove_options(argv) + argv_options = {} + OptionParser.new do |opt| + opt.on('-l') { |v| argv_options[:l] = v } + opt.on('-w') { |v| argv_options[:w] = v } + opt.on('-c') { |v| argv_options[:c] = v } + # argvからオプションを取り除く + opt.parse!(argv) + end + + unless argv_options.key?(:l) || argv_options.key?(:w) || argv_options.key?(:c) + argv_options[:l] = true + argv_options[:w] = true + argv_options[:c] = true + end + argv_options +end + +main diff --git a/99.wc_object/wc.rb b/99.wc_object/wc.rb new file mode 100644 index 0000000000..3be86f73f9 --- /dev/null +++ b/99.wc_object/wc.rb @@ -0,0 +1,7 @@ +# ! /usr/bin/env ruby +# frozen_string_literal: true + +require 'optparse' + +argv_arguments = ARGV +p argv_arguments From 02457954c3c82a4e5f0a75213e0eb7ca75731b33 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 1 Jul 2024 00:48:26 +0900 Subject: [PATCH 02/41] =?UTF-8?q?l=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=80=81w=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=80=81=E8=A1=A8=E7=A4=BA=E5=BD=A2=E5=BC=8F=E3=81=AE?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 7a47d611b8..e40974eb54 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -5,19 +5,37 @@ def main argv_options = parse_and_remove_options(ARGV) - while (input_text = ARGF.gets(nil)) + output_text = [] + ARGF.each(nil) do |input_text| wc_info = [] + file_name = ARGF.filename + wc_info << [:filename, file_name] + wc_info << [:line, input_text.lines.count] if argv_options[:l] + wc_info << [:word, input_text.split(/\s+/).size] if argv_options[:w] + wc_info << [:bytesize, input_text.size] if argv_options[:c] + output_text << wc_info.to_h + end - wc_info = { line: count_line(input_text) } if argv_options[:l] - wc_info = { word: count_word(input_text) } if argv_options[:w] - wc_info = { bytesize: input_text.size } if argv_options[:c] + max_width = get_max_width(output_text) + print_output(output_text, max_width) +end - puts "#{wc_info[:line] || ''} #{wc_info[:word] || ''} #{wc_info[:bytesize] || ''}" +def print_output(output_text, max_width) + output_text.each do |output_line| + long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_width] + 2) || ''} " \ + "#{output_line[:word].to_s.rjust(max_width[:word_width]) || ''} " \ + "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_width]) || ''} " \ + "#{output_line[:filename]}" + puts long_sentence end end -def count_line(line); end -def count_word(line); end +def get_max_width(output_text) + max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max || 0 + max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max || 0 + max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length }.max || 0 + { line_width: max_lines_width, word_width: max_words_width, bytesize_width: max_bytesizes_width } +end def parse_and_remove_options(argv) argv_options = {} @@ -25,7 +43,6 @@ def parse_and_remove_options(argv) opt.on('-l') { |v| argv_options[:l] = v } opt.on('-w') { |v| argv_options[:w] = v } opt.on('-c') { |v| argv_options[:c] = v } - # argvからオプションを取り除く opt.parse!(argv) end From c32b622ee329ef24103e08269ebde8c0ffa8ea07 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 00:02:26 +0900 Subject: [PATCH 03/41] =?UTF-8?q?=E5=AE=9F=E6=85=8B=E3=82=92=E8=A1=A8?= =?UTF-8?q?=E3=81=97=E3=81=9F=E5=A4=89=E6=95=B0=E5=90=8D=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=80=82=E3=81=BE=E3=81=9F=E3=80=81=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E3=82=B9=E3=83=9A=E3=83=BC=E3=82=B9=E3=82=92CONSTANT=E3=81=A7?= =?UTF-8?q?=E8=AA=BF=E6=95=B4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 10 ++++++---- : | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 : diff --git a/05.wc/wc.rb b/05.wc/wc.rb index e40974eb54..a097fc2cf4 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -20,11 +20,13 @@ def main print_output(output_text, max_width) end +DISPLAY_ADJUST_NUMBER = 1 + def print_output(output_text, max_width) output_text.each do |output_line| - long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_width] + 2) || ''} " \ - "#{output_line[:word].to_s.rjust(max_width[:word_width]) || ''} " \ - "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_width]) || ''} " \ + long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_display_witdh]) || ''}" \ + "#{output_line[:word].to_s.rjust(max_width[:word_display_width]+DISPLAY_ADJUST_NUMBER) || ''}" \ + "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width]+DISPLAY_ADJUST_NUMBER) || ''}" \ "#{output_line[:filename]}" puts long_sentence end @@ -34,7 +36,7 @@ def get_max_width(output_text) max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max || 0 max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max || 0 max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length }.max || 0 - { line_width: max_lines_width, word_width: max_words_width, bytesize_width: max_bytesizes_width } + { line_display_witdh: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } end def parse_and_remove_options(argv) diff --git a/: b/: new file mode 100644 index 0000000000..2e062d2d64 --- /dev/null +++ b/: @@ -0,0 +1,57 @@ +# ! /usr/bin/env ruby +# frozen_string_literal: true + +require 'optparse' + +def main + argv_options = parse_and_remove_options(ARGV) + output_text = [] + ARGF.each(nil) do |input_text| + wc_info = [] + file_name = ARGF.filename + wc_info << [:filename, file_name] + wc_info << [:line, input_text.lines.count] if argv_options[:l] + wc_info << [:word, input_text.split(/\s+/).size] if argv_options[:w] + wc_info << [:bytesize, input_text.size] if argv_options[:c] + output_text << wc_info.to_h + end + + max_width = get_max_width(output_text) + print_output(output_text, max_width) +end + +def print_output(output_text, max_width) + output_text.each do |output_line| + long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_display_witdh]) || ''}" \ + "#{output_line[:word].to_s.rjust(max_width[:word_display_width]) || ''}" \ + "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width]) || ''}" \ + "#{output_line[:filename]}" + puts long_sentence + end +end + +def get_max_width(output_text) + max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max || 0 + max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max || 0 + max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length }.max || 0 + { line_word_width: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } +end + +def parse_and_remove_options(argv) + argv_options = {} + OptionParser.new do |opt| + opt.on('-l') { |v| argv_options[:l] = v } + opt.on('-w') { |v| argv_options[:w] = v } + opt.on('-c') { |v| argv_options[:c] = v } + opt.parse!(argv) + end + + unless argv_options.key?(:l) || argv_options.key?(:w) || argv_options.key?(:c) + argv_options[:l] = true + argv_options[:w] = true + argv_options[:c] = true + end + argv_options +end + +main From 1881ba610f0b7321e5cf709cd5a6ca09b907946f Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 00:17:05 +0900 Subject: [PATCH 04/41] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E3=82=B3=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=81=97=E3=81=9F=E3=81=AE=E3=81=A7=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- : | 57 --------------------------------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 : diff --git a/: b/: deleted file mode 100644 index 2e062d2d64..0000000000 --- a/: +++ /dev/null @@ -1,57 +0,0 @@ -# ! /usr/bin/env ruby -# frozen_string_literal: true - -require 'optparse' - -def main - argv_options = parse_and_remove_options(ARGV) - output_text = [] - ARGF.each(nil) do |input_text| - wc_info = [] - file_name = ARGF.filename - wc_info << [:filename, file_name] - wc_info << [:line, input_text.lines.count] if argv_options[:l] - wc_info << [:word, input_text.split(/\s+/).size] if argv_options[:w] - wc_info << [:bytesize, input_text.size] if argv_options[:c] - output_text << wc_info.to_h - end - - max_width = get_max_width(output_text) - print_output(output_text, max_width) -end - -def print_output(output_text, max_width) - output_text.each do |output_line| - long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_display_witdh]) || ''}" \ - "#{output_line[:word].to_s.rjust(max_width[:word_display_width]) || ''}" \ - "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width]) || ''}" \ - "#{output_line[:filename]}" - puts long_sentence - end -end - -def get_max_width(output_text) - max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max || 0 - max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max || 0 - max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length }.max || 0 - { line_word_width: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } -end - -def parse_and_remove_options(argv) - argv_options = {} - OptionParser.new do |opt| - opt.on('-l') { |v| argv_options[:l] = v } - opt.on('-w') { |v| argv_options[:w] = v } - opt.on('-c') { |v| argv_options[:c] = v } - opt.parse!(argv) - end - - unless argv_options.key?(:l) || argv_options.key?(:w) || argv_options.key?(:c) - argv_options[:l] = true - argv_options[:w] = true - argv_options[:c] = true - end - argv_options -end - -main From 5497ac79b7d610f180bcdfd59288166261fb85d5 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 02:07:51 +0900 Subject: [PATCH 05/41] =?UTF-8?q?=E9=9B=86=E8=A8=88=E8=A1=8C=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index a097fc2cf4..0d510ae972 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -15,28 +15,34 @@ def main wc_info << [:bytesize, input_text.size] if argv_options[:c] output_text << wc_info.to_h end - + output_text << file_total_info(output_text) max_width = get_max_width(output_text) print_output(output_text, max_width) end DISPLAY_ADJUST_NUMBER = 1 +TOTAL = '合計' + +def file_total_info(output_text) + file_total_info = output_text.flat_map(&:to_a).group_by(&:first).reject { |k, _v| k == :filename } + file_total_info.transform_values { |value| value.map(&:last).sum }.merge(filename: TOTAL) +end def print_output(output_text, max_width) output_text.each do |output_line| - long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_display_witdh]) || ''}" \ - "#{output_line[:word].to_s.rjust(max_width[:word_display_width]+DISPLAY_ADJUST_NUMBER) || ''}" \ - "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width]+DISPLAY_ADJUST_NUMBER) || ''}" \ + long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_display_width]) || ''}" \ + "#{output_line[:word].to_s.rjust(max_width[:word_display_width]) || ''}" \ + "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width] ).concat(' ') || ''}" \ "#{output_line[:filename]}" puts long_sentence end end def get_max_width(output_text) - max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max || 0 - max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max || 0 + max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max + DISPLAY_ADJUST_NUMBER || 0 + max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max + DISPLAY_ADJUST_NUMBER || 0 max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length }.max || 0 - { line_display_witdh: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } + { line_display_width: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } end def parse_and_remove_options(argv) From a95e93b32247ec32e1831e06356b063692a2ed0b Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 02:23:10 +0900 Subject: [PATCH 06/41] =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=81=AE=E8=AA=BF?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- - | 1 + 05.wc/wc.rb | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 - diff --git a/- b/- new file mode 100644 index 0000000000..804c5e03b2 --- /dev/null +++ b/- @@ -0,0 +1 @@ +rererc diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 0d510ae972..a31be613d9 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -8,7 +8,7 @@ def main output_text = [] ARGF.each(nil) do |input_text| wc_info = [] - file_name = ARGF.filename + file_name = ARGF.filename == "-" ? nil : ARGF.filename wc_info << [:filename, file_name] wc_info << [:line, input_text.lines.count] if argv_options[:l] wc_info << [:word, input_text.split(/\s+/).size] if argv_options[:w] @@ -32,16 +32,16 @@ def print_output(output_text, max_width) output_text.each do |output_line| long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_display_width]) || ''}" \ "#{output_line[:word].to_s.rjust(max_width[:word_display_width]) || ''}" \ - "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width] ).concat(' ') || ''}" \ - "#{output_line[:filename]}" + "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width] ) || ''}" \ + " #{output_line[:filename]}" puts long_sentence end end def get_max_width(output_text) - max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max + DISPLAY_ADJUST_NUMBER || 0 + max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max || 0 max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max + DISPLAY_ADJUST_NUMBER || 0 - max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length }.max || 0 + max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length + DISPLAY_ADJUST_NUMBER }.max || 0 { line_display_width: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } end From 6004c3fbe3837b19c142ecdbbeb89ad9822bbd20 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 17:08:50 +0900 Subject: [PATCH 07/41] =?UTF-8?q?-w=20-c=20=E3=82=B3=E3=83=9E=E3=83=B3?= =?UTF-8?q?=E3=83=89=E4=BD=BF=E7=94=A8=E6=99=82=E3=81=AB=E4=B8=8D=E8=A6=81?= =?UTF-8?q?=E3=81=AA=E9=9A=99=E9=96=93=E3=81=8C=E7=99=BA=E7=94=9F=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9F=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E3=80=82=E3=81=BE=E3=81=9F=E3=80=81=E4=B8=80=E9=83=A8=E8=A6=8B?= =?UTF-8?q?=E3=81=88=E6=96=B9=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index a31be613d9..bae534dff4 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -8,7 +8,7 @@ def main output_text = [] ARGF.each(nil) do |input_text| wc_info = [] - file_name = ARGF.filename == "-" ? nil : ARGF.filename + file_name = ARGF.filename wc_info << [:filename, file_name] wc_info << [:line, input_text.lines.count] if argv_options[:l] wc_info << [:word, input_text.split(/\s+/).size] if argv_options[:w] @@ -30,16 +30,16 @@ def file_total_info(output_text) def print_output(output_text, max_width) output_text.each do |output_line| - long_sentence = "#{output_line[:line].to_s.rjust(max_width[:line_display_width]) || ''}" \ - "#{output_line[:word].to_s.rjust(max_width[:word_display_width]) || ''}" \ - "#{output_line[:bytesize].to_s.rjust(max_width[:bytesize_display_width] ) || ''}" \ - " #{output_line[:filename]}" + long_sentence = "#{output_line[:line]&.to_s&.rjust(max_width[:line_display_width])}"\ + "#{output_line[:word]&.to_s&.rjust(max_width[:word_display_width])}"\ + "#{output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize_display_width])}"\ + " #{output_line[:filename] == '-' ? nil : output_line[:filename]}" puts long_sentence end end def get_max_width(output_text) - max_lines_width = output_text.map { |entry| entry[:line].to_s.length }.max || 0 + max_lines_width = output_text.map { |entry| entry[:line].to_s.length + DISPLAY_ADJUST_NUMBER }.max || 0 max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max + DISPLAY_ADJUST_NUMBER || 0 max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length + DISPLAY_ADJUST_NUMBER }.max || 0 { line_display_width: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } From 0f17f1a04b35267b32b609550f16f7da63d3ded1 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 17:57:34 +0900 Subject: [PATCH 08/41] =?UTF-8?q?=E9=9B=86=E8=A8=88=E8=A1=8C=E3=81=AF?= =?UTF-8?q?=E8=A4=87=E6=95=B0=E5=BC=95=E6=95=B0=E3=81=AE=E3=81=BF=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E3=81=95=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=82=E3=81=BE=E3=81=9F=E4=B8=80=E9=83=A8?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=81=AE=E8=A4=87=E9=9B=91=E3=81=95=E3=82=92?= =?UTF-8?q?=E8=BB=BD=E6=B8=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index bae534dff4..b1b124d253 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -15,12 +15,13 @@ def main wc_info << [:bytesize, input_text.size] if argv_options[:c] output_text << wc_info.to_h end - output_text << file_total_info(output_text) + output_text << file_total_info(output_text) if output_text.size > MULTIPLE_DISPLAY max_width = get_max_width(output_text) print_output(output_text, max_width) end DISPLAY_ADJUST_NUMBER = 1 +MULTIPLE_DISPLAY = 2 TOTAL = '合計' def file_total_info(output_text) @@ -30,14 +31,19 @@ def file_total_info(output_text) def print_output(output_text, max_width) output_text.each do |output_line| - long_sentence = "#{output_line[:line]&.to_s&.rjust(max_width[:line_display_width])}"\ - "#{output_line[:word]&.to_s&.rjust(max_width[:word_display_width])}"\ - "#{output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize_display_width])}"\ - " #{output_line[:filename] == '-' ? nil : output_line[:filename]}" - puts long_sentence + puts format_output_line(output_line, max_width) end end +def format_output_line(output_line, max_width) + line = output_line[:line]&.to_s&.rjust(max_width[:line_display_width]) + word = output_line[:word]&.to_s&.rjust(max_width[:word_display_width]) + bytesize = output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize_display_width]) + filename = output_line[:filename] == '-' ? '' : output_line[:filename] + + "#{line}#{word}#{bytesize} #{filename}" +end + def get_max_width(output_text) max_lines_width = output_text.map { |entry| entry[:line].to_s.length + DISPLAY_ADJUST_NUMBER }.max || 0 max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max + DISPLAY_ADJUST_NUMBER || 0 From 5853d2f316c4837bf2d1f0718978ae95232f3965 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 18:22:25 +0900 Subject: [PATCH 09/41] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- - | 1 - 1 file changed, 1 deletion(-) delete mode 100644 - diff --git a/- b/- deleted file mode 100644 index 804c5e03b2..0000000000 --- a/- +++ /dev/null @@ -1 +0,0 @@ -rererc From 31eca07883d5adcdc511fb3d9bb698b71afd33f6 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 7 Jul 2024 18:24:28 +0900 Subject: [PATCH 10/41] =?UTF-8?q?=E8=AA=A4=E3=81=A3=E3=81=A6=E4=BD=9C?= =?UTF-8?q?=E6=88=90=E3=81=97=E3=81=9F=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 99.wc_object/wc.rb | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 99.wc_object/wc.rb diff --git a/99.wc_object/wc.rb b/99.wc_object/wc.rb deleted file mode 100644 index 3be86f73f9..0000000000 --- a/99.wc_object/wc.rb +++ /dev/null @@ -1,7 +0,0 @@ -# ! /usr/bin/env ruby -# frozen_string_literal: true - -require 'optparse' - -argv_arguments = ARGV -p argv_arguments From fd88d652b5a4f46796f9eb188b399014ae08712e Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 13 Jul 2024 18:44:11 +0900 Subject: [PATCH 11/41] =?UTF-8?q?=E5=A4=89=E6=95=B0=E5=90=8D=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=A8=E4=B8=8D=E8=A6=81=E3=81=AA=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 66 ++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index b1b124d253..67da3f39e5 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -4,28 +4,28 @@ require 'optparse' def main - argv_options = parse_and_remove_options(ARGV) - output_text = [] + options = parse_options(ARGV) + output_texts = [] ARGF.each(nil) do |input_text| - wc_info = [] + wc_info = {} file_name = ARGF.filename - wc_info << [:filename, file_name] - wc_info << [:line, input_text.lines.count] if argv_options[:l] - wc_info << [:word, input_text.split(/\s+/).size] if argv_options[:w] - wc_info << [:bytesize, input_text.size] if argv_options[:c] - output_text << wc_info.to_h + wc_info[:filename] = file_name + wc_info[:line] = input_text.lines.count if options[:l] + wc_info[:word] = input_text.split(/\s+/).size if options[:w] + wc_info[:bytesize] = input_text.size if options[:c] + output_texts << wc_info end - output_text << file_total_info(output_text) if output_text.size > MULTIPLE_DISPLAY - max_width = get_max_width(output_text) - print_output(output_text, max_width) + output_texts << file_total_info(output_texts) if output_texts.size > MULTIPLE_DISPLAY + max_width = get_max_widths(output_texts) + print_output(output_texts, max_width) end -DISPLAY_ADJUST_NUMBER = 1 -MULTIPLE_DISPLAY = 2 +DDISPLAY_ADJUST_LENGTH = 1 +MULTIPLE_DISPLAY = 1 TOTAL = '合計' -def file_total_info(output_text) - file_total_info = output_text.flat_map(&:to_a).group_by(&:first).reject { |k, _v| k == :filename } +def file_total_info(output_texts) + file_total_info = output_texts.flat_map(&:to_a).group_by(&:first).reject { |k, _v| k == :filename } file_total_info.transform_values { |value| value.map(&:last).sum }.merge(filename: TOTAL) end @@ -36,36 +36,36 @@ def print_output(output_text, max_width) end def format_output_line(output_line, max_width) - line = output_line[:line]&.to_s&.rjust(max_width[:line_display_width]) - word = output_line[:word]&.to_s&.rjust(max_width[:word_display_width]) - bytesize = output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize_display_width]) + line = output_line[:line]&.to_s&.rjust(max_width[:line]) + word = output_line[:word]&.to_s&.rjust(max_width[:word]) + bytesize = output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize]) filename = output_line[:filename] == '-' ? '' : output_line[:filename] "#{line}#{word}#{bytesize} #{filename}" end -def get_max_width(output_text) - max_lines_width = output_text.map { |entry| entry[:line].to_s.length + DISPLAY_ADJUST_NUMBER }.max || 0 - max_words_width = output_text.map { |entry| entry[:word].to_s.length }.max + DISPLAY_ADJUST_NUMBER || 0 - max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length + DISPLAY_ADJUST_NUMBER }.max || 0 - { line_display_width: max_lines_width, word_display_width: max_words_width, bytesize_display_width: max_bytesizes_width } +def get_max_widths(output_text) + max_lines_width = output_text.map { |entry| entry[:line].to_s.length + DDISPLAY_ADJUST_LENGTH }.max + max_words_width = output_text.map { |entry| entry[:word].to_s.length + DDISPLAY_ADJUST_LENGTH }.max + max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length + DDISPLAY_ADJUST_LENGTH }.max + { line: max_lines_width, word: max_words_width, bytesize: max_bytesizes_width } end -def parse_and_remove_options(argv) - argv_options = {} +def parse_options(argv) + options = {} OptionParser.new do |opt| - opt.on('-l') { |v| argv_options[:l] = v } - opt.on('-w') { |v| argv_options[:w] = v } - opt.on('-c') { |v| argv_options[:c] = v } + opt.on('-l') { |v| options[:l] = v } + opt.on('-w') { |v| options[:w] = v } + opt.on('-c') { |v| options[:c] = v } opt.parse!(argv) end - unless argv_options.key?(:l) || argv_options.key?(:w) || argv_options.key?(:c) - argv_options[:l] = true - argv_options[:w] = true - argv_options[:c] = true + unless options.key?(:l) || options.key?(:w) || options.key?(:c) + options[:l] = true + options[:w] = true + options[:c] = true end - argv_options + options end main From 118024199325f8e95327954a14b8182ddcacfee8 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 13 Jul 2024 18:46:38 +0900 Subject: [PATCH 12/41] =?UTF-8?q?=E5=AE=9A=E6=95=B0=E3=81=AE=E5=AE=A3?= =?UTF-8?q?=E8=A8=80=E3=82=92=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 67da3f39e5..fa26038269 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,6 +3,10 @@ require 'optparse' +DDISPLAY_ADJUST_LENGTH = 1 +MULTIPLE_DISPLAY = 1 +TOTAL = '合計' + def main options = parse_options(ARGV) output_texts = [] @@ -20,9 +24,6 @@ def main print_output(output_texts, max_width) end -DDISPLAY_ADJUST_LENGTH = 1 -MULTIPLE_DISPLAY = 1 -TOTAL = '合計' def file_total_info(output_texts) file_total_info = output_texts.flat_map(&:to_a).group_by(&:first).reject { |k, _v| k == :filename } From 5ce62908799e98b2d44adbd608ce00116b1c516e Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 13 Jul 2024 19:02:14 +0900 Subject: [PATCH 13/41] =?UTF-8?q?=E5=86=97=E9=95=B7=E3=81=AA=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index fa26038269..d96fb51cc5 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -24,7 +24,6 @@ def main print_output(output_texts, max_width) end - def file_total_info(output_texts) file_total_info = output_texts.flat_map(&:to_a).group_by(&:first).reject { |k, _v| k == :filename } file_total_info.transform_values { |value| value.map(&:last).sum }.merge(filename: TOTAL) @@ -32,17 +31,13 @@ def file_total_info(output_texts) def print_output(output_text, max_width) output_text.each do |output_line| - puts format_output_line(output_line, max_width) - end -end + line = output_line[:line].to_s.rjust(max_width[:line]) if output_line[:line] + word = output_line[:word].to_s.rjust(max_width[:word]) if output_line[:word] + bytesize = output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize]) + filename = output_line[:filename] -def format_output_line(output_line, max_width) - line = output_line[:line]&.to_s&.rjust(max_width[:line]) - word = output_line[:word]&.to_s&.rjust(max_width[:word]) - bytesize = output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize]) - filename = output_line[:filename] == '-' ? '' : output_line[:filename] - - "#{line}#{word}#{bytesize} #{filename}" + puts "#{line}#{word}#{bytesize} #{filename}" + end end def get_max_widths(output_text) From 0ef908657f54fe7e54a83a64074c501f14221742 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 13 Jul 2024 19:56:57 +0900 Subject: [PATCH 14/41] =?UTF-8?q?=E3=83=AD=E3=82=B8=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=92=E5=8D=98=E7=B4=94=E3=81=AA=E3=82=82=E3=81=AE=E3=81=AB?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index d96fb51cc5..89c35ccdb4 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -25,8 +25,11 @@ def main end def file_total_info(output_texts) - file_total_info = output_texts.flat_map(&:to_a).group_by(&:first).reject { |k, _v| k == :filename } - file_total_info.transform_values { |value| value.map(&:last).sum }.merge(filename: TOTAL) + file_total_info = output_texts.inject({}) do |result, hash| + result.merge(hash) { |_key, oldvalue, newval| oldvalue + newval } + end + file_total_info[:filename] = TOTAL + file_total_info end def print_output(output_text, max_width) @@ -34,7 +37,7 @@ def print_output(output_text, max_width) line = output_line[:line].to_s.rjust(max_width[:line]) if output_line[:line] word = output_line[:word].to_s.rjust(max_width[:word]) if output_line[:word] bytesize = output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize]) - filename = output_line[:filename] + filename = output_line[:filename] puts "#{line}#{word}#{bytesize} #{filename}" end From 8ce47eb8a9f3a4b5005cbaf298e8bb255210d629 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 13 Jul 2024 20:06:21 +0900 Subject: [PATCH 15/41] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 89c35ccdb4..120b462b06 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -36,7 +36,7 @@ def print_output(output_text, max_width) output_text.each do |output_line| line = output_line[:line].to_s.rjust(max_width[:line]) if output_line[:line] word = output_line[:word].to_s.rjust(max_width[:word]) if output_line[:word] - bytesize = output_line[:bytesize]&.to_s&.rjust(max_width[:bytesize]) + bytesize = output_line[:bytesize].to_s.rjust(max_width[:bytesize]) filename = output_line[:filename] puts "#{line}#{word}#{bytesize} #{filename}" From a90767c354dff028e8cd47a61170e806032ab578 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 13 Jul 2024 20:27:00 +0900 Subject: [PATCH 16/41] =?UTF-8?q?=E5=A4=89=E6=95=B0=E5=90=8D=E3=82=92?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 120b462b06..233da2c1cd 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -26,7 +26,7 @@ def main def file_total_info(output_texts) file_total_info = output_texts.inject({}) do |result, hash| - result.merge(hash) { |_key, oldvalue, newval| oldvalue + newval } + result.merge(hash) { |_key, val1, val2| val1 + val2 } end file_total_info[:filename] = TOTAL file_total_info From 15c4248bfb2a3d968ae39b8259532f404fdaf398 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 13 Jul 2024 20:53:02 +0900 Subject: [PATCH 17/41] =?UTF-8?q?=E5=A4=89=E6=95=B0=E5=90=8D=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 233da2c1cd..351039572c 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -26,7 +26,7 @@ def main def file_total_info(output_texts) file_total_info = output_texts.inject({}) do |result, hash| - result.merge(hash) { |_key, val1, val2| val1 + val2 } + result.merge(hash) { |_key, current_val, new_val| current_val + new_val } end file_total_info[:filename] = TOTAL file_total_info From 7928ac5b5aed26aa1bf7dc8bfb3d1817f7494f04 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sun, 14 Jul 2024 09:41:23 +0900 Subject: [PATCH 18/41] =?UTF-8?q?=E4=B8=8D=E5=BF=85=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 351039572c..5a67e65d4a 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,7 +3,6 @@ require 'optparse' -DDISPLAY_ADJUST_LENGTH = 1 MULTIPLE_DISPLAY = 1 TOTAL = '合計' @@ -19,7 +18,8 @@ def main wc_info[:bytesize] = input_text.size if options[:c] output_texts << wc_info end - output_texts << file_total_info(output_texts) if output_texts.size > MULTIPLE_DISPLAY + # 複数行出力する場合は、集計行を表示する + output_texts << file_total_info(output_texts) if output_texts.size > 2 max_width = get_max_widths(output_texts) print_output(output_texts, max_width) end From 07f858b651e101378b0f4655c29a9830d4358346 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 15:03:52 +0900 Subject: [PATCH 19/41] =?UTF-8?q?=E5=89=8A=E9=99=A4=E3=81=97=E3=81=9F?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E3=81=8C=E8=AA=A4=E3=81=A3=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=81=9F=E3=81=9F=E3=82=81=E3=80=81=E6=AD=A3=E3=81=97=E3=81=84?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 5a67e65d4a..53ec7c222e 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,7 +3,7 @@ require 'optparse' -MULTIPLE_DISPLAY = 1 +DDISPLAY_ADJUST_LENGTH = 1 TOTAL = '合計' def main From f88ef39ba834523a1368a5a2d74d86b8a150bacc Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 15:05:58 +0900 Subject: [PATCH 20/41] =?UTF-8?q?ARGF.filename=E3=82=92=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E6=A0=BC=E7=B4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 53ec7c222e..3f7d62ab29 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -11,8 +11,8 @@ def main output_texts = [] ARGF.each(nil) do |input_text| wc_info = {} - file_name = ARGF.filename - wc_info[:filename] = file_name + + wc_info[:filename] = ARGF.filename wc_info[:line] = input_text.lines.count if options[:l] wc_info[:word] = input_text.split(/\s+/).size if options[:w] wc_info[:bytesize] = input_text.size if options[:c] From 152e4bec79d944d6460fbff19a6600ea835b1bf1 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 15:36:14 +0900 Subject: [PATCH 21/41] =?UTF-8?q?=E9=96=A2=E6=95=B0=E5=90=8D,=E5=A4=89?= =?UTF-8?q?=E6=95=B0=E5=90=8D=E3=82=92=E3=82=88=E3=82=8A=E8=AA=AC=E6=98=8E?= =?UTF-8?q?=E7=9A=84=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 3f7d62ab29..38001a6918 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -8,45 +8,46 @@ def main options = parse_options(ARGV) - output_texts = [] + wc_count_results = [] ARGF.each(nil) do |input_text| - wc_info = {} + wc_count_result = {} - wc_info[:filename] = ARGF.filename - wc_info[:line] = input_text.lines.count if options[:l] - wc_info[:word] = input_text.split(/\s+/).size if options[:w] - wc_info[:bytesize] = input_text.size if options[:c] - output_texts << wc_info + wc_count_result[:filename] = ARGF.filename + wc_count_result[:line] = input_text.lines.count if options[:l] + wc_count_result[:word] = input_text.split(/\s+/).size if options[:w] + wc_count_result[:bytesize] = input_text.size if options[:c] + wc_count_results << wc_count_result end + wc_results = wc_count_results # 複数行出力する場合は、集計行を表示する - output_texts << file_total_info(output_texts) if output_texts.size > 2 - max_width = get_max_widths(output_texts) - print_output(output_texts, max_width) + wc_results << get_total_row(wc_count_results) if wc_count_results.size > 2 + max_column_widths = get_max_column_widths(wc_results) + show_wc_results(wc_results, max_column_widths) end -def file_total_info(output_texts) - file_total_info = output_texts.inject({}) do |result, hash| - result.merge(hash) { |_key, current_val, new_val| current_val + new_val } +def get_total_row(wc_count_results) + total_row = wc_count_results.inject({}) do |result, wc_count_data| + result.merge(wc_count_data) { |_key, current_val, adding_value| current_val + adding_value } end - file_total_info[:filename] = TOTAL - file_total_info + total_row[:filename] = TOTAL + total_row end -def print_output(output_text, max_width) - output_text.each do |output_line| - line = output_line[:line].to_s.rjust(max_width[:line]) if output_line[:line] - word = output_line[:word].to_s.rjust(max_width[:word]) if output_line[:word] - bytesize = output_line[:bytesize].to_s.rjust(max_width[:bytesize]) +def show_wc_results(wc_results, max_column_widths) + wc_results.each do |output_line| + line = output_line[:line].to_s.rjust(max_column_widths[:line]) if output_line[:line] + word = output_line[:word].to_s.rjust(max_column_widths[:word]) if output_line[:word] + bytesize = output_line[:bytesize].to_s.rjust(max_column_widths[:bytesize]) filename = output_line[:filename] puts "#{line}#{word}#{bytesize} #{filename}" end end -def get_max_widths(output_text) - max_lines_width = output_text.map { |entry| entry[:line].to_s.length + DDISPLAY_ADJUST_LENGTH }.max - max_words_width = output_text.map { |entry| entry[:word].to_s.length + DDISPLAY_ADJUST_LENGTH }.max - max_bytesizes_width = output_text.map { |entry| entry[:bytesize].to_s.length + DDISPLAY_ADJUST_LENGTH }.max +def get_max_column_widths(wc_results) + max_lines_width = wc_results.map { |entry| entry[:line].to_s.length + DDISPLAY_ADJUST_LENGTH }.max + max_words_width = wc_results.map { |entry| entry[:word].to_s.length + DDISPLAY_ADJUST_LENGTH }.max + max_bytesizes_width = wc_results.map { |entry| entry[:bytesize].to_s.length + DDISPLAY_ADJUST_LENGTH }.max { line: max_lines_width, word: max_words_width, bytesize: max_bytesizes_width } end From c31302946446616eda7eb7025166371ef9052de0 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 15:39:02 +0900 Subject: [PATCH 22/41] =?UTF-8?q?get=5Fmax=5Fcolumn=5Fwidths=E3=81=AE?= =?UTF-8?q?=E6=A9=9F=E8=83=BD=E3=81=8C=E7=9B=B4=E6=84=9F=E7=9A=84=E3=81=AB?= =?UTF-8?q?=E3=82=8F=E3=81=8B=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 38001a6918..d671a8ac74 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -35,9 +35,9 @@ def get_total_row(wc_count_results) def show_wc_results(wc_results, max_column_widths) wc_results.each do |output_line| - line = output_line[:line].to_s.rjust(max_column_widths[:line]) if output_line[:line] - word = output_line[:word].to_s.rjust(max_column_widths[:word]) if output_line[:word] - bytesize = output_line[:bytesize].to_s.rjust(max_column_widths[:bytesize]) + line = output_line[:line].to_s.rjust(max_column_widths[:line] + DDISPLAY_ADJUST_LENGTH) if output_line[:line] + word = output_line[:word].to_s.rjust(max_column_widths[:word] + DDISPLAY_ADJUST_LENGTH) if output_line[:word] + bytesize = output_line[:bytesize].to_s.rjust(max_column_widths[:bytesize] + DDISPLAY_ADJUST_LENGTH) filename = output_line[:filename] puts "#{line}#{word}#{bytesize} #{filename}" @@ -45,9 +45,9 @@ def show_wc_results(wc_results, max_column_widths) end def get_max_column_widths(wc_results) - max_lines_width = wc_results.map { |entry| entry[:line].to_s.length + DDISPLAY_ADJUST_LENGTH }.max - max_words_width = wc_results.map { |entry| entry[:word].to_s.length + DDISPLAY_ADJUST_LENGTH }.max - max_bytesizes_width = wc_results.map { |entry| entry[:bytesize].to_s.length + DDISPLAY_ADJUST_LENGTH }.max + max_lines_width = wc_results.map { |entry| entry[:line].to_s.length }.max + max_words_width = wc_results.map { |entry| entry[:word].to_s.length }.max + max_bytesizes_width = wc_results.map { |entry| entry[:bytesize].to_s.length }.max { line: max_lines_width, word: max_words_width, bytesize: max_bytesizes_width } end From 3f04331ed918eb59b959771f8920468275d6b4d1 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 15:40:35 +0900 Subject: [PATCH 23/41] =?UTF-8?q?2=E5=80=8B=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=97=E3=81=9F=E9=9A=9B?= =?UTF-8?q?=E3=81=AB=E9=9B=86=E8=A8=88=E6=A5=AD=E3=81=8C=E5=87=BA=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=83=90=E3=82=B0=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index d671a8ac74..7ef39272b4 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -20,7 +20,7 @@ def main end wc_results = wc_count_results # 複数行出力する場合は、集計行を表示する - wc_results << get_total_row(wc_count_results) if wc_count_results.size > 2 + wc_results << get_total_row(wc_count_results) if wc_count_results.size > 1 max_column_widths = get_max_column_widths(wc_results) show_wc_results(wc_results, max_column_widths) end From 3572c720e1c0e4e67582fb380f05a3ae53079a8c Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 15:43:14 +0900 Subject: [PATCH 24/41] =?UTF-8?q?=E5=86=97=E9=95=B7=E3=81=AA=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E3=82=92=E7=B0=A1=E6=BD=94=E3=81=AB=E8=A8=98?= =?UTF-8?q?=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 7ef39272b4..87aade4c6c 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -60,7 +60,7 @@ def parse_options(argv) opt.parse!(argv) end - unless options.key?(:l) || options.key?(:w) || options.key?(:c) + if options.empty? options[:l] = true options[:w] = true options[:c] = true From 8480673c2fa9d57fbd6de192254d4822d81b8bdf Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 23:27:26 +0900 Subject: [PATCH 25/41] =?UTF-8?q?=E5=A4=89=E6=95=B0=E5=90=8D=E3=82=92?= =?UTF-8?q?=E8=AA=AC=E6=98=8E=E7=9A=84=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 57 ++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 87aade4c6c..b5d431c0f6 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,51 +3,54 @@ require 'optparse' -DDISPLAY_ADJUST_LENGTH = 1 +SHOW_ADJUST_SPACE_SIZE = 1 TOTAL = '合計' def main options = parse_options(ARGV) - wc_count_results = [] + counts = [] ARGF.each(nil) do |input_text| - wc_count_result = {} + count = {} - wc_count_result[:filename] = ARGF.filename - wc_count_result[:line] = input_text.lines.count if options[:l] - wc_count_result[:word] = input_text.split(/\s+/).size if options[:w] - wc_count_result[:bytesize] = input_text.size if options[:c] - wc_count_results << wc_count_result + count[:filename] = ARGF.filename + count[:line] = input_text.lines.count if options[:l] + count[:word] = input_text.split(/\s+/).size if options[:w] + count[:bytesize] = input_text.size if options[:c] + counts << count end - wc_results = wc_count_results + rows = counts # 複数行出力する場合は、集計行を表示する - wc_results << get_total_row(wc_count_results) if wc_count_results.size > 1 - max_column_widths = get_max_column_widths(wc_results) - show_wc_results(wc_results, max_column_widths) + rows << get_wc_total_row(counts) if counts.size > 1 + max_column_widths = get_max_column_widths(rows) + show_wc_rows(rows, max_column_widths) end -def get_total_row(wc_count_results) - total_row = wc_count_results.inject({}) do |result, wc_count_data| - result.merge(wc_count_data) { |_key, current_val, adding_value| current_val + adding_value } +def get_wc_total_row(counts) + total_row = counts.inject({}) do |result, column_name| + result.merge(column_name) { |_key, current_val, adding_value| current_val + adding_value } end total_row[:filename] = TOTAL total_row end -def show_wc_results(wc_results, max_column_widths) - wc_results.each do |output_line| - line = output_line[:line].to_s.rjust(max_column_widths[:line] + DDISPLAY_ADJUST_LENGTH) if output_line[:line] - word = output_line[:word].to_s.rjust(max_column_widths[:word] + DDISPLAY_ADJUST_LENGTH) if output_line[:word] - bytesize = output_line[:bytesize].to_s.rjust(max_column_widths[:bytesize] + DDISPLAY_ADJUST_LENGTH) - filename = output_line[:filename] - - puts "#{line}#{word}#{bytesize} #{filename}" +def show_wc_rows(rows, max_column_widths) + rows.each do |row| + cells = {} + row.each_pair do |column_name, cell| + cells[column_name] = if column_name == :filename + cell + else + cell.to_s.rjust(max_column_widths[column_name] + SHOW_ADJUST_SPACE_SIZE) + end + end + puts "#{cells[:line]}#{cells[:word]}#{cells[:bytesize]} #{cells[:filename]}" end end -def get_max_column_widths(wc_results) - max_lines_width = wc_results.map { |entry| entry[:line].to_s.length }.max - max_words_width = wc_results.map { |entry| entry[:word].to_s.length }.max - max_bytesizes_width = wc_results.map { |entry| entry[:bytesize].to_s.length }.max +def get_max_column_widths(wc_rows) + max_lines_width = wc_rows.map { |entry| entry[:line].to_s.length }.max + max_words_width = wc_rows.map { |entry| entry[:word].to_s.length }.max + max_bytesizes_width = wc_rows.map { |entry| entry[:bytesize].to_s.length }.max { line: max_lines_width, word: max_words_width, bytesize: max_bytesizes_width } end From 1d6ca7c70b57d83d3a4e8cea9c18441f5c35cfca Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 23:31:14 +0900 Subject: [PATCH 26/41] =?UTF-8?q?=E6=A8=99=E6=BA=96=E5=85=A5=E5=8A=9B?= =?UTF-8?q?=E3=81=AE=E4=BE=8B=E5=A4=96=E7=9A=84=E3=81=AAfilename=E3=81=AB?= =?UTF-8?q?=E5=AF=BE=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index b5d431c0f6..79bd143edc 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -4,6 +4,7 @@ require 'optparse' SHOW_ADJUST_SPACE_SIZE = 1 +STANDARD_INPUT_EXCEPTIONAL_WORD = '-' TOTAL = '合計' def main @@ -25,7 +26,7 @@ def main show_wc_rows(rows, max_column_widths) end -def get_wc_total_row(counts) +def get__total_row(counts) total_row = counts.inject({}) do |result, column_name| result.merge(column_name) { |_key, current_val, adding_value| current_val + adding_value } end @@ -38,7 +39,8 @@ def show_wc_rows(rows, max_column_widths) cells = {} row.each_pair do |column_name, cell| cells[column_name] = if column_name == :filename - cell + # 標準入力を受け取る際に例外的に"-"が入力されるため除外する + cell unless cell == STANDARD_INPUT_EXCEPTIONAL_WORD else cell.to_s.rjust(max_column_widths[column_name] + SHOW_ADJUST_SPACE_SIZE) end From 7900ee3483a242b480e7a94371f036c6b7434abd Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Mon, 15 Jul 2024 23:40:27 +0900 Subject: [PATCH 27/41] =?UTF-8?q?input=5Ftext=E3=81=8C=E6=8A=BD=E8=B1=A1?= =?UTF-8?q?=E7=9A=84=E3=81=99=E3=82=8B=E3=82=AE=E3=83=AB=E3=81=9F=E3=82=81?= =?UTF-8?q?=E5=85=B7=E4=BD=93=E7=9A=84=E3=81=AB=E3=80=82=E3=81=BE=E3=81=9F?= =?UTF-8?q?=E3=80=81=E9=96=A2=E6=95=B0=E5=90=8D=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 79bd143edc..f1becf2faa 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -10,13 +10,13 @@ def main options = parse_options(ARGV) counts = [] - ARGF.each(nil) do |input_text| + ARGF.each(nil) do |standard_input_or_file| count = {} count[:filename] = ARGF.filename - count[:line] = input_text.lines.count if options[:l] - count[:word] = input_text.split(/\s+/).size if options[:w] - count[:bytesize] = input_text.size if options[:c] + count[:line] = standard_input_or_file.lines.count if options[:l] + count[:word] = standard_input_or_file.split(/\s+/).size if options[:w] + count[:bytesize] = standard_input_or_file.size if options[:c] counts << count end rows = counts @@ -26,7 +26,7 @@ def main show_wc_rows(rows, max_column_widths) end -def get__total_row(counts) +def get_wc_total_row(counts) total_row = counts.inject({}) do |result, column_name| result.merge(column_name) { |_key, current_val, adding_value| current_val + adding_value } end From 955cd88afa18f78c2eabfecc2a48ec33cb8ee4bb Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Wed, 17 Jul 2024 22:56:35 +0900 Subject: [PATCH 28/41] =?UTF-8?q?=E5=A4=89=E6=95=B0=E5=90=8D=E3=81=A8?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E5=90=8D=E3=82=92=E3=82=8F=E3=81=8B=E3=82=8A?= =?UTF-8?q?=E3=82=84=E3=81=99=E3=81=8F=E3=80=82=E3=83=AD=E3=82=B8=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=82=E8=AA=AD=E3=81=BF=E3=82=84=E3=81=99=E3=81=8F?= =?UTF-8?q?=E3=81=AA=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index f1becf2faa..50fb7d719a 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,56 +3,58 @@ require 'optparse' -SHOW_ADJUST_SPACE_SIZE = 1 -STANDARD_INPUT_EXCEPTIONAL_WORD = '-' +SEPARATOR_SIZE = 1 +STDIN_FILENAME = '-' TOTAL = '合計' def main options = parse_options(ARGV) counts = [] - ARGF.each(nil) do |standard_input_or_file| + ARGF.each(nil) do |input_text| count = {} count[:filename] = ARGF.filename - count[:line] = standard_input_or_file.lines.count if options[:l] - count[:word] = standard_input_or_file.split(/\s+/).size if options[:w] - count[:bytesize] = standard_input_or_file.size if options[:c] + count[:line] = input_text.lines.count if options[:l] + count[:word] = input_text.split(/\s+/).size if options[:w] + count[:bytesize] = input_text.size if options[:c] counts << count end - rows = counts - # 複数行出力する場合は、集計行を表示する - rows << get_wc_total_row(counts) if counts.size > 1 - max_column_widths = get_max_column_widths(rows) - show_wc_rows(rows, max_column_widths) + counts << calculate_counts_total(counts) if counts.size > 1 + max_column_widths = calculate_wc_max_column_widths(counts) + show_wc_rows(counts, max_column_widths) end -def get_wc_total_row(counts) - total_row = counts.inject({}) do |result, column_name| - result.merge(column_name) { |_key, current_val, adding_value| current_val + adding_value } +def calculate_counts_total(counts) + counts_without_filename = counts.map { |count| count.except(:filename) } + + total = counts_without_filename.inject({}) do |result, count| + result.merge(count) do |_key, current_val, adding_value| + current_val + adding_value + end end - total_row[:filename] = TOTAL - total_row + total[:filename] = TOTAL + total end def show_wc_rows(rows, max_column_widths) rows.each do |row| cells = {} - row.each_pair do |column_name, cell| + row.each do |column_name, cell| cells[column_name] = if column_name == :filename - # 標準入力を受け取る際に例外的に"-"が入力されるため除外する - cell unless cell == STANDARD_INPUT_EXCEPTIONAL_WORD + cell unless cell == STDIN_FILENAME else - cell.to_s.rjust(max_column_widths[column_name] + SHOW_ADJUST_SPACE_SIZE) + column_width = max_column_widths[column_name] + SEPARATOR_SIZE + cell.to_s.rjust(column_width) end end puts "#{cells[:line]}#{cells[:word]}#{cells[:bytesize]} #{cells[:filename]}" end end -def get_max_column_widths(wc_rows) - max_lines_width = wc_rows.map { |entry| entry[:line].to_s.length }.max - max_words_width = wc_rows.map { |entry| entry[:word].to_s.length }.max - max_bytesizes_width = wc_rows.map { |entry| entry[:bytesize].to_s.length }.max +def calculate_wc_max_column_widths(rows) + max_lines_width = rows.map { |entry| entry[:line].to_s.length }.max + max_words_width = rows.map { |entry| entry[:word].to_s.length }.max + max_bytesizes_width = rows.map { |entry| entry[:bytesize].to_s.length }.max { line: max_lines_width, word: max_words_width, bytesize: max_bytesizes_width } end From a1770ba0fd5fdc99b17480e07f386683883a4362 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Thu, 18 Jul 2024 21:20:14 +0900 Subject: [PATCH 29/41] =?UTF-8?q?clean:=E5=A4=89=E6=95=B0=E5=90=8D?= =?UTF-8?q?=E3=81=A8=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E5=90=8D=E3=82=92?= =?UTF-8?q?=E3=82=88=E3=82=8A=E3=82=8F=E3=81=8B=E3=82=8A=E3=82=84=E3=81=99?= =?UTF-8?q?=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 56 ++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 50fb7d719a..0e3a728fe7 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -9,53 +9,51 @@ def main options = parse_options(ARGV) - counts = [] + rows = [] ARGF.each(nil) do |input_text| - count = {} + counts = {} - count[:filename] = ARGF.filename - count[:line] = input_text.lines.count if options[:l] - count[:word] = input_text.split(/\s+/).size if options[:w] - count[:bytesize] = input_text.size if options[:c] - counts << count + counts[:filename] = ARGF.filename + counts[:line] = input_text.lines.count if options[:l] + counts[:word] = input_text.split(/\s+/).size if options[:w] + counts[:byte] = input_text.size if options[:c] + rows << counts end - counts << calculate_counts_total(counts) if counts.size > 1 - max_column_widths = calculate_wc_max_column_widths(counts) - show_wc_rows(counts, max_column_widths) + rows << calculate_total_rows(rows) if rows.size > 1 + max_widths = calculate_max_widths(rows) + show_rows(rows, max_widths) end -def calculate_counts_total(counts) - counts_without_filename = counts.map { |count| count.except(:filename) } - - total = counts_without_filename.inject({}) do |result, count| - result.merge(count) do |_key, current_val, adding_value| - current_val + adding_value +def calculate_total_rows(rows) + total = rows.inject({}) do |result, count| + result.merge(count) do |key, current_val, adding_value| + current_val + adding_value unless key == :filename end end total[:filename] = TOTAL total end -def show_wc_rows(rows, max_column_widths) +def show_rows(rows, max_column_widths) rows.each do |row| - cells = {} - row.each do |column_name, cell| - cells[column_name] = if column_name == :filename - cell unless cell == STDIN_FILENAME - else - column_width = max_column_widths[column_name] + SEPARATOR_SIZE - cell.to_s.rjust(column_width) - end + columns = {} + row.each do |name, value| + columns[name] = if name == :filename + value unless value == STDIN_FILENAME + else + column_width = max_column_widths[name] + value.to_s.rjust(column_width) + ' ' * SEPARATOR_SIZE + end end - puts "#{cells[:line]}#{cells[:word]}#{cells[:bytesize]} #{cells[:filename]}" + puts columns.values_at(:line, :word, :byte, :filename).join end end -def calculate_wc_max_column_widths(rows) +def calculate_max_widths(rows) max_lines_width = rows.map { |entry| entry[:line].to_s.length }.max max_words_width = rows.map { |entry| entry[:word].to_s.length }.max - max_bytesizes_width = rows.map { |entry| entry[:bytesize].to_s.length }.max - { line: max_lines_width, word: max_words_width, bytesize: max_bytesizes_width } + max_bytes_width = rows.map { |entry| entry[:byte].to_s.length }.max + { line: max_lines_width, word: max_words_width, byte: max_bytes_width } end def parse_options(argv) From 40fdf93e74a440189103eea59fd4b75669283fe7 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Thu, 18 Jul 2024 21:44:11 +0900 Subject: [PATCH 30/41] =?UTF-8?q?clean:calculate=5Fmax=5Fwitdhs=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E3=82=92DRY=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 0e3a728fe7..4395f524b6 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -50,10 +50,12 @@ def show_rows(rows, max_column_widths) end def calculate_max_widths(rows) - max_lines_width = rows.map { |entry| entry[:line].to_s.length }.max - max_words_width = rows.map { |entry| entry[:word].to_s.length }.max - max_bytes_width = rows.map { |entry| entry[:byte].to_s.length }.max - { line: max_lines_width, word: max_words_width, byte: max_bytes_width } + max_widths = {} + keys = rows.map(&:keys).flatten.uniq + keys.each do |name| + max_widths[name] = rows.map { |entry| entry[name].to_s.length }.max unless name == :filename + end + max_widths end def parse_options(argv) From 0d3f3c17d47b594f923cc992d4a4e8f55ee08466 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Thu, 18 Jul 2024 21:52:54 +0900 Subject: [PATCH 31/41] =?UTF-8?q?clean:=E3=83=96=E3=83=AD=E3=83=83?= =?UTF-8?q?=E3=82=AF=E5=86=85=E3=81=AE=E5=A4=89=E6=95=B0=E3=82=92=E3=83=AC?= =?UTF-8?q?=E3=82=B7=E3=83=BC=E3=83=90=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 4395f524b6..0efe670f37 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -52,8 +52,8 @@ def show_rows(rows, max_column_widths) def calculate_max_widths(rows) max_widths = {} keys = rows.map(&:keys).flatten.uniq - keys.each do |name| - max_widths[name] = rows.map { |entry| entry[name].to_s.length }.max unless name == :filename + keys.each do |key| + max_widths[key] = rows.map { |entry| entry[key].to_s.length }.max unless key == :filename end max_widths end From eee8b7e993ee5880e1d11393831688b905ceba8c Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Fri, 19 Jul 2024 12:09:35 +0900 Subject: [PATCH 32/41] =?UTF-8?q?clean:=E8=A1=A8=E7=A4=BA=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=82=B9=E3=83=9A=E3=83=BC=E3=82=B9=E3=81=AE=E6=8C=BF?= =?UTF-8?q?=E5=85=A5=E3=82=92=E3=82=88=E3=82=8A=E5=88=86=E3=81=8B=E3=82=8A?= =?UTF-8?q?=E3=82=84=E3=81=99=E3=81=84=E5=BD=A2=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 0efe670f37..9ad169c254 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,7 +3,6 @@ require 'optparse' -SEPARATOR_SIZE = 1 STDIN_FILENAME = '-' TOTAL = '合計' @@ -42,10 +41,10 @@ def show_rows(rows, max_column_widths) value unless value == STDIN_FILENAME else column_width = max_column_widths[name] - value.to_s.rjust(column_width) + ' ' * SEPARATOR_SIZE + value.to_s.rjust(column_width) end end - puts columns.values_at(:line, :word, :byte, :filename).join + puts columns.values_at(:line, :word, :byte, :filename).join(' ') end end From 2a3f36b7e6eefa171014e79cc98fc2371087ee52 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Fri, 19 Jul 2024 12:18:48 +0900 Subject: [PATCH 33/41] =?UTF-8?q?clean:=E5=A4=89=E6=95=B0=E5=90=8D?= =?UTF-8?q?=E3=82=92=E3=82=88=E3=82=8A=E4=B8=80=E8=B2=AB=E3=81=97=E3=81=9F?= =?UTF-8?q?=E5=BD=A2=E3=81=AB=E5=A4=89=E6=9B=B4=E3=80=82=E3=81=BE=E3=81=9F?= =?UTF-8?q?=E3=80=81=E6=9D=A1=E4=BB=B6=E5=BC=8F=E3=82=92=E3=82=88=E3=82=8A?= =?UTF-8?q?=E8=A6=8B=E3=82=84=E3=81=99=E3=81=84=E5=BD=A2=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 9ad169c254..eab8becd84 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -24,8 +24,8 @@ def main end def calculate_total_rows(rows) - total = rows.inject({}) do |result, count| - result.merge(count) do |key, current_val, adding_value| + total = rows.inject({}) do |result, counts| + result.merge(counts) do |key, current_val, adding_value| current_val + adding_value unless key == :filename end end @@ -33,15 +33,15 @@ def calculate_total_rows(rows) total end -def show_rows(rows, max_column_widths) - rows.each do |row| +def show_rows(rows, max_widths) + rows.each do |counts| columns = {} - row.each do |name, value| + counts.each do |name, value| columns[name] = if name == :filename value unless value == STDIN_FILENAME else - column_width = max_column_widths[name] - value.to_s.rjust(column_width) + width = max_widths[name] + value.to_s.rjust(width) end end puts columns.values_at(:line, :word, :byte, :filename).join(' ') @@ -66,11 +66,7 @@ def parse_options(argv) opt.parse!(argv) end - if options.empty? - options[:l] = true - options[:w] = true - options[:c] = true - end + options.empty? ? { l: true, w: true, c: true } : options options end From 8ecdc269207c1277f9ff1d0bc960eee10c3b7681 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Fri, 19 Jul 2024 12:29:34 +0900 Subject: [PATCH 34/41] =?UTF-8?q?fix:=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E6=8C=87=E5=AE=9A=E3=81=97=E3=81=AA=E3=81=84=E9=9A=9B?= =?UTF-8?q?=E3=81=AB=E3=80=81-lwc=E3=81=8C=E6=8C=87=E5=AE=9A=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=82=8B=E3=81=AF=E3=81=9A=E3=81=A0=E3=81=8C=E3=80=81?= =?UTF-8?q?=E3=83=90=E3=82=B0=E3=81=8C=E6=B7=B7=E5=85=A5=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=81=9F=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index eab8becd84..597db3482a 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -67,7 +67,6 @@ def parse_options(argv) end options.empty? ? { l: true, w: true, c: true } : options - options end main From 96f3e9f1ba9e7dc08c37a7f8875288480ce1c85b Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Fri, 19 Jul 2024 12:33:05 +0900 Subject: [PATCH 35/41] =?UTF-8?q?clean:=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89calulate=5Fmax=5Fwidths=E3=82=92=E3=82=88=E3=82=8A?= =?UTF-8?q?=E5=88=86=E3=81=8B=E3=82=8A=E3=82=84=E3=81=99=E3=81=84=E5=BD=A2?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 597db3482a..773e07e951 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -50,9 +50,9 @@ def show_rows(rows, max_widths) def calculate_max_widths(rows) max_widths = {} - keys = rows.map(&:keys).flatten.uniq - keys.each do |key| - max_widths[key] = rows.map { |entry| entry[key].to_s.length }.max unless key == :filename + max_widths = %i[line word byte].to_h do |name| + max_width = rows.map { |counts| counts[name].to_s.length }.max + [name,max_width] end max_widths end From 18b16c4c5b2b8ee89089faaea59bafcb8d702b28 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Fri, 19 Jul 2024 12:37:45 +0900 Subject: [PATCH 36/41] clean:rubocop --- 05.wc/wc.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 773e07e951..e4a5952155 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -49,12 +49,10 @@ def show_rows(rows, max_widths) end def calculate_max_widths(rows) - max_widths = {} - max_widths = %i[line word byte].to_h do |name| + %i[line word byte].to_h do |name| max_width = rows.map { |counts| counts[name].to_s.length }.max - [name,max_width] + [name, max_width] end - max_widths end def parse_options(argv) From ceb6a3ddeb17d1d53ae0165bbfcf1da6d49afb6a Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 20 Jul 2024 09:59:16 +0900 Subject: [PATCH 37/41] =?UTF-8?q?clean:build=5Frows=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=AB=E6=97=A2=E5=AD=98=E3=81=AEmain?= =?UTF-8?q?=E3=81=AE=E3=83=AD=E3=82=B8=E3=83=83=E3=82=AF=E3=82=92=E5=88=87?= =?UTF-8?q?=E3=82=8A=E5=87=BA=E3=81=97=E3=80=81=E3=83=A1=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=AE=E8=A6=8B=E9=80=9A?= =?UTF-8?q?=E3=81=97=E3=82=92=E5=90=91=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index e4a5952155..ee7353ff7a 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -8,6 +8,13 @@ def main options = parse_options(ARGV) + rows = build_rows(options) + rows << calculate_total_rows(rows) if rows.size > 1 + max_widths = calculate_max_widths(rows) + show_rows(rows, max_widths) +end + +def build_rows(options) rows = [] ARGF.each(nil) do |input_text| counts = {} @@ -18,9 +25,7 @@ def main counts[:byte] = input_text.size if options[:c] rows << counts end - rows << calculate_total_rows(rows) if rows.size > 1 - max_widths = calculate_max_widths(rows) - show_rows(rows, max_widths) + rows end def calculate_total_rows(rows) From a418071d9bbe1b46b74888ad2dc3f5505373ffc0 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 20 Jul 2024 10:08:21 +0900 Subject: [PATCH 38/41] =?UTF-8?q?clean:=E6=AD=A3=E8=A6=8F=E8=A1=A8?= =?UTF-8?q?=E7=8F=BE=E3=81=AB=E7=BD=AE=E3=81=8D=E6=8F=9B=E3=81=88=E3=82=8B?= =?UTF-8?q?=E3=81=93=E3=81=A8=E3=81=A7=E6=9D=A1=E4=BB=B6=E5=88=86=E5=B2=90?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index ee7353ff7a..504afa73ac 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,7 +3,7 @@ require 'optparse' -STDIN_FILENAME = '-' +STDIN_FILENAME = /^-$/ TOTAL = '合計' def main @@ -43,7 +43,7 @@ def show_rows(rows, max_widths) columns = {} counts.each do |name, value| columns[name] = if name == :filename - value unless value == STDIN_FILENAME + value.sub(STDIN_FILENAME,"") else width = max_widths[name] value.to_s.rjust(width) From 758644ce702fd08f341404fa85c869b5fd8b32b7 Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 20 Jul 2024 10:16:47 +0900 Subject: [PATCH 39/41] =?UTF-8?q?clean:=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=81=AE=E5=86=8D=E5=88=A9=E7=94=A8=E6=80=A7=E3=82=92?= =?UTF-8?q?=E5=90=91=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 504afa73ac..b7406b632a 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -10,8 +10,7 @@ def main options = parse_options(ARGV) rows = build_rows(options) rows << calculate_total_rows(rows) if rows.size > 1 - max_widths = calculate_max_widths(rows) - show_rows(rows, max_widths) + show_rows(rows) end def build_rows(options) @@ -38,7 +37,8 @@ def calculate_total_rows(rows) total end -def show_rows(rows, max_widths) +def show_rows(rows) + max_widths = calculate_max_widths(rows) rows.each do |counts| columns = {} counts.each do |name, value| From 3bc64b58a92e4866717223c3525055a986068d2a Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 20 Jul 2024 10:20:00 +0900 Subject: [PATCH 40/41] =?UTF-8?q?clean:build=5Frows=E3=81=AE=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E3=81=AB=E9=9B=86=E8=A8=88=E8=A1=8C?= =?UTF-8?q?=E3=81=AE=E8=BF=BD=E5=8A=A0=E5=87=A6=E7=90=86=E3=82=92=E5=90=AB?= =?UTF-8?q?=E3=82=80=E3=81=A8=E3=81=BE=E3=81=A8=E3=81=BE=E3=82=8A=E3=81=8C?= =?UTF-8?q?=E3=82=88=E3=81=8F=E3=81=AA=E3=82=8B=E3=81=A8=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=81=9F=E3=82=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index b7406b632a..5bb4cefe10 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -9,7 +9,6 @@ def main options = parse_options(ARGV) rows = build_rows(options) - rows << calculate_total_rows(rows) if rows.size > 1 show_rows(rows) end @@ -24,6 +23,7 @@ def build_rows(options) counts[:byte] = input_text.size if options[:c] rows << counts end + rows << calculate_total_rows(rows) if rows.size > 1 rows end From 775cc20b5b380aa174f16683b36b7616853d928a Mon Sep 17 00:00:00 2001 From: kutimiti1234 Date: Sat, 20 Jul 2024 10:27:38 +0900 Subject: [PATCH 41/41] =?UTF-8?q?clean:=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=81=AE=E6=9D=A1=E4=BB=B6=E5=88=86=E5=B2=90=E3=82=92?= =?UTF-8?q?3=E3=81=93=E3=81=86=E6=BC=94=E7=AE=97=E5=AD=90=E3=81=A7?= =?UTF-8?q?=E8=A6=8B=E3=82=84=E3=81=99=E3=81=8F=E3=80=82=E3=81=BE=E3=81=9F?= =?UTF-8?q?=E3=80=81=E5=AE=9A=E6=95=B0=E3=81=AE=E5=90=8D=E5=89=8D=E3=82=92?= =?UTF-8?q?=E5=AE=9F=E6=85=8B=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 5bb4cefe10..c642c34e80 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,7 +3,7 @@ require 'optparse' -STDIN_FILENAME = /^-$/ +STDIN_PATTERN = /^-$/ TOTAL = '合計' def main @@ -40,14 +40,9 @@ def calculate_total_rows(rows) def show_rows(rows) max_widths = calculate_max_widths(rows) rows.each do |counts| - columns = {} - counts.each do |name, value| - columns[name] = if name == :filename - value.sub(STDIN_FILENAME,"") - else - width = max_widths[name] - value.to_s.rjust(width) - end + columns = counts.to_h do |name, value| + text = name == :filename ? value.sub(STDIN_PATTERN, '') : value.to_s.rjust(max_widths[name]) + [name, text] end puts columns.values_at(:line, :word, :byte, :filename).join(' ') end