Skip to content
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
65c31b5
wcコマンドを作成(l、w、cオプション)
s-tone-gs Sep 10, 2025
f16378f
rubocopで修正
s-tone-gs Sep 11, 2025
5fd8ba8
flagsによる制御を最小限にするために修正
s-tone-gs Sep 12, 2025
581b8fc
コードリーディングをしやすくするために修正
s-tone-gs Sep 16, 2025
1ee4ccd
実行効率を上げるために修正
s-tone-gs Sep 16, 2025
77ea30e
今後予期せぬ副作用が起こらないように修正
s-tone-gs Sep 16, 2025
44beccb
出力のスタイルを修正
s-tone-gs Sep 16, 2025
c0ac463
出力する値を間違えていたので修正
s-tone-gs Sep 16, 2025
0eed6c8
rubocopの修正を反映
s-tone-gs Sep 16, 2025
90934e9
出力幅が動的に変化するように修正
s-tone-gs Sep 22, 2025
ea6443d
上から下に自然に読めるように修正
s-tone-gs Sep 22, 2025
3d09423
メソッドからの返り値の受け取り方を変更
s-tone-gs Sep 24, 2025
0506db2
メソッド設計を見直し
s-tone-gs Sep 24, 2025
07680e4
余計な改行を削除
s-tone-gs Sep 24, 2025
f38807f
メソッド名を見直し
s-tone-gs Sep 24, 2025
c0dd417
必須要件外の実装を削除
s-tone-gs Sep 24, 2025
18cd3cf
合計算出のメソッドのロジックをシンプルに修正
s-tone-gs Sep 24, 2025
e1fceb9
レイアウト調整のメソッドをシンプルに
s-tone-gs Sep 24, 2025
bc05889
オプションの出力制御の方法を変更
s-tone-gs Sep 24, 2025
740c96f
rubocopの修正提案を反映
s-tone-gs Sep 24, 2025
1ac3773
変数名を修正
s-tone-gs Sep 24, 2025
d31e9b1
返り値や引数の順番を修正
s-tone-gs Sep 25, 2025
e11ccf9
命名を修正
s-tone-gs Sep 25, 2025
fa413ca
キーワード引数である必要がなくなったので修正
s-tone-gs Sep 25, 2025
200be15
冗長であったため修正
s-tone-gs Sep 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions 05.wc/wc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require 'optparse'

def main
column_names, paths = column_names_and_paths
statistics_matrix =
if paths.empty?
[build_statistics_row($stdin.read)]
else
build_statistics_matrix(paths)
end
width = calculate_output_width(statistics_matrix, column_names)
statistics_matrix.each do |statistics|
render(statistics, column_names, width)
end
end

def column_names_and_paths
line = false
word = false
size = false
opt = OptionParser.new
opt.on('-l') { |v| line = v }
opt.on('-w') { |v| word = v }
opt.on('-c') { |v| size = v }
paths = opt.parse(ARGV)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARGVを直接記述しました

column_names = [line, word, size].none? ? %i[line_count word_count size] : { line_count: line, word_count: word, size: size }.select { |_, v| v }.keys
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ちょっと横に長すぎる気がしますね。改行した方が読みやすいかも。

Suggested change
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 = [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

def build_statistics_row(input, name = nil)
{
line_count: input.lines.count,
word_count: input.split.count,
size: input.size,
name: name
}
end

def build_statistics_matrix(paths)
statistics_matrix = paths.map do |path|
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)
line_count_sum = 0
word_count_sum = 0
size_sum = 0
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(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(statistics, column_names, width)
filterd_statistics = column_names.map do |column_name|
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filterd -> filtered ですね。
ただ、filteredだと不要なデータを取り除くイメージです。
https://docs.ruby-lang.org/ja/latest/method/Array/i/filter.html

ここでやってるのは見た目の調整だと思うので、formatted とかの方が良さそうです

statistics[column_name].to_s.rjust(width)
end
puts [*filterd_statistics, statistics[:name]].join(' ')
end

main