From 9f7bd506209da49585196b347a21e245826bf205 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Thu, 25 Apr 2024 17:32:28 +0900 Subject: [PATCH 01/13] =?UTF-8?q?wc=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=81=AE=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 作成完了し、少しリファクタリングもしたが、まだ改良の余地あり --- 05.wc/wc.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 05.wc/wc.rb diff --git a/05.wc/wc.rb b/05.wc/wc.rb new file mode 100755 index 0000000000..7d28dbc967 --- /dev/null +++ b/05.wc/wc.rb @@ -0,0 +1,64 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'optparse' +require 'debug' + +option = ARGV.getopts('lwc') +filename = ARGV + +def input_pattern(option) + words_width = 7 + gets_file = readlines + gets_file = gets_file.join('') + chars_in_file = gets_file.bytesize.to_s + lines_in_file = gets_file.lines.length.to_s + words_in_file = gets_file.split.count.to_s + option_values = { 'c' => chars_in_file, 'l' => lines_in_file, 'w' => words_in_file } + option_select = option.select { _2 == true } + use_option = option_select.empty? ? %w[l w c] : option_select.map { _1[0] } + display_value = use_option.length != 1 ? use_option.map { |item| option_values[item].rjust(words_width) } : [option_values[use_option[0]]] + puts display_value.join(' ') +end + +def display_values(filename, file_read, option, words_width) + matrix_to_display = Array.new(file_read.length) { [] } + file_read.map.with_index do |item, i| + chars_in_file = item.bytesize.to_s + lines_in_file = item.lines.length.to_s + words_in_file = item.split.count.to_s + option_values = { 'c' => chars_in_file, 'l' => lines_in_file, 'w' => words_in_file } + option_select = option.select { _2 == true } + use_option = option_select.empty? ? %w[l w c] : option_select.map { _1[0] } + display_value = use_option.length != 1 ? use_option.map { |item| option_values[item].rjust(words_width) } : [option_values[use_option[0]]] + display_value.map! { _1.rjust(words_width) } if filename.length > 1 + matrix_to_display[i].push(display_value.push(filename[i])) + end + matrix_to_display.each { puts _1.join(' ') } +end + +def display_totals(file_read, option, words_width) + total_l = 0 + total_w = 0 + total_c = 0 + file_read.map do |item| + total_l += item.lines.length + total_w += item.split.count + total_c += item.bytesize + end + option_values = { 'l' => total_l, 'w' => total_w, 'c' => total_c } + option_select = option.select { _2 == true } + use_option = option_select.empty? ? %w[l w c] : option_select.map { _1[0] } + display_total_values = use_option.map { |item| option_values[item].to_s.rjust(words_width) } if file_read.length > 1 + puts display_total_values.push('total').join(' ') +end + +if !filename.empty? + file_read = filename.map { File.read(_1) } + words_width = file_read.map { _1.bytesize.to_s }.max.length + + display_values(filename, file_read, option, words_width) + display_totals(file_read, option, words_width) if filename.length > 1 +else + input_pattern(option) +end From 1d7bd40c323f762a8423e297152fe0c3ac0668fe Mon Sep 17 00:00:00 2001 From: jepe-w Date: Fri, 26 Apr 2024 14:39:30 +0900 Subject: [PATCH 02/13] =?UTF-8?q?=E5=89=8D=E5=9B=9E=E3=82=B3=E3=83=9F?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=81=8B=E3=82=89=E7=89=B9=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=81=AA=E3=81=97=E3=80=80-m=20=E6=94=B9=E5=96=84?= =?UTF-8?q?=E3=81=AE=E4=BD=99=E5=9C=B0=E3=81=8C=E3=81=82=E3=82=8B=E6=B0=97?= =?UTF-8?q?=E3=81=AF=E3=81=A8=E3=81=A6=E3=82=82=E3=81=99=E3=82=8B=E3=81=8C?= =?UTF-8?q?=E3=80=81=E3=81=A9=E3=81=86=E6=94=B9=E5=96=84=E3=81=99=E3=81=B9?= =?UTF-8?q?=E3=81=8D=E3=81=8B=E3=82=8F=E3=81=8B=E3=82=89=E3=81=9A=E6=96=AD?= =?UTF-8?q?=E5=BF=B5?= 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 7d28dbc967..21840ef0ff 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -2,15 +2,15 @@ # frozen_string_literal: true require 'optparse' -require 'debug' option = ARGV.getopts('lwc') filename = ARGV -def input_pattern(option) +def standard_input(option) words_width = 7 gets_file = readlines gets_file = gets_file.join('') + chars_in_file = gets_file.bytesize.to_s lines_in_file = gets_file.lines.length.to_s words_in_file = gets_file.split.count.to_s @@ -60,5 +60,5 @@ def display_totals(file_read, option, words_width) display_values(filename, file_read, option, words_width) display_totals(file_read, option, words_width) if filename.length > 1 else - input_pattern(option) + standard_input(option) end From 5abe3bf222af03df7878c2310de827779a0fd297 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Thu, 2 May 2024 17:31:25 +0900 Subject: [PATCH 03/13] =?UTF-8?q?result=5Fof=5Foptions=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=AE=E3=81=95=E3=81=8F=E3=81=9B=E3=81=84?= =?UTF-8?q?=E3=80=82=E7=95=AA=E5=8F=B7=E6=8C=87=E5=AE=9A=E3=83=91=E3=83=A9?= =?UTF-8?q?=E3=83=A1=E3=83=BC=E3=82=BF=E3=82=92=E5=89=8A=E9=99=A4=E3=80=82?= =?UTF-8?q?=E3=83=8F=E3=83=83=E3=82=B7=E3=83=A5=E3=81=AE=E5=A4=89=E6=95=B0?= =?UTF-8?q?=E3=82=92=E8=A4=87=E6=95=B0=E5=BD=A2=E3=81=AB=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E3=80=82=E7=A0=B4=E5=A3=8A=E7=9A=84=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4=E3=80=82=E7=84=A1=E5=91=B3?= =?UTF-8?q?=E7=84=A1=E8=87=AD=E5=A4=89=E6=95=B0(item)=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E3=80=82=E3=83=88=E3=83=BC=E3=82=BF=E3=83=AB=E5=80=A4?= =?UTF-8?q?=E3=82=92=E3=83=8F=E3=83=83=E3=82=B7=E3=83=A5=E3=81=A7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E6=94=B9?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit レビューにて指摘いただいた箇所の修正。個人的に気になった個所の改善 --- 05.wc/wc.rb | 91 +++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 21840ef0ff..5f1b6d1e2b 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -3,62 +3,65 @@ require 'optparse' -option = ARGV.getopts('lwc') -filename = ARGV +def main + options = ARGV.getopts('lwc') + filename = ARGV + option_values = { 'l' => 0, 'w' => 0, 'c' => 0 } -def standard_input(option) + if !filename.empty? + file_read = filename.map { |file| File.read(file) } + words_width = file_read.map { |file_detail| file_detail.bytesize.to_s }.max.length + display_values(filename, file_read, options, words_width) + display_totals(option_values, file_read, options, words_width) if filename.length > 1 + else + standard_input(options) + end +end + +def standard_input(options) words_width = 7 gets_file = readlines gets_file = gets_file.join('') - chars_in_file = gets_file.bytesize.to_s - lines_in_file = gets_file.lines.length.to_s - words_in_file = gets_file.split.count.to_s - option_values = { 'c' => chars_in_file, 'l' => lines_in_file, 'w' => words_in_file } - option_select = option.select { _2 == true } - use_option = option_select.empty? ? %w[l w c] : option_select.map { _1[0] } - display_value = use_option.length != 1 ? use_option.map { |item| option_values[item].rjust(words_width) } : [option_values[use_option[0]]] - puts display_value.join(' ') + result_of_option = result_of_options(gets_file, options, words_width) + puts result_of_option.join(' ') end -def display_values(filename, file_read, option, words_width) +def display_values(filename, file_read, options, words_width) matrix_to_display = Array.new(file_read.length) { [] } - file_read.map.with_index do |item, i| - chars_in_file = item.bytesize.to_s - lines_in_file = item.lines.length.to_s - words_in_file = item.split.count.to_s - option_values = { 'c' => chars_in_file, 'l' => lines_in_file, 'w' => words_in_file } - option_select = option.select { _2 == true } - use_option = option_select.empty? ? %w[l w c] : option_select.map { _1[0] } - display_value = use_option.length != 1 ? use_option.map { |item| option_values[item].rjust(words_width) } : [option_values[use_option[0]]] - display_value.map! { _1.rjust(words_width) } if filename.length > 1 - matrix_to_display[i].push(display_value.push(filename[i])) + file_read.map.with_index do |file_detail, i| + result_of_option = result_of_options(file_detail, options, words_width) + if filename.length > 1 + rjust_result_of_option = result_of_option.map { |value| value.rjust(words_width) } + matrix_to_display[i].push(rjust_result_of_option.push(filename[i])) + else + matrix_to_display[i].push(result_of_option.push(filename[i])) + end end - matrix_to_display.each { puts _1.join(' ') } + matrix_to_display.each { |display| puts display.join(' ') } end -def display_totals(file_read, option, words_width) - total_l = 0 - total_w = 0 - total_c = 0 - file_read.map do |item| - total_l += item.lines.length - total_w += item.split.count - total_c += item.bytesize - end - option_values = { 'l' => total_l, 'w' => total_w, 'c' => total_c } - option_select = option.select { _2 == true } - use_option = option_select.empty? ? %w[l w c] : option_select.map { _1[0] } - display_total_values = use_option.map { |item| option_values[item].to_s.rjust(words_width) } if file_read.length > 1 - puts display_total_values.push('total').join(' ') +def result_of_options(files, options, words_width) + chars_in_file = files.bytesize.to_s + lines_in_file = files.lines.length.to_s + words_in_file = files.split.count.to_s + option_values = { 'l' => lines_in_file, 'w' => words_in_file, 'c' => chars_in_file } + option_select = options.select { |_key, value| value } + use_option = option_select.empty? ? %w[l w c] : option_select.map { |option| option[0] } + use_option.length != 1 ? use_option.map { |option| option_values[option].rjust(words_width) } : [option_values[use_option[0]]] end -if !filename.empty? - file_read = filename.map { File.read(_1) } - words_width = file_read.map { _1.bytesize.to_s }.max.length +def display_totals(option_values, file_read, options, words_width) + file_read.map do |file| + option_values['l'] += file.lines.length + option_values['w'] += file.split.count + option_values['c'] += file.bytesize + end + option_select = options.select { |_key, value| value } + use_option = option_select.empty? ? %w[l w c] : option_select.map { |option| option[0] } + display_total_values = use_option.map { |option| option_values[option].to_s.rjust(words_width) } if file_read.length > 1 - display_values(filename, file_read, option, words_width) - display_totals(file_read, option, words_width) if filename.length > 1 -else - standard_input(option) + puts display_total_values.push('total').join(' ') end + +main From 32af58fac0772ad038b7b234a2395ddbba76e1ab Mon Sep 17 00:00:00 2001 From: jepe-w Date: Wed, 8 May 2024 17:16:54 +0900 Subject: [PATCH 04/13] =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=81=AE=E8=AA=BF?= =?UTF-8?q?=E6=95=B4=E3=80=82obtained=5Foption=5Fvalue=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 配列filenameにつけた変数名を複数形へ修正。option_valuesの定義位置がおかしかったため移動。ifの条件を否定形ではなく、肯定形へ修正。ofを使用した変数名の修正。勘違いを誘発する変数名を修正。戻り値を使わないのにmapを使っていた箇所を修正 --- 05.wc/wc.rb | 86 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 5f1b6d1e2b..d491b0973f 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -2,65 +2,75 @@ # frozen_string_literal: true require 'optparse' +require 'debug' def main options = ARGV.getopts('lwc') - filename = ARGV - option_values = { 'l' => 0, 'w' => 0, 'c' => 0 } + filenames = ARGV - if !filename.empty? - file_read = filename.map { |file| File.read(file) } - words_width = file_read.map { |file_detail| file_detail.bytesize.to_s }.max.length - display_values(filename, file_read, options, words_width) - display_totals(option_values, file_read, options, words_width) if filename.length > 1 + if filenames.any? + file_details = filenames.map { |file| File.read(file) } + words_width = file_details.map { |detail| detail.bytesize.to_s }.max.length + display_values(filenames, file_details, options, words_width) + display_totals(file_details, options, words_width) if filenames.length > 1 else - standard_input(options) + display_inputted_values(options) end end -def standard_input(options) +def display_inputted_values(options) words_width = 7 - gets_file = readlines - gets_file = gets_file.join('') + gets_inputted_values = readlines + input_values = gets_inputted_values.join('') - result_of_option = result_of_options(gets_file, options, words_width) - puts result_of_option.join(' ') + option_values = obtained_option_value(input_values, options, words_width) + puts option_values.join(' ') end -def display_values(filename, file_read, options, words_width) - matrix_to_display = Array.new(file_read.length) { [] } - file_read.map.with_index do |file_detail, i| - result_of_option = result_of_options(file_detail, options, words_width) - if filename.length > 1 - rjust_result_of_option = result_of_option.map { |value| value.rjust(words_width) } - matrix_to_display[i].push(rjust_result_of_option.push(filename[i])) +def display_values(filenames, file_details, options, words_width) + matrix_to_display = Array.new(file_details.length) { [] } + file_details.map.with_index do |file_detail, i| + option_values = obtained_option_value(file_detail, options, words_width) + if filenames.length > 1 + rjust_result_of_option = option_values.map { |value| value.rjust(words_width) } + matrix_to_display[i].push(rjust_result_of_option.push(filenames[i])) else - matrix_to_display[i].push(result_of_option.push(filename[i])) + matrix_to_display[i].push(option_values.push(filenames[i])) end end matrix_to_display.each { |display| puts display.join(' ') } end -def result_of_options(files, options, words_width) - chars_in_file = files.bytesize.to_s - lines_in_file = files.lines.length.to_s - words_in_file = files.split.count.to_s - option_values = { 'l' => lines_in_file, 'w' => words_in_file, 'c' => chars_in_file } - option_select = options.select { |_key, value| value } - use_option = option_select.empty? ? %w[l w c] : option_select.map { |option| option[0] } - use_option.length != 1 ? use_option.map { |option| option_values[option].rjust(words_width) } : [option_values[use_option[0]]] +def obtained_option_value(files, options, words_width) + chars = files.bytesize.to_s + lines = files.lines.length.to_s + words = files.split.count.to_s + use_option = options.select { |_key, value| value }.keys + options_hash = {} + use_option.each do |option| + case option + when 'l' then options_hash['l'] = lines + when 'w' then options_hash['w'] = words + when 'c' then options_hash['c'] = chars + end + end + if use_option.empty? + use_option = %w[l w c] + options_hash = { 'l' => lines, 'w' => words, 'c' => chars } + end + options_hash.length > 1 ? use_option.map { |option| options_hash[option].rjust(words_width) } : [options_hash[use_option[0]]] end -def display_totals(option_values, file_read, options, words_width) - file_read.map do |file| - option_values['l'] += file.lines.length - option_values['w'] += file.split.count - option_values['c'] += file.bytesize +def display_totals(file_details, options, words_width) + options_hash = { 'l' => 0, 'w' => 0, 'c' => 0 } + file_details.each do |detail| + options_hash['l'] += detail.lines.length + options_hash['w'] += detail.split.count + options_hash['c'] += detail.bytesize end - option_select = options.select { |_key, value| value } - use_option = option_select.empty? ? %w[l w c] : option_select.map { |option| option[0] } - display_total_values = use_option.map { |option| option_values[option].to_s.rjust(words_width) } if file_read.length > 1 - + use_option = options.select { |_key, value| value }.keys + use_option = %w[l w c] if use_option.empty? + display_total_values = use_option.map { |option| options_hash[option].to_s.rjust(words_width) } if file_details.length > 1 puts display_total_values.push('total').join(' ') end From 2ee75709c6a39e96cd5e69aae1a36547171acd8b Mon Sep 17 00:00:00 2001 From: jepe-w Date: Wed, 8 May 2024 17:17:21 +0900 Subject: [PATCH 05/13] =?UTF-8?q?require=20'debug'=E3=81=AE=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= 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 d491b0973f..40cea42d6c 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -2,7 +2,6 @@ # frozen_string_literal: true require 'optparse' -require 'debug' def main options = ARGV.getopts('lwc') From 4b7b832102a0c2c027d52a33c0d121468af25db6 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Wed, 15 May 2024 13:44:10 +0900 Subject: [PATCH 06/13] =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92=E5=B0=8F?= =?UTF-8?q?=E5=88=86=E3=81=91=E3=81=AB=E3=81=97=E3=81=A6=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2人三脚ロジックを解消するために、hashを作るメソッド、標準入力か引数にファイル名を渡すかで違うメソッドを使うように修正。 --- 05.wc/wc.rb | 98 ++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 40cea42d6c..1399a1f4de 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -6,71 +6,69 @@ def main options = ARGV.getopts('lwc') filenames = ARGV - + use_opts = options.select { |_key, value| value }.empty? ? %w[l w c] : options.select { |_key, value| value }.keys if filenames.any? - file_details = filenames.map { |file| File.read(file) } - words_width = file_details.map { |detail| detail.bytesize.to_s }.max.length - display_values(filenames, file_details, options, words_width) - display_totals(file_details, options, words_width) if filenames.length > 1 + display_values(filenames, use_opts) else - display_inputted_values(options) + display_inputted_values(use_opts) end end -def display_inputted_values(options) - words_width = 7 - gets_inputted_values = readlines - input_values = gets_inputted_values.join('') - - option_values = obtained_option_value(input_values, options, words_width) - puts option_values.join(' ') +def create_detail_hash(files) + files.map.with_index do |file, i| + file = File.read(file) if files.length > 1 + { + 'name' => files[i], + 'c' => file.bytesize.to_s, + 'l' => file.lines.length.to_s, + 'w' => file.split.count.to_s + } + end end -def display_values(filenames, file_details, options, words_width) - matrix_to_display = Array.new(file_details.length) { [] } - file_details.map.with_index do |file_detail, i| - option_values = obtained_option_value(file_detail, options, words_width) - if filenames.length > 1 - rjust_result_of_option = option_values.map { |value| value.rjust(words_width) } - matrix_to_display[i].push(rjust_result_of_option.push(filenames[i])) - else - matrix_to_display[i].push(option_values.push(filenames[i])) - end +def matrix_values(detail_hash, use_opts, words_width) + if use_opts.length > 1 + detail_hash.map { |hash| use_opts.map { |option| hash[option.to_s].rjust(words_width) }.push(hash['name']) } + else + detail_hash.map { |hash| "#{hash[use_opts[0].to_s]} #{hash[0]}" } end - matrix_to_display.each { |display| puts display.join(' ') } end -def obtained_option_value(files, options, words_width) - chars = files.bytesize.to_s - lines = files.lines.length.to_s - words = files.split.count.to_s - use_option = options.select { |_key, value| value }.keys - options_hash = {} - use_option.each do |option| - case option - when 'l' then options_hash['l'] = lines - when 'w' then options_hash['w'] = words - when 'c' then options_hash['c'] = chars - end - end - if use_option.empty? - use_option = %w[l w c] - options_hash = { 'l' => lines, 'w' => words, 'c' => chars } +def values(detail_hash, use_opts, words_width) + if use_opts.length > 1 + use_opts.map { |option| detail_hash[0][option.to_s].rjust(words_width) } + else + use_opts.map { |option| detail_hash[0][option.to_s] } end - options_hash.length > 1 ? use_option.map { |option| options_hash[option].rjust(words_width) } : [options_hash[use_option[0]]] end -def display_totals(file_details, options, words_width) - options_hash = { 'l' => 0, 'w' => 0, 'c' => 0 } +def display_totals(file_details, use_opts, words_width) + value_hash = { 'l' => 0, 'w' => 0, 'c' => 0 } file_details.each do |detail| - options_hash['l'] += detail.lines.length - options_hash['w'] += detail.split.count - options_hash['c'] += detail.bytesize + value_hash['l'] += detail.lines.length + value_hash['w'] += detail.split.count + value_hash['c'] += detail.bytesize end - use_option = options.select { |_key, value| value }.keys - use_option = %w[l w c] if use_option.empty? - display_total_values = use_option.map { |option| options_hash[option].to_s.rjust(words_width) } if file_details.length > 1 - puts display_total_values.push('total').join(' ') + total_values = use_opts.length > 1 ? use_opts.map { |option| value_hash[option].to_s.rjust(words_width) } : use_opts.map { |option| value_hash[option] } + puts total_values.push('total').join(' ') +end + +def display_values(filenames, use_opts) + file_details = filenames.map { |file| File.read(file) } + words_width = file_details.map(&:bytesize).max.to_s.length + detail_hash = create_detail_hash(filenames) + values = matrix_values(detail_hash, use_opts, words_width) + use_opts.length > 1 ? values.each { |item| puts item.join(' ') } : values.each { |item| puts item } + display_totals(file_details, use_opts, words_width) if filenames.length > 1 +end + +def display_inputted_values(use_opts) + words_width = 7 + gets_inputted_values = readlines + files = [gets_inputted_values.join('')] + detail_hash = create_detail_hash(files) + display_values = values(detail_hash, use_opts, words_width) + puts display_values.join(' ') end main From f7633891457f5efd386074cffc8f660087fa7a1a Mon Sep 17 00:00:00 2001 From: jepe-w Date: Wed, 15 May 2024 13:51:56 +0900 Subject: [PATCH 07/13] =?UTF-8?q?=E5=A4=89=E6=95=B0=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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 1399a1f4de..2adc32297b 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -14,7 +14,7 @@ def main end end -def create_detail_hash(files) +def create_detail_hashes(files) files.map.with_index do |file, i| file = File.read(file) if files.length > 1 { @@ -53,7 +53,7 @@ def display_totals(file_details, use_opts, words_width) puts total_values.push('total').join(' ') end -def display_values(filenames, use_opts) +def display_files_values(filenames, use_opts) file_details = filenames.map { |file| File.read(file) } words_width = file_details.map(&:bytesize).max.to_s.length detail_hash = create_detail_hash(filenames) From fcc03cdc52cda71d3cc8f4bfd14992a3810e6113 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Thu, 16 May 2024 08:19:28 +0900 Subject: [PATCH 08/13] =?UTF-8?q?=E5=A4=89=E6=95=B0=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 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 2adc32297b..3cc562af11 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -8,7 +8,7 @@ def main filenames = ARGV use_opts = options.select { |_key, value| value }.empty? ? %w[l w c] : options.select { |_key, value| value }.keys if filenames.any? - display_values(filenames, use_opts) + display_files_values(filenames, use_opts) else display_inputted_values(use_opts) end @@ -56,7 +56,7 @@ def display_totals(file_details, use_opts, words_width) def display_files_values(filenames, use_opts) file_details = filenames.map { |file| File.read(file) } words_width = file_details.map(&:bytesize).max.to_s.length - detail_hash = create_detail_hash(filenames) + detail_hash = create_detail_hashes(filenames) values = matrix_values(detail_hash, use_opts, words_width) use_opts.length > 1 ? values.each { |item| puts item.join(' ') } : values.each { |item| puts item } display_totals(file_details, use_opts, words_width) if filenames.length > 1 @@ -66,7 +66,7 @@ def display_inputted_values(use_opts) words_width = 7 gets_inputted_values = readlines files = [gets_inputted_values.join('')] - detail_hash = create_detail_hash(files) + detail_hash = create_detail_hashes(files) display_values = values(detail_hash, use_opts, words_width) puts display_values.join(' ') end From 68ede5f67f00207964dcc6f4d32bb9f8c5e10d82 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Tue, 21 May 2024 16:17:08 +0900 Subject: [PATCH 09/13] =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=81=AE=E9=A0=86=E7=95=AA=E3=82=92=E8=AA=BF=E6=95=B4=E3=80=82?= =?UTF-8?q?=E5=A4=89=E6=95=B0=E5=90=8D=E3=82=92=E8=AA=BF=E6=95=B4=E3=80=82?= =?UTF-8?q?=E5=85=A8=E4=BD=93=E7=9A=84=E3=81=AB=E3=83=AA=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 前回のレビューにて指摘を受けた、強引な処理や、へんてこなコードを自分なりに改善。 --- 05.wc/wc.rb | 89 +++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 3cc562af11..f450121edd 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -6,69 +6,64 @@ def main options = ARGV.getopts('lwc') filenames = ARGV - use_opts = options.select { |_key, value| value }.empty? ? %w[l w c] : options.select { |_key, value| value }.keys + use_opts = options.select { |_key, value| value }.keys + use_opts = %w[l w c] if use_opts.empty? + if filenames.any? - display_files_values(filenames, use_opts) + display_values = display_files_values(filenames, use_opts) + display_values.each { |value| use_opts.length >= 2 ? (puts value.join(' ')) : (puts value) } else - display_inputted_values(use_opts) - end -end - -def create_detail_hashes(files) - files.map.with_index do |file, i| - file = File.read(file) if files.length > 1 - { - 'name' => files[i], - 'c' => file.bytesize.to_s, - 'l' => file.lines.length.to_s, - 'w' => file.split.count.to_s - } + gets_inputted_values = readlines + input_text = gets_inputted_values.join('') + display_values = display_files_values(input_text, use_opts) + puts display_values.join(' ') end end -def matrix_values(detail_hash, use_opts, words_width) - if use_opts.length > 1 - detail_hash.map { |hash| use_opts.map { |option| hash[option.to_s].rjust(words_width) }.push(hash['name']) } +def display_files_values(files, use_opts) + if files.instance_of?(String) + handle_standard_input(files, use_opts) else - detail_hash.map { |hash| "#{hash[use_opts[0].to_s]} #{hash[0]}" } + handle_multiple_files(files, use_opts) end end -def values(detail_hash, use_opts, words_width) - if use_opts.length > 1 - use_opts.map { |option| detail_hash[0][option.to_s].rjust(words_width) } - else - use_opts.map { |option| detail_hash[0][option.to_s] } - end +def handle_standard_input(files, use_opts) + file_details = files + detail_hash = create_detail_hash(file_details, 'input') + create_display_values(detail_hash, use_opts) end -def display_totals(file_details, use_opts, words_width) - value_hash = { 'l' => 0, 'w' => 0, 'c' => 0 } - file_details.each do |detail| - value_hash['l'] += detail.lines.length - value_hash['w'] += detail.split.count - value_hash['c'] += detail.bytesize +def handle_multiple_files(files, use_opts) + file_details = files.map { |file| File.read(file) } + if files.length >= 2 + file_details.push(file_details.join('')) + files.push('total') end - total_values = use_opts.length > 1 ? use_opts.map { |option| value_hash[option].to_s.rjust(words_width) } : use_opts.map { |option| value_hash[option] } - puts total_values.push('total').join(' ') + detail_hash = file_details.map.with_index { |filedetail, index| create_detail_hash(filedetail, files[index]) } + words_width = detail_hash.map { |hash| hash.select { |_key, value| value.instance_of?(Integer) }.values.max }.max.to_s.length + words_width = 0 if detail_hash.length <= 1 && use_opts.length <= 1 + create_display_values(detail_hash, use_opts, words_width) end -def display_files_values(filenames, use_opts) - file_details = filenames.map { |file| File.read(file) } - words_width = file_details.map(&:bytesize).max.to_s.length - detail_hash = create_detail_hashes(filenames) - values = matrix_values(detail_hash, use_opts, words_width) - use_opts.length > 1 ? values.each { |item| puts item.join(' ') } : values.each { |item| puts item } - display_totals(file_details, use_opts, words_width) if filenames.length > 1 +def create_detail_hash(filedetails, filename) + { + 'name' => filename, + 'c' => filedetails.bytesize, + 'l' => filedetails.lines.length, + 'w' => filedetails.split.count + } end -def display_inputted_values(use_opts) - words_width = 7 - gets_inputted_values = readlines - files = [gets_inputted_values.join('')] - detail_hash = create_detail_hashes(files) - display_values = values(detail_hash, use_opts, words_width) - puts display_values.join(' ') +def create_display_values(detail_hash, use_opts, words_width = 0) + standard_input_data = detail_hash.instance_of?(Hash) + if standard_input_data + use_opts.map { |option| detail_hash[option.to_s] } + elsif use_opts.length >= 2 + detail_hash.map { |hash| use_opts.map { |option| hash[option.to_s].to_s.rjust(words_width) }.push(hash['name']) } + else + detail_hash.map { |hash| "#{hash[use_opts[0].to_s].to_s.rjust(words_width)} #{hash['name']}" } + end end main From 0b7f2eebc8d1cf171fc2ebfc1a1f0ebc66585aff Mon Sep 17 00:00:00 2001 From: jepe-w Date: Fri, 24 May 2024 11:23:51 +0900 Subject: [PATCH 10/13] =?UTF-8?q?display=5Fvalues=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=AE=E6=94=B9=E5=96=84=E3=80=82option?= =?UTF-8?q?=E3=82=92=E3=83=8F=E3=83=83=E3=82=B7=E3=83=A5=E3=81=AE=E3=81=BE?= =?UTF-8?q?=E3=81=BE=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B=E6=96=B9=E6=B3=95?= =?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 | 91 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index f450121edd..884b0828f0 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -5,64 +5,77 @@ def main options = ARGV.getopts('lwc') + if options.values.uniq.length == 1 + options = options.each_key do |key| + options[key] = true + end + end filenames = ARGV - use_opts = options.select { |_key, value| value }.keys - use_opts = %w[l w c] if use_opts.empty? if filenames.any? - display_values = display_files_values(filenames, use_opts) - display_values.each { |value| use_opts.length >= 2 ? (puts value.join(' ')) : (puts value) } + handle_multiple_files(filenames, options) else - gets_inputted_values = readlines - input_text = gets_inputted_values.join('') - display_values = display_files_values(input_text, use_opts) - puts display_values.join(' ') + inputted_values = readlines + input_text = inputted_values.join('') + handle_standard_input(input_text, options) end end -def display_files_values(files, use_opts) - if files.instance_of?(String) - handle_standard_input(files, use_opts) - else - handle_multiple_files(files, use_opts) - end +def handle_standard_input(input_text, options) + detail_hash = create_detail_hash(input_text) + display_values([detail_hash], options) end -def handle_standard_input(files, use_opts) - file_details = files - detail_hash = create_detail_hash(file_details, 'input') - create_display_values(detail_hash, use_opts) -end - -def handle_multiple_files(files, use_opts) - file_details = files.map { |file| File.read(file) } +def handle_multiple_files(files, options) + file_texts = files.map do |file| + File.read(file) + end if files.length >= 2 - file_details.push(file_details.join('')) + file_texts.push(file_texts.join('')) files.push('total') end - detail_hash = file_details.map.with_index { |filedetail, index| create_detail_hash(filedetail, files[index]) } - words_width = detail_hash.map { |hash| hash.select { |_key, value| value.instance_of?(Integer) }.values.max }.max.to_s.length - words_width = 0 if detail_hash.length <= 1 && use_opts.length <= 1 - create_display_values(detail_hash, use_opts, words_width) + detail_hash = file_texts.map.with_index do |file_text, index| + create_detail_hash(file_text, files[index]) + end + words_width = create_words_width(detail_hash, options) + display_values(detail_hash, options, words_width) end -def create_detail_hash(filedetails, filename) +def create_words_width(detail_hash, options) + numbers_hash = detail_hash.map do |hash| + hash.reject do |key, _value| + key == 'name' + end + end + + if detail_hash.length <= 1 && options.count do |_key, value| + value == true + end <= 1 + 0 + else + numbers_hash.map do |number| + number.values.max + end.max.to_s.length + end +end + +def create_detail_hash(text, filename = 'default') { 'name' => filename, - 'c' => filedetails.bytesize, - 'l' => filedetails.lines.length, - 'w' => filedetails.split.count + 'c' => text.bytesize, + 'l' => text.lines.length, + 'w' => text.split.count } end -def create_display_values(detail_hash, use_opts, words_width = 0) - standard_input_data = detail_hash.instance_of?(Hash) - if standard_input_data - use_opts.map { |option| detail_hash[option.to_s] } - elsif use_opts.length >= 2 - detail_hash.map { |hash| use_opts.map { |option| hash[option.to_s].to_s.rjust(words_width) }.push(hash['name']) } - else - detail_hash.map { |hash| "#{hash[use_opts[0].to_s].to_s.rjust(words_width)} #{hash['name']}" } +def display_values(file_details, options, words_width = 0) + file_details.each do |file_detail| + columns = [] + columns << file_detail['l'].to_s.rjust(words_width) if options['l'] + columns << file_detail['w'].to_s.rjust(words_width) if options['w'] + columns << file_detail['c'].to_s.rjust(words_width) if options['c'] + columns << file_detail['name'] if file_detail['name'] != 'default' + puts columns.join(' ') end end From f1ccf042036a2718364a4b07c4aaacb7d7824178 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Mon, 27 May 2024 11:39:20 +0900 Subject: [PATCH 11/13] =?UTF-8?q?options=E3=81=AE=E5=80=A4=E3=82=92?= =?UTF-8?q?=E3=81=99=E3=81=B9=E3=81=A6false=E3=81=A0=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=81=ABtrue=E3=81=B8=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3=E3=80=82=E6=A8=99?= =?UTF-8?q?=E6=BA=96=E5=85=A5=E5=8A=9B=E3=81=A7=E5=8F=97=E3=81=91=E5=8F=96?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E5=80=A4=E3=81=AE=E5=A4=89=E6=95=B0=E3=81=B8?= =?UTF-8?q?=E3=81=AE=E6=A0=BC=E7=B4=8D=E6=96=B9=E6=B3=95=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E3=80=82=E4=BA=8C=E4=BA=BA=E4=B8=89=E8=84=9A=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89(handle=5Fmultiple=5Ffiles=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89)=E3=81=AE=E6=94=B9=E5=96=84=E3=80=82total?= =?UTF-8?q?=E5=80=A4=E3=81=AE=E5=8F=96=E5=BE=97=E6=96=B9=E6=B3=95=E3=82=92?= =?UTF-8?q?=E6=94=B9=E5=96=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit もともとはoptionの値がすべて同じ場合はという条件だったが、より意図が伝わりやすいすべてfalseだった場合という方法へ改善。標準入力で受け取った値をいったん変数へ入れる冗長な手順を削除し、受け取った値をそのままjoinするように修正。total値の取得方法として、文字列をくっつけてその文字数等を取得するやり方をしていたが、このやり方では、場合によって思いがけない誤差が生まれる可能性があるため、値の合計値を取得するやり方に修正。failnameのデフォルト値を必ず重複しない値('')に修正。 --- 05.wc/wc.rb | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 884b0828f0..940cb210ec 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -5,50 +5,50 @@ def main options = ARGV.getopts('lwc') - if options.values.uniq.length == 1 - options = options.each_key do |key| - options[key] = true - end - end + options.transform_values! { true } if options.values.all? { |value| value == false } filenames = ARGV if filenames.any? handle_multiple_files(filenames, options) else - inputted_values = readlines - input_text = inputted_values.join('') - handle_standard_input(input_text, options) + text = readlines.join('') + handle_standard_input(text, options) end end -def handle_standard_input(input_text, options) - detail_hash = create_detail_hash(input_text) - display_values([detail_hash], options) +def handle_standard_input(text, options) + detail_hashes = create_detail_hash(text) + display_values([detail_hashes], options) end def handle_multiple_files(files, options) - file_texts = files.map do |file| - File.read(file) + detail_hashes = files.map do |file| + text = File.read(file) + detail = create_detail_hash(text) + detail['name'] = file + detail end if files.length >= 2 - file_texts.push(file_texts.join('')) - files.push('total') - end - detail_hash = file_texts.map.with_index do |file_text, index| - create_detail_hash(file_text, files[index]) + total = { 'name' => 'total' } + options.each_key do |option| + total[option.to_s] = detail_hashes.inject(0) do |sum, hash| + sum + hash[option.to_s] + end + end + detail_hashes << total end - words_width = create_words_width(detail_hash, options) - display_values(detail_hash, options, words_width) + words_width = create_words_width(detail_hashes, options) + display_values(detail_hashes, options, words_width) end -def create_words_width(detail_hash, options) - numbers_hash = detail_hash.map do |hash| +def create_words_width(detail_hashes, options) + numbers_hash = detail_hashes.map do |hash| hash.reject do |key, _value| key == 'name' end end - if detail_hash.length <= 1 && options.count do |_key, value| + if detail_hashes.length <= 1 && options.count do |_key, value| value == true end <= 1 0 @@ -59,7 +59,7 @@ def create_words_width(detail_hash, options) end end -def create_detail_hash(text, filename = 'default') +def create_detail_hash(text, filename = '') { 'name' => filename, 'c' => text.bytesize, @@ -74,7 +74,7 @@ def display_values(file_details, options, words_width = 0) columns << file_detail['l'].to_s.rjust(words_width) if options['l'] columns << file_detail['w'].to_s.rjust(words_width) if options['w'] columns << file_detail['c'].to_s.rjust(words_width) if options['c'] - columns << file_detail['name'] if file_detail['name'] != 'default' + columns << file_detail['name'] if file_detail['name'] != '' puts columns.join(' ') end end From 0e94747f5492996d3274eab9e84b0d139f69b492 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Thu, 30 May 2024 10:54:31 +0900 Subject: [PATCH 12/13] =?UTF-8?q?=E7=84=A1=E9=A7=84=E3=80=81=E5=86=97?= =?UTF-8?q?=E9=95=B7=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E3=80=82=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E5=90=8D=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=20=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E5=90=8D=E5=A4=89=E6=9B=B4=20create=5Fwords=5Fwidth=20->=20cal?= =?UTF-8?q?c=5Fcolumn=5Fwidth=20=20(l.55~)=20if=E3=81=AE=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E3=81=A7=E6=94=B9=E8=A1=8C=E3=81=95=E3=82=8C=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=82=8B=E3=82=82=E3=81=AE=E3=82=92=E8=A6=8B=E3=82=84=E3=81=99?= =?UTF-8?q?=E3=81=8F=E3=81=99=E3=82=8B=E3=81=9F=E3=82=81=E3=81=AB=E3=80=81?= =?UTF-8?q?(l.51)=20=E3=81=A7=E5=A4=89=E6=95=B0=E3=81=B8=E3=81=84=E3=81=A3?= =?UTF-8?q?=E3=81=9F=E3=82=93=E4=BB=A3=E5=85=A5=E3=80=82=20handle=5Fmultip?= =?UTF-8?q?le=5Ffiles=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=AB?= =?UTF-8?q?=E3=81=A6=E3=80=81total[option.to=5Fs]=E3=81=A8=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9Fto=5Fs=E3=82=92=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=80=82=E3=83=88=E3=83=BC=E3=82=BF=E3=83=AB=E5=80=A4=E3=82=92?= =?UTF-8?q?sum=E3=81=A7=E7=AE=97=E5=87=BA=20=E3=81=AB=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=80=82l8=20{=20|value|=20value=20=3D=3D=20false=20}=E3=82=92?= =?UTF-8?q?!value=E3=81=B8=E4=BF=AE=E6=AD=A3=E3=80=82"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.wc/wc.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 940cb210ec..673bdf9ba0 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -5,7 +5,7 @@ def main options = ARGV.getopts('lwc') - options.transform_values! { true } if options.values.all? { |value| value == false } + options.transform_values! { true } if options.values.all?(&:!) filenames = ARGV if filenames.any? @@ -31,26 +31,28 @@ def handle_multiple_files(files, options) if files.length >= 2 total = { 'name' => 'total' } options.each_key do |option| - total[option.to_s] = detail_hashes.inject(0) do |sum, hash| - sum + hash[option.to_s] + total[option] = detail_hashes.sum do |hash| + hash[option] end end detail_hashes << total end - words_width = create_words_width(detail_hashes, options) - display_values(detail_hashes, options, words_width) + width = calc_column_width(detail_hashes, options) + display_values(detail_hashes, options, width) end -def create_words_width(detail_hashes, options) +def calc_column_width(detail_hashes, options) numbers_hash = detail_hashes.map do |hash| hash.reject do |key, _value| key == 'name' end end - if detail_hashes.length <= 1 && options.count do |_key, value| + options_count = options.count do |_key, value| value == true - end <= 1 + end + + if detail_hashes.length <= 1 && options_count <= 1 0 else numbers_hash.map do |number| @@ -68,12 +70,12 @@ def create_detail_hash(text, filename = '') } end -def display_values(file_details, options, words_width = 0) +def display_values(file_details, options, width = 0) file_details.each do |file_detail| columns = [] - columns << file_detail['l'].to_s.rjust(words_width) if options['l'] - columns << file_detail['w'].to_s.rjust(words_width) if options['w'] - columns << file_detail['c'].to_s.rjust(words_width) if options['c'] + columns << file_detail['l'].to_s.rjust(width) if options['l'] + columns << file_detail['w'].to_s.rjust(width) if options['w'] + columns << file_detail['c'].to_s.rjust(width) if options['c'] columns << file_detail['name'] if file_detail['name'] != '' puts columns.join(' ') end From 04125bbf6c8017000e3d2ff74d491dfc03df84a0 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Fri, 31 May 2024 10:03:13 +0900 Subject: [PATCH 13/13] =?UTF-8?q?calc=5Fcolumn=5Fwidth=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=AE=E4=BF=AE=E6=AD=A3=E3=80=82=E3=81=9D?= =?UTF-8?q?=E3=81=AE=E4=BB=96=E6=8C=87=E6=91=98=E7=AE=87=E6=89=80=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit calc_column_widthにて、文字数の最大値を求めるコードを、自然と一番大きくなるtotalのバイト数を取得する方法に変更。handle_multiple_filesメソッドのtotalを求めるコードを簡略化。optionの値について、values.none?でtrueが返ってくる場合のみ要素をすべて反転するという処理に修正。 --- 05.wc/wc.rb | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/05.wc/wc.rb b/05.wc/wc.rb index 673bdf9ba0..927eeab1ac 100755 --- a/05.wc/wc.rb +++ b/05.wc/wc.rb @@ -5,13 +5,13 @@ def main options = ARGV.getopts('lwc') - options.transform_values! { true } if options.values.all?(&:!) + options.transform_values!(&:!) if options.values.none? filenames = ARGV if filenames.any? handle_multiple_files(filenames, options) else - text = readlines.join('') + text = readlines.join handle_standard_input(text, options) end end @@ -31,9 +31,7 @@ def handle_multiple_files(files, options) if files.length >= 2 total = { 'name' => 'total' } options.each_key do |option| - total[option] = detail_hashes.sum do |hash| - hash[option] - end + total[option] = detail_hashes.sum { |hash| hash[option] } end detail_hashes << total end @@ -42,12 +40,6 @@ def handle_multiple_files(files, options) end def calc_column_width(detail_hashes, options) - numbers_hash = detail_hashes.map do |hash| - hash.reject do |key, _value| - key == 'name' - end - end - options_count = options.count do |_key, value| value == true end @@ -55,9 +47,7 @@ def calc_column_width(detail_hashes, options) if detail_hashes.length <= 1 && options_count <= 1 0 else - numbers_hash.map do |number| - number.values.max - end.max.to_s.length + detail_hashes[-1]['c'].to_s.length end end