From 65c31b591132e5d1ba920857f0c2fbc5dff2419b Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 10 Sep 2025 13:37:59 +0900 Subject: [PATCH 01/25] =?UTF-8?q?wc=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=82=92=E4=BD=9C=E6=88=90=EF=BC=88l=E3=80=81w=E3=80=81c?= =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 05.wc/wc.rb diff --git a/05.wc/wc.rb b/05.wc/wc.rb new file mode 100644 index 0000000000..3997d35778 --- /dev/null +++ b/05.wc/wc.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require 'optparse' + +def get_option_and_paths(arguments) + line = false + word = false + byte = false + opt = OptionParser.new + opt.on('-l') { |v| line = v } + opt.on('-w') { |v| word = v } + opt.on('-c') { |v| byte = v } + paths = opt.parse(arguments) + if !line && !word && !byte + { line: true, word: true, byte: true, paths: } + else + { line:, word:, byte:, paths: } + end +end + +def build_stdin_output + input = STDIN.read + outputs = { + line: input.lines.count, + word: input.split.count, + byte: input.size, + } +end + +def build_file_output(path) + begin + outputs = { + line: File.read(path).lines.count, + word: File.read(path).split.count, + byte: File.size(path), + name: path, + } + rescue Errno::ENOTDIR + outputs = { + error: "wc: #{path}: Not a directory", + } + rescue Errno::ENOENT + outputs = { + error: "wc: #{path}: No such file or directory", + } + end +end + +def build_directory_output(path) + outputs = { + message: "wc: #{path}: Is a directory\n", + line: 0, + word: 0, + byte: 0, + name: path + } +end + +def calculate_total(totals, outputs) + return totals if outputs.key?(:error) + totals[:line] += outputs[:line] + totals[:word] += outputs[:word] + totals[:byte] += outputs[:byte] + totals +end + +def display(outputs, flags) + outputs.each do |key, value| + if flags[key] + print "#{value} " + end + end + puts '' +end + +def display_totals(totals, flags) + totals[:name] = 'total' + display(totals, flags) +end + +get_option_and_paths(ARGV) => { line:, word:, byte:, paths: } +totals = Hash.new { |h, k| h[k] = 0 } +output_flags = { line:, word:, byte:, name: true, message: true, error: true} +if paths.empty? + stdins = build_stdin_output + display(stdins, output_flags) +else + paths.each do |path| + outputs = {} + if File.directory?(path) + outputs = build_directory_output(path) + else + outputs = build_file_output(path) + totals = calculate_total(totals, outputs) + end + display(outputs, output_flags) + end + display_totals(totals, output_flags) if paths.length > 1 +end From f16378fbf256b4fab9a391dbf6567b7d13338994 Mon Sep 17 00:00:00 2001 From: shoma Date: Thu, 11 Sep 2025 10:12:05 +0900 Subject: [PATCH 02/25] =?UTF-8?q?rubocop=E3=81=A7=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 | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 3997d35778..338a771422 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -19,35 +19,33 @@ def get_option_and_paths(arguments) end def build_stdin_output - input = STDIN.read - outputs = { + input = $stdin.read + { line: input.lines.count, word: input.split.count, - byte: input.size, + byte: input.size } end def build_file_output(path) - begin - outputs = { - line: File.read(path).lines.count, - word: File.read(path).split.count, - byte: File.size(path), - name: path, - } - rescue Errno::ENOTDIR - outputs = { - error: "wc: #{path}: Not a directory", - } - rescue Errno::ENOENT - outputs = { - error: "wc: #{path}: No such file or directory", - } - end + { + line: File.read(path).lines.count, + word: File.read(path).split.count, + byte: File.size(path), + name: path + } +rescue Errno::ENOTDIR + { + error: "wc: #{path}: Not a directory" + } +rescue Errno::ENOENT + { + error: "wc: #{path}: No such file or directory" + } end def build_directory_output(path) - outputs = { + { message: "wc: #{path}: Is a directory\n", line: 0, word: 0, @@ -58,6 +56,7 @@ def build_directory_output(path) def calculate_total(totals, outputs) return totals if outputs.key?(:error) + totals[:line] += outputs[:line] totals[:word] += outputs[:word] totals[:byte] += outputs[:byte] @@ -66,9 +65,7 @@ def calculate_total(totals, outputs) def display(outputs, flags) outputs.each do |key, value| - if flags[key] - print "#{value} " - end + print "#{value} " if flags[key] end puts '' end @@ -80,7 +77,7 @@ def display_totals(totals, flags) get_option_and_paths(ARGV) => { line:, word:, byte:, paths: } totals = Hash.new { |h, k| h[k] = 0 } -output_flags = { line:, word:, byte:, name: true, message: true, error: true} +output_flags = { line:, word:, byte:, name: true, message: true, error: true } if paths.empty? stdins = build_stdin_output display(stdins, output_flags) From 5fd8ba82bc7e656ed1d60ca06b5ec9cfe25da060 Mon Sep 17 00:00:00 2001 From: shoma Date: Fri, 12 Sep 2025 17:39:19 +0900 Subject: [PATCH 03/25] =?UTF-8?q?flags=E3=81=AB=E3=82=88=E3=82=8B=E5=88=B6?= =?UTF-8?q?=E5=BE=A1=E3=82=92=E6=9C=80=E5=B0=8F=E9=99=90=E3=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=81=9F=E3=82=81=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 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 338a771422..c9cf7296fd 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -64,8 +64,12 @@ def calculate_total(totals, outputs) end def display(outputs, flags) - outputs.each do |key, value| - print "#{value} " if flags[key] + # ループ処理にするとflagsとoutputsの要素の順番が同期する必要があるため、今後予期せぬバグに繋がると判断しこのように記述している。 + outputs.delete(:line) unless flags[:line] + outputs.delete(:word) unless flags[:word] + outputs.delete(:byte) unless flags[:byte] + outputs.each_value do |value| + print "#{value} " end puts '' end @@ -77,7 +81,7 @@ def display_totals(totals, flags) get_option_and_paths(ARGV) => { line:, word:, byte:, paths: } totals = Hash.new { |h, k| h[k] = 0 } -output_flags = { line:, word:, byte:, name: true, message: true, error: true } +output_flags = { line:, word:, byte: } if paths.empty? stdins = build_stdin_output display(stdins, output_flags) From 581b8fca9b24028c32a685ede300f91ab8a58a57 Mon Sep 17 00:00:00 2001 From: shoma Date: Tue, 16 Sep 2025 09:32:35 +0900 Subject: [PATCH 04/25] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=83=AA?= =?UTF-8?q?=E3=83=BC=E3=83=87=E3=82=A3=E3=83=B3=E3=82=B0=E3=82=92=E3=81=97?= =?UTF-8?q?=E3=82=84=E3=81=99=E3=81=8F=E3=81=99=E3=82=8B=E3=81=9F=E3=82=81?= =?UTF-8?q?=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 | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index c9cf7296fd..a59e2cdd08 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -2,6 +2,28 @@ require 'optparse' +def main + get_option_and_paths(ARGV) => { line:, word:, byte:, paths: } + totals = Hash.new { |h, k| h[k] = 0 } + output_flags = { line:, word:, byte: } + if paths.empty? + stdins = build_stdin_output + display(stdins, output_flags) + else + paths.each do |path| + outputs = {} + if File.directory?(path) + outputs = build_directory_output(path) + else + outputs = build_file_output(path) + totals = calculate_total(totals, outputs) + end + display(outputs, output_flags) + end + display_totals(totals, output_flags) if paths.length > 1 + end +end + def get_option_and_paths(arguments) line = false word = false @@ -79,22 +101,4 @@ def display_totals(totals, flags) display(totals, flags) end -get_option_and_paths(ARGV) => { line:, word:, byte:, paths: } -totals = Hash.new { |h, k| h[k] = 0 } -output_flags = { line:, word:, byte: } -if paths.empty? - stdins = build_stdin_output - display(stdins, output_flags) -else - paths.each do |path| - outputs = {} - if File.directory?(path) - outputs = build_directory_output(path) - else - outputs = build_file_output(path) - totals = calculate_total(totals, outputs) - end - display(outputs, output_flags) - end - display_totals(totals, output_flags) if paths.length > 1 -end +main From 1ee4ccd6e14edcd934222bffcca80fb274760ba8 Mon Sep 17 00:00:00 2001 From: shoma Date: Tue, 16 Sep 2025 09:40:19 +0900 Subject: [PATCH 05/25] =?UTF-8?q?=E5=AE=9F=E8=A1=8C=E5=8A=B9=E7=8E=87?= =?UTF-8?q?=E3=82=92=E4=B8=8A=E3=81=92=E3=82=8B=E3=81=9F=E3=82=81=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 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index a59e2cdd08..da3a8ccf43 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -50,9 +50,10 @@ def build_stdin_output end def build_file_output(path) + content = File.read(path) { - line: File.read(path).lines.count, - word: File.read(path).split.count, + line: content.lines.count, + word: content.split.count, byte: File.size(path), name: path } From 77ea30ee5576b0bf8666e24fbff6144ee9672661 Mon Sep 17 00:00:00 2001 From: shoma Date: Tue, 16 Sep 2025 11:12:02 +0900 Subject: [PATCH 06/25] =?UTF-8?q?=E4=BB=8A=E5=BE=8C=E4=BA=88=E6=9C=9F?= =?UTF-8?q?=E3=81=9B=E3=81=AC=E5=89=AF=E4=BD=9C=E7=94=A8=E3=81=8C=E8=B5=B7?= =?UTF-8?q?=E3=81=93=E3=82=89=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=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 | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index da3a8ccf43..d430cc75c3 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -4,7 +4,7 @@ def main get_option_and_paths(ARGV) => { line:, word:, byte:, paths: } - totals = Hash.new { |h, k| h[k] = 0 } + totals = { line: 0, word: 0, byte: 0 } output_flags = { line:, word:, byte: } if paths.empty? stdins = build_stdin_output @@ -80,18 +80,18 @@ def build_directory_output(path) def calculate_total(totals, outputs) return totals if outputs.key?(:error) - totals[:line] += outputs[:line] - totals[:word] += outputs[:word] - totals[:byte] += outputs[:byte] - totals + totals.each_with_object({}) do |(key, value), hash| + hash[key] = value + outputs[key] + end end def display(outputs, flags) - # ループ処理にするとflagsとoutputsの要素の順番が同期する必要があるため、今後予期せぬバグに繋がると判断しこのように記述している。 - outputs.delete(:line) unless flags[:line] - outputs.delete(:word) unless flags[:word] - outputs.delete(:byte) unless flags[:byte] - outputs.each_value do |value| + filtered_outputs = outputs.reject do |key| + (key == :line && !flags[:line]) || + (key == :word && !flags[:word]) || + (key == :byte && !flags[:byte]) + end + filtered_outputs.each_value do |value| print "#{value} " end puts '' From 44beccb90bf0780dfc1afd3642f5dffa36063d02 Mon Sep 17 00:00:00 2001 From: shoma Date: Tue, 16 Sep 2025 11:55:49 +0900 Subject: [PATCH 07/25] =?UTF-8?q?=E5=87=BA=E5=8A=9B=E3=81=AE=E3=82=B9?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=AB=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 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index d430cc75c3..0cf8f5e692 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -85,14 +85,21 @@ def calculate_total(totals, outputs) end end +def adjust_style(output, key) + width = output.to_s.length + 1 + return "#{output}".rjust(width) if key == :line || :word || :byte + + "#{output}".ljust(width) +end + def display(outputs, flags) filtered_outputs = outputs.reject do |key| (key == :line && !flags[:line]) || (key == :word && !flags[:word]) || (key == :byte && !flags[:byte]) end - filtered_outputs.each_value do |value| - print "#{value} " + filtered_outputs.each do |key, value| + print adjust_style(value, key) end puts '' end From c0ac4635e1511d37294a3536bc7716c3e2b17c26 Mon Sep 17 00:00:00 2001 From: shoma Date: Tue, 16 Sep 2025 12:05:18 +0900 Subject: [PATCH 08/25] =?UTF-8?q?=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B?= =?UTF-8?q?=E5=80=A4=E3=82=92=E9=96=93=E9=81=95=E3=81=88=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=81=9F=E3=81=AE=E3=81=A7=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 0cf8f5e692..89733dc0f6 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -54,7 +54,7 @@ def build_file_output(path) { line: content.lines.count, word: content.split.count, - byte: File.size(path), + byte: content.size, name: path } rescue Errno::ENOTDIR From 0eed6c89aabcf043650f1de75c1521dc448e9df6 Mon Sep 17 00:00:00 2001 From: shoma Date: Tue, 16 Sep 2025 13:03:48 +0900 Subject: [PATCH 09/25] =?UTF-8?q?rubocop=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E3=82=92=E5=8F=8D=E6=98=A0?= 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 89733dc0f6..14176b725a 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -86,10 +86,11 @@ def calculate_total(totals, outputs) end def adjust_style(output, key) - width = output.to_s.length + 1 - return "#{output}".rjust(width) if key == :line || :word || :byte + str_output = output.to_s + width = str_output.length + 1 + return str_output.rjust(width) if key == :line || :word || :byte - "#{output}".ljust(width) + str_output.ljust(width) end def display(outputs, flags) From 90934e9be4c44b2dbd4b791f5ae223f21e77da3b Mon Sep 17 00:00:00 2001 From: shoma Date: Mon, 22 Sep 2025 10:34:27 +0900 Subject: [PATCH 10/25] =?UTF-8?q?=E5=87=BA=E5=8A=9B=E5=B9=85=E3=81=8C?= =?UTF-8?q?=E5=8B=95=E7=9A=84=E3=81=AB=E5=A4=89=E5=8C=96=E3=81=99=E3=82=8B?= =?UTF-8?q?=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 | 139 +++++++++++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 73 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 14176b725a..c1b0cae653 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,28 +3,11 @@ require 'optparse' def main - get_option_and_paths(ARGV) => { line:, word:, byte:, paths: } - totals = { line: 0, word: 0, byte: 0 } - output_flags = { line:, word:, byte: } - if paths.empty? - stdins = build_stdin_output - display(stdins, output_flags) - else - paths.each do |path| - outputs = {} - if File.directory?(path) - outputs = build_directory_output(path) - else - outputs = build_file_output(path) - totals = calculate_total(totals, outputs) - end - display(outputs, output_flags) - end - display_totals(totals, output_flags) if paths.length > 1 - end + option_and_paths => { paths:, **option_flags } + display(paths, option_flags) end -def get_option_and_paths(arguments) +def option_and_paths line = false word = false byte = false @@ -32,7 +15,7 @@ def get_option_and_paths(arguments) opt.on('-l') { |v| line = v } opt.on('-w') { |v| word = v } opt.on('-c') { |v| byte = v } - paths = opt.parse(arguments) + paths = opt.parse(ARGV) if !line && !word && !byte { line: true, word: true, byte: true, paths: } else @@ -40,74 +23,84 @@ def get_option_and_paths(arguments) end end -def build_stdin_output - input = $stdin.read - { - line: input.lines.count, - word: input.split.count, - byte: input.size - } +def display(paths, option_flags) + text_metadata_collection = + if paths.empty? + [ + set_text_metadata( + option_flags, + input: $stdin.read + ) + ] + else + build_text_metadata( + paths, + option_flags + ) + end + width = calculate_output_width(text_metadata_collection) + text_metadata_collection.each do |text_metadata| + render(text_metadata, width) + end end -def build_file_output(path) - content = File.read(path) - { - line: content.lines.count, - word: content.split.count, - byte: content.size, - name: path - } -rescue Errno::ENOTDIR - { - error: "wc: #{path}: Not a directory" - } -rescue Errno::ENOENT - { - error: "wc: #{path}: No such file or directory" - } +def build_text_metadata(paths, option_flags) + text_metadata_collection = paths.map do |path| + if File.directory?(path) + set_text_metadata(option_flags, message: "wc: #{path}: Is a directory\n", name: path) + elsif !File.exist?(path) + %r{/$}.match?(path) ? { error: "wc: #{path}: Not a directory" } : { error: "wc: #{path}: No such file or directory" } + else + set_text_metadata(option_flags, input: File.read(path), name: path) + end + end + if text_metadata_collection.length > 1 + text_metadata_collection + [calculate_total(text_metadata_collection)] + else + text_metadata_collection + end end -def build_directory_output(path) +def set_text_metadata(option_flags, message: nil, input: '', name: nil) { - message: "wc: #{path}: Is a directory\n", - line: 0, - word: 0, - byte: 0, - name: path - } + message: message, + line_count: option_flags[:line] ? input.lines.count : nil, + word_count: option_flags[:word] ? input.split.count : nil, + size: option_flags[:byte] ? input.size : nil, + name: name + }.compact end -def calculate_total(totals, outputs) - return totals if outputs.key?(:error) - - totals.each_with_object({}) do |(key, value), hash| - hash[key] = value + outputs[key] +def calculate_total(text_metadata_collection) + totals = text_metadata_collection.each_with_object({}) do |text_metadata, temporary| + text_metadata.each do |key, data| + # text_metadataにはtotalを求めたい値以外も存在するためフィルタをかけている + temporary[key] = (temporary[key] || 0) + data if %i[line_count word_count size].include?(key) + end end + totals.merge(name: 'total') end -def adjust_style(output, key) - str_output = output.to_s - width = str_output.length + 1 - return str_output.rjust(width) if key == :line || :word || :byte - - str_output.ljust(width) +def calculate_output_width(text_metadata_collection) + text_metadata_collection.each_with_object([]) do |text_metadata, widths| + %i[line_count word_count size].each do |key| + widths << text_metadata[key].to_s.length + end + end.compact.max end -def display(outputs, flags) - filtered_outputs = outputs.reject do |key| - (key == :line && !flags[:line]) || - (key == :word && !flags[:word]) || - (key == :byte && !flags[:byte]) - end - filtered_outputs.each do |key, value| - print adjust_style(value, key) +def render(text_metadata, width) + text_metadata.each do |key, data| + print adjust_style(data, width, key) end puts '' end -def display_totals(totals, flags) - totals[:name] = 'total' - display(totals, flags) +def adjust_style(data, width, key) + str_data = data.to_s + return "#{str_data.rjust(width)} " if %i[line_count word_count size].include?(key) + + str_data.ljust(width) end main From ea6443d0f818c2818c54b09190a1d36b8151d71d Mon Sep 17 00:00:00 2001 From: shoma Date: Mon, 22 Sep 2025 10:43:11 +0900 Subject: [PATCH 11/25] =?UTF-8?q?=E4=B8=8A=E3=81=8B=E3=82=89=E4=B8=8B?= =?UTF-8?q?=E3=81=AB=E8=87=AA=E7=84=B6=E3=81=AB=E8=AA=AD=E3=82=81=E3=82=8B?= =?UTF-8?q?=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 | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index c1b0cae653..b6ceb66021 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -44,6 +44,16 @@ def display(paths, option_flags) end end +def set_text_metadata(option_flags, message: nil, input: '', name: nil) + { + message: message, + line_count: option_flags[:line] ? input.lines.count : nil, + word_count: option_flags[:word] ? input.split.count : nil, + size: option_flags[:byte] ? input.size : nil, + name: name + }.compact +end + def build_text_metadata(paths, option_flags) text_metadata_collection = paths.map do |path| if File.directory?(path) @@ -61,16 +71,6 @@ def build_text_metadata(paths, option_flags) end end -def set_text_metadata(option_flags, message: nil, input: '', name: nil) - { - message: message, - line_count: option_flags[:line] ? input.lines.count : nil, - word_count: option_flags[:word] ? input.split.count : nil, - size: option_flags[:byte] ? input.size : nil, - name: name - }.compact -end - def calculate_total(text_metadata_collection) totals = text_metadata_collection.each_with_object({}) do |text_metadata, temporary| text_metadata.each do |key, data| From 3d09423d45abb9cf96edb2be7f3c8d32364a0043 Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 10:02:32 +0900 Subject: [PATCH 12/25] =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=81=8B=E3=82=89=E3=81=AE=E8=BF=94=E3=82=8A=E5=80=A4=E3=81=AE?= =?UTF-8?q?=E5=8F=97=E3=81=91=E5=8F=96=E3=82=8A=E6=96=B9=E3=82=92=E5=A4=89?= =?UTF-8?q?=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 b6ceb66021..73b4949e06 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,7 +3,7 @@ require 'optparse' def main - option_and_paths => { paths:, **option_flags } + paths, option_flags = option_and_paths display(paths, option_flags) end @@ -17,9 +17,9 @@ def option_and_paths opt.on('-c') { |v| byte = v } paths = opt.parse(ARGV) if !line && !word && !byte - { line: true, word: true, byte: true, paths: } + [ paths, { line: true, word: true, byte: true} ] else - { line:, word:, byte:, paths: } + [ paths, { line:, word:, byte:, paths: } ] end end From 0506db284d0e43a801cbdd9eb9b9dc0e576d7d0f Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 10:04:28 +0900 Subject: [PATCH 13/25] =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E8=A8=AD=E8=A8=88=E3=82=92=E8=A6=8B=E7=9B=B4=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 73b4949e06..ec780c1e62 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -4,26 +4,6 @@ def main paths, option_flags = option_and_paths - display(paths, option_flags) -end - -def option_and_paths - line = false - word = false - byte = false - opt = OptionParser.new - opt.on('-l') { |v| line = v } - opt.on('-w') { |v| word = v } - opt.on('-c') { |v| byte = v } - paths = opt.parse(ARGV) - if !line && !word && !byte - [ paths, { line: true, word: true, byte: true} ] - else - [ paths, { line:, word:, byte:, paths: } ] - end -end - -def display(paths, option_flags) text_metadata_collection = if paths.empty? [ @@ -44,6 +24,22 @@ def display(paths, option_flags) end end +def option_and_paths + line = false + word = false + byte = false + opt = OptionParser.new + opt.on('-l') { |v| line = v } + opt.on('-w') { |v| word = v } + opt.on('-c') { |v| byte = v } + paths = opt.parse(ARGV) + if !line && !word && !byte + [ paths, { line: true, word: true, byte: true} ] + else + [ paths, { line:, word:, byte:, paths: } ] + end +end + def set_text_metadata(option_flags, message: nil, input: '', name: nil) { message: message, From 07680e4458cda930eba53bb7cd85471de871c819 Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 10:06:03 +0900 Subject: [PATCH 14/25] =?UTF-8?q?=E4=BD=99=E8=A8=88=E3=81=AA=E6=94=B9?= =?UTF-8?q?=E8=A1=8C=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 | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index ec780c1e62..566b27d9aa 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -6,17 +6,9 @@ def main paths, option_flags = option_and_paths text_metadata_collection = if paths.empty? - [ - set_text_metadata( - option_flags, - input: $stdin.read - ) - ] + [set_text_metadata(option_flags, input: $stdin.read)] else - build_text_metadata( - paths, - option_flags - ) + build_text_metadata(paths, option_flags) end width = calculate_output_width(text_metadata_collection) text_metadata_collection.each do |text_metadata| From f38807fe11a49006f8150af3a7f713f33232404d Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 10:19:33 +0900 Subject: [PATCH 15/25] =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E5=90=8D=E3=82=92=E8=A6=8B=E7=9B=B4=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 566b27d9aa..bd3fb1a6f9 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -6,7 +6,7 @@ def main paths, option_flags = option_and_paths text_metadata_collection = if paths.empty? - [set_text_metadata(option_flags, input: $stdin.read)] + [create_text_metadata(option_flags, input: $stdin.read)] else build_text_metadata(paths, option_flags) end @@ -32,7 +32,7 @@ def option_and_paths end end -def set_text_metadata(option_flags, message: nil, input: '', name: nil) +def create_text_metadata(option_flags, message: nil, input: '', name: nil) { message: message, line_count: option_flags[:line] ? input.lines.count : nil, @@ -45,11 +45,11 @@ def set_text_metadata(option_flags, message: nil, input: '', name: nil) def build_text_metadata(paths, option_flags) text_metadata_collection = paths.map do |path| if File.directory?(path) - set_text_metadata(option_flags, message: "wc: #{path}: Is a directory\n", name: path) + create_text_metadata(option_flags, message: "wc: #{path}: Is a directory\n", name: path) elsif !File.exist?(path) %r{/$}.match?(path) ? { error: "wc: #{path}: Not a directory" } : { error: "wc: #{path}: No such file or directory" } else - set_text_metadata(option_flags, input: File.read(path), name: path) + create_text_metadata(option_flags, input: File.read(path), name: path) end end if text_metadata_collection.length > 1 From c0dd41778150f60a671d3cdffdc1653a6deb8a67 Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 10:33:48 +0900 Subject: [PATCH 16/25] =?UTF-8?q?=E5=BF=85=E9=A0=88=E8=A6=81=E4=BB=B6?= =?UTF-8?q?=E5=A4=96=E3=81=AE=E5=AE=9F=E8=A3=85=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 | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index bd3fb1a6f9..40573c062b 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -32,9 +32,8 @@ def option_and_paths end end -def create_text_metadata(option_flags, message: nil, input: '', name: nil) +def create_text_metadata(option_flags, input: , name: nil) { - message: message, line_count: option_flags[:line] ? input.lines.count : nil, word_count: option_flags[:word] ? input.split.count : nil, size: option_flags[:byte] ? input.size : nil, @@ -44,13 +43,7 @@ def create_text_metadata(option_flags, message: nil, input: '', name: nil) def build_text_metadata(paths, option_flags) text_metadata_collection = paths.map do |path| - if File.directory?(path) - create_text_metadata(option_flags, message: "wc: #{path}: Is a directory\n", name: path) - elsif !File.exist?(path) - %r{/$}.match?(path) ? { error: "wc: #{path}: Not a directory" } : { error: "wc: #{path}: No such file or directory" } - else - create_text_metadata(option_flags, input: File.read(path), name: path) - end + create_text_metadata(option_flags, input: File.read(path), name: path) end if text_metadata_collection.length > 1 text_metadata_collection + [calculate_total(text_metadata_collection)] From 18cd3cffd95346306c8d214c9669abcf4b43787a Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 11:13:44 +0900 Subject: [PATCH 17/25] =?UTF-8?q?=E5=90=88=E8=A8=88=E7=AE=97=E5=87=BA?= =?UTF-8?q?=E3=81=AE=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=AE=E3=83=AD?= =?UTF-8?q?=E3=82=B8=E3=83=83=E3=82=AF=E3=82=92=E3=82=B7=E3=83=B3=E3=83=97?= =?UTF-8?q?=E3=83=AB=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 | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 40573c062b..947e2a9cf1 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -19,16 +19,16 @@ def main def option_and_paths line = false word = false - byte = false + size = false opt = OptionParser.new opt.on('-l') { |v| line = v } opt.on('-w') { |v| word = v } - opt.on('-c') { |v| byte = v } + opt.on('-c') { |v| size = v } paths = opt.parse(ARGV) - if !line && !word && !byte - [ paths, { line: true, word: true, byte: true} ] + if !line && !word && !size + [ paths, { line: true, word: true, size: true} ] else - [ paths, { line:, word:, byte:, paths: } ] + [ paths, { line:, word:, size:, paths: } ] end end @@ -36,7 +36,7 @@ def create_text_metadata(option_flags, input: , name: nil) { line_count: option_flags[:line] ? input.lines.count : nil, word_count: option_flags[:word] ? input.split.count : nil, - size: option_flags[:byte] ? input.size : nil, + size: option_flags[:size] ? input.size : nil, name: name }.compact end @@ -46,20 +46,22 @@ def build_text_metadata(paths, option_flags) create_text_metadata(option_flags, input: File.read(path), name: path) end if text_metadata_collection.length > 1 - text_metadata_collection + [calculate_total(text_metadata_collection)] + text_metadata_collection + [calculate_total(text_metadata_collection, option_flags)] else text_metadata_collection end end -def calculate_total(text_metadata_collection) - totals = text_metadata_collection.each_with_object({}) do |text_metadata, temporary| - text_metadata.each do |key, data| - # text_metadataにはtotalを求めたい値以外も存在するためフィルタをかけている - temporary[key] = (temporary[key] || 0) + data if %i[line_count word_count size].include?(key) - end +def calculate_total(text_metadata_collection, option_flags) + line_count_sum = option_flags[:line] ? 0 : nil + word_count_sum = option_flags[:word] ? 0 :nil + size_sum = option_flags[:size] ? 0 :nil + text_metadata_collection.each do |text_metadata| + line_count_sum += text_metadata[:line_count] if option_flags[:line] + word_count_sum += text_metadata[:word_count] if option_flags[:word] + size_sum += text_metadata[:size] if option_flags[:size] end - totals.merge(name: 'total') + { line_count: line_count_sum, word_count: word_count_sum, size: size_sum, name: 'total' }.compact end def calculate_output_width(text_metadata_collection) From e1fceb9cf6399fff334e064f16622069ac72fa4e Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 11:29:20 +0900 Subject: [PATCH 18/25] =?UTF-8?q?=E3=83=AC=E3=82=A4=E3=82=A2=E3=82=A6?= =?UTF-8?q?=E3=83=88=E8=AA=BF=E6=95=B4=E3=81=AE=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E3=82=B7=E3=83=B3=E3=83=97=E3=83=AB=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 947e2a9cf1..bb8331c3bc 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'optparse' +require 'debug' def main paths, option_flags = option_and_paths @@ -65,11 +66,12 @@ def calculate_total(text_metadata_collection, option_flags) end def calculate_output_width(text_metadata_collection) - text_metadata_collection.each_with_object([]) do |text_metadata, widths| - %i[line_count word_count size].each do |key| - widths << text_metadata[key].to_s.length + widths = text_metadata_collection.map do |text_metadata| + %i[line_count word_count size].map do |key| + text_metadata[key].to_s.length end - end.compact.max + end + widths.flatten.max end def render(text_metadata, width) @@ -81,9 +83,7 @@ def render(text_metadata, width) def adjust_style(data, width, key) str_data = data.to_s - return "#{str_data.rjust(width)} " if %i[line_count word_count size].include?(key) - - str_data.ljust(width) + %i[line_count word_count size].include?(key) ? "#{str_data.rjust(width)} " : str_data end main From bc05889267b664af48c81523179a6d7c2e9432ae Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 14:04:45 +0900 Subject: [PATCH 19/25] =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=81=AE=E5=87=BA=E5=8A=9B=E5=88=B6=E5=BE=A1=E3=81=AE?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E3=82=92=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 | 71 ++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index bb8331c3bc..4160b6058d 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -1,23 +1,22 @@ # frozen_string_literal: true require 'optparse' -require 'debug' def main - paths, option_flags = option_and_paths + paths, target_options = options_and_paths text_metadata_collection = if paths.empty? - [create_text_metadata(option_flags, input: $stdin.read)] + [create_text_metadata(input: $stdin.read)] else - build_text_metadata(paths, option_flags) + build_text_metadata(paths) end - width = calculate_output_width(text_metadata_collection) + width = calculate_output_width(text_metadata_collection, target_options) text_metadata_collection.each do |text_metadata| - render(text_metadata, width) + render(target_options, text_metadata, width) end end -def option_and_paths +def options_and_paths line = false word = false size = false @@ -26,64 +25,68 @@ def option_and_paths opt.on('-w') { |v| word = v } opt.on('-c') { |v| size = v } paths = opt.parse(ARGV) - if !line && !word && !size - [ paths, { line: true, word: true, size: true} ] + target_options = if !line && !word && !size + [:line_count, :word_count, :size] else - [ paths, { line:, word:, size:, paths: } ] + {line_count: line, word_count: word, size: size}.map do |option, flag| + next unless flag + option + end end + [paths, target_options] end -def create_text_metadata(option_flags, input: , name: nil) +def create_text_metadata(input: , name: nil) { - line_count: option_flags[:line] ? input.lines.count : nil, - word_count: option_flags[:word] ? input.split.count : nil, - size: option_flags[:size] ? input.size : nil, + line_count: input.lines.count, + word_count: input.split.count, + size: input.size, name: name - }.compact + } end -def build_text_metadata(paths, option_flags) +def build_text_metadata(paths) text_metadata_collection = paths.map do |path| - create_text_metadata(option_flags, input: File.read(path), name: path) + create_text_metadata(input: File.read(path), name: path) end if text_metadata_collection.length > 1 - text_metadata_collection + [calculate_total(text_metadata_collection, option_flags)] + text_metadata_collection + [calculate_total(text_metadata_collection)] else text_metadata_collection end end -def calculate_total(text_metadata_collection, option_flags) - line_count_sum = option_flags[:line] ? 0 : nil - word_count_sum = option_flags[:word] ? 0 :nil - size_sum = option_flags[:size] ? 0 :nil +def calculate_total(text_metadata_collection) + line_count_sum = 0 + word_count_sum = 0 + size_sum = 0 text_metadata_collection.each do |text_metadata| - line_count_sum += text_metadata[:line_count] if option_flags[:line] - word_count_sum += text_metadata[:word_count] if option_flags[:word] - size_sum += text_metadata[:size] if option_flags[:size] + line_count_sum += text_metadata[:line_count] + word_count_sum += text_metadata[:word_count] + size_sum += text_metadata[:size] end - { line_count: line_count_sum, word_count: word_count_sum, size: size_sum, name: 'total' }.compact + { line_count: line_count_sum, word_count: word_count_sum, size: size_sum, name: 'total' } end -def calculate_output_width(text_metadata_collection) +def calculate_output_width(text_metadata_collection, target_options) widths = text_metadata_collection.map do |text_metadata| - %i[line_count word_count size].map do |key| + target_options.map do |key| text_metadata[key].to_s.length end end widths.flatten.max end -def render(text_metadata, width) - text_metadata.each do |key, data| - print adjust_style(data, width, key) +def render(target_options, text_metadata, width) + target_options.each do |key| + print adjust_style(text_metadata[key], width) end + print text_metadata[:name] puts '' end -def adjust_style(data, width, key) - str_data = data.to_s - %i[line_count word_count size].include?(key) ? "#{str_data.rjust(width)} " : str_data +def adjust_style(metadata, width) + "#{metadata.to_s.rjust(width)} " end main From 740c96fd8a491c0c1e51d730847da3b9fb8cabac Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 15:05:20 +0900 Subject: [PATCH 20/25] =?UTF-8?q?rubocop=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E6=8F=90=E6=A1=88=E3=82=92=E5=8F=8D=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 4160b6058d..9d964cdbaa 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -25,18 +25,18 @@ def options_and_paths opt.on('-w') { |v| word = v } opt.on('-c') { |v| size = v } paths = opt.parse(ARGV) - target_options = if !line && !word && !size - [:line_count, :word_count, :size] - else - {line_count: line, word_count: word, size: size}.map do |option, flag| - next unless flag - option + target_options = + if [line, word, size].none? + %i[line_count word_count size] + else + { line_count: line, word_count: word, size: size }.map do |option, flag| + option if flag + end.compact end - end [paths, target_options] end -def create_text_metadata(input: , name: nil) +def create_text_metadata(input:, name: nil) { line_count: input.lines.count, word_count: input.split.count, @@ -74,19 +74,15 @@ def calculate_output_width(text_metadata_collection, target_options) text_metadata[key].to_s.length end end - widths.flatten.max + widths.flatten.max end def render(target_options, text_metadata, width) target_options.each do |key| - print adjust_style(text_metadata[key], width) + print "#{text_metadata[key].to_s.rjust(width)} " end print text_metadata[:name] puts '' end -def adjust_style(metadata, width) - "#{metadata.to_s.rjust(width)} " -end - main From 1ac37736d04814767020d2926475796f2d8c25af Mon Sep 17 00:00:00 2001 From: shoma Date: Wed, 24 Sep 2025 15:08:52 +0900 Subject: [PATCH 21/25] =?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 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 9d964cdbaa..faa70ef1c7 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -70,16 +70,16 @@ def calculate_total(text_metadata_collection) def calculate_output_width(text_metadata_collection, target_options) widths = text_metadata_collection.map do |text_metadata| - target_options.map do |key| - text_metadata[key].to_s.length + target_options.map do |option| + text_metadata[option].to_s.length end end widths.flatten.max end def render(target_options, text_metadata, width) - target_options.each do |key| - print "#{text_metadata[key].to_s.rjust(width)} " + target_options.each do |option| + print "#{text_metadata[option].to_s.rjust(width)} " end print text_metadata[:name] puts '' From d31e9b17ab8640e180ff8b68e5f9bab1cc4925be Mon Sep 17 00:00:00 2001 From: shoma Date: Thu, 25 Sep 2025 10:20:32 +0900 Subject: [PATCH 22/25] =?UTF-8?q?=E8=BF=94=E3=82=8A=E5=80=A4=E3=82=84?= =?UTF-8?q?=E5=BC=95=E6=95=B0=E3=81=AE=E9=A0=86=E7=95=AA=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index faa70ef1c7..6c8cad366c 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,16 +3,16 @@ require 'optparse' def main - paths, target_options = options_and_paths + options, paths = options_and_paths text_metadata_collection = if paths.empty? [create_text_metadata(input: $stdin.read)] else build_text_metadata(paths) end - width = calculate_output_width(text_metadata_collection, target_options) + width = calculate_output_width(text_metadata_collection, options) text_metadata_collection.each do |text_metadata| - render(target_options, text_metadata, width) + render(text_metadata, options, width) end end @@ -25,7 +25,7 @@ def options_and_paths opt.on('-w') { |v| word = v } opt.on('-c') { |v| size = v } paths = opt.parse(ARGV) - target_options = + options = if [line, word, size].none? %i[line_count word_count size] else @@ -33,7 +33,7 @@ def options_and_paths option if flag end.compact end - [paths, target_options] + [options, paths] end def create_text_metadata(input:, name: nil) @@ -68,17 +68,17 @@ def calculate_total(text_metadata_collection) { line_count: line_count_sum, word_count: word_count_sum, size: size_sum, name: 'total' } end -def calculate_output_width(text_metadata_collection, target_options) +def calculate_output_width(text_metadata_collection, options) widths = text_metadata_collection.map do |text_metadata| - target_options.map do |option| + options.map do |option| text_metadata[option].to_s.length end end widths.flatten.max end -def render(target_options, text_metadata, width) - target_options.each do |option| +def render(text_metadata, options, width) + options.each do |option| print "#{text_metadata[option].to_s.rjust(width)} " end print text_metadata[:name] From e11ccf92e583f08e8640b2db54d467598526f032 Mon Sep 17 00:00:00 2001 From: shoma Date: Thu, 25 Sep 2025 10:46:12 +0900 Subject: [PATCH 23/25] =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 60 ++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 6c8cad366c..1c30e62f73 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,20 +3,20 @@ require 'optparse' def main - options, paths = options_and_paths - text_metadata_collection = + column_names, paths = column_names_and_paths + statistics_matrix = if paths.empty? - [create_text_metadata(input: $stdin.read)] + [build_statistics_row(input: $stdin.read)] else - build_text_metadata(paths) + build_statistics_matrix(paths) end - width = calculate_output_width(text_metadata_collection, options) - text_metadata_collection.each do |text_metadata| - render(text_metadata, options, width) + width = calculate_output_width(statistics_matrix, column_names) + statistics_matrix.each do |statistics| + render(statistics, column_names, width) end end -def options_and_paths +def column_names_and_paths line = false word = false size = false @@ -25,7 +25,7 @@ def options_and_paths opt.on('-w') { |v| word = v } opt.on('-c') { |v| size = v } paths = opt.parse(ARGV) - options = + column_names = if [line, word, size].none? %i[line_count word_count size] else @@ -33,10 +33,10 @@ def options_and_paths option if flag end.compact end - [options, paths] + [column_names, paths] end -def create_text_metadata(input:, name: nil) +def build_statistics_row(input:, name: nil) { line_count: input.lines.count, word_count: input.split.count, @@ -45,43 +45,43 @@ def create_text_metadata(input:, name: nil) } end -def build_text_metadata(paths) - text_metadata_collection = paths.map do |path| - create_text_metadata(input: File.read(path), name: path) +def build_statistics_matrix(paths) + statistics_matrix = paths.map do |path| + build_statistics_row( File.read(path), path) end - if text_metadata_collection.length > 1 - text_metadata_collection + [calculate_total(text_metadata_collection)] + if statistics_matrix.length > 1 + statistics_matrix + [calculate_total(statistics_matrix)] else - text_metadata_collection + statistics_matrix end end -def calculate_total(text_metadata_collection) +def calculate_total(statistics_matrix) line_count_sum = 0 word_count_sum = 0 size_sum = 0 - text_metadata_collection.each do |text_metadata| - line_count_sum += text_metadata[:line_count] - word_count_sum += text_metadata[:word_count] - size_sum += text_metadata[:size] + statistics_matrix.each do |statistics| + line_count_sum += statistics[:line_count] + word_count_sum += statistics[:word_count] + size_sum += statistics[:size] end { line_count: line_count_sum, word_count: word_count_sum, size: size_sum, name: 'total' } end -def calculate_output_width(text_metadata_collection, options) - widths = text_metadata_collection.map do |text_metadata| - options.map do |option| - text_metadata[option].to_s.length +def calculate_output_width(statistics_matrix, column_names) + widths = statistics_matrix.map do |statistics| + column_names.map do |column_name| + statistics[column_name].to_s.length end end widths.flatten.max end -def render(text_metadata, options, width) - options.each do |option| - print "#{text_metadata[option].to_s.rjust(width)} " +def render(statistics, column_names, width) + column_names.each do |column_name| + print "#{statistics[column_name].to_s.rjust(width)} " end - print text_metadata[:name] + print statistics[:name] puts '' end From fa413caa8919af177ff1cbed8b075c2e561c8b21 Mon Sep 17 00:00:00 2001 From: shoma Date: Thu, 25 Sep 2025 10:51:41 +0900 Subject: [PATCH 24/25] =?UTF-8?q?=E3=82=AD=E3=83=BC=E3=83=AF=E3=83=BC?= =?UTF-8?q?=E3=83=89=E5=BC=95=E6=95=B0=E3=81=A7=E3=81=82=E3=82=8B=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E3=81=8C=E3=81=AA=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E3=81=AE=E3=81=A7=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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 1c30e62f73..05c4380110 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -6,7 +6,7 @@ def main column_names, paths = column_names_and_paths statistics_matrix = if paths.empty? - [build_statistics_row(input: $stdin.read)] + [build_statistics_row($stdin.read)] else build_statistics_matrix(paths) end @@ -36,7 +36,7 @@ def column_names_and_paths [column_names, paths] end -def build_statistics_row(input:, name: nil) +def build_statistics_row(input, name = nil) { line_count: input.lines.count, word_count: input.split.count, From 200be156b8c7b2a1c6a1bad10f69be4a971e3ad3 Mon Sep 17 00:00:00 2001 From: shoma Date: Thu, 25 Sep 2025 11:48:11 +0900 Subject: [PATCH 25/25] =?UTF-8?q?=E5=86=97=E9=95=B7=E3=81=A7=E3=81=82?= =?UTF-8?q?=E3=81=A3=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 | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 05c4380110..77326a4a5c 100644 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -25,14 +25,7 @@ def column_names_and_paths opt.on('-w') { |v| word = v } opt.on('-c') { |v| size = v } paths = opt.parse(ARGV) - column_names = - if [line, word, size].none? - %i[line_count word_count size] - else - { line_count: line, word_count: word, size: size }.map do |option, flag| - option if flag - end.compact - end + column_names = [line, word, size].none? ? %i[line_count word_count size] : { line_count: line, word_count: word, size: size }.select { |_, v| v }.keys [column_names, paths] end @@ -47,13 +40,10 @@ def build_statistics_row(input, name = nil) def build_statistics_matrix(paths) statistics_matrix = paths.map do |path| - build_statistics_row( File.read(path), path) - end - if statistics_matrix.length > 1 - statistics_matrix + [calculate_total(statistics_matrix)] - else - statistics_matrix + build_statistics_row(File.read(path), path) end + statistics_matrix << calculate_total(statistics_matrix) if paths.size > 1 + statistics_matrix end def calculate_total(statistics_matrix) @@ -78,11 +68,10 @@ def calculate_output_width(statistics_matrix, column_names) end def render(statistics, column_names, width) - column_names.each do |column_name| - print "#{statistics[column_name].to_s.rjust(width)} " + filterd_statistics = column_names.map do |column_name| + statistics[column_name].to_s.rjust(width) end - print statistics[:name] - puts '' + puts [*filterd_statistics, statistics[:name]].join(' ') end main