forked from fjordllc/ruby-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
wcコマンドの実装 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kutimiti1234
wants to merge
41
commits into
main
Choose a base branch
from
my-wc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
wcコマンドの実装 #12
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
1d98489
wcコマンドの入力と出力を記述
kutimiti1234 0245795
lオプション、wオプション、表示形式の実装
kutimiti1234 c32b622
実態を表した変数名に変更。また、表示スペースをCONSTANTで調整)
kutimiti1234 1881ba6
不要なファイルをコミットしたので削除
kutimiti1234 5497ac7
集計行を追加
kutimiti1234 a95e93b
表示の調整
kutimiti1234 6004c3f
-w -c コマンド使用時に不要な隙間が発生していたので修正。また、一部見え方を修正
kutimiti1234 0f17f1a
集計行は複数引数のみ表示されるように修正。また一部関数の複雑さを軽減
kutimiti1234 5853d2f
不要なファイルを削除
kutimiti1234 31eca07
誤って作成したファイルを削除
kutimiti1234 fd88d65
変数名の変更と不要な処理を削除
kutimiti1234 1180241
定数の宣言を移動
kutimiti1234 5ce6290
冗長なメソッドを削除
kutimiti1234 0ef9086
ロジックを単純なものに修正
kutimiti1234 8ce47eb
不要な処理を削除
kutimiti1234 a90767c
変数名を変更
kutimiti1234 15c4248
変数名を修正
kutimiti1234 7928ac5
不必要な定数を削除
kutimiti1234 07f858b
削除した定数が誤っていたため、正しい定数に修正
kutimiti1234 f88ef39
ARGF.filenameを直接格納
kutimiti1234 152e4be
関数名,変数名をより説明的に変更
kutimiti1234 c313029
get_max_column_widthsの機能が直感的にわかるように設定
kutimiti1234 3f04331
2個ファイルを指定した際に集計業が出ないバグを修正
kutimiti1234 3572c72
冗長な条件分を簡潔に記述
kutimiti1234 8480673
変数名を説明的に変更
kutimiti1234 1d6ca7c
標準入力の例外的なfilenameに対する処理
kutimiti1234 7900ee3
input_textが抽象的するギルため具体的に。また、関数名を修正
kutimiti1234 955cd88
変数名と関数名をわかりやすく。ロジックも読みやすくなるように修正
kutimiti1234 a1770ba
clean:変数名とメソッド名をよりわかりやすく
kutimiti1234 40fdf93
clean:calculate_max_witdhsメソッドをDRYに
kutimiti1234 0d3f3c1
clean:ブロック内の変数をレシーバに合わせる
kutimiti1234 eee8b7e
clean:表示時のスペースの挿入をより分かりやすい形に変更
kutimiti1234 2a3f36b
clean:変数名をより一貫した形に変更。また、条件式をより見やすい形に変更。
kutimiti1234 8ecdc26
fix:オプション指定しない際に、-lwcが指定されるはずだが、バグが混入していたため修正
kutimiti1234 96f3e9f
clean:メソッドcalulate_max_widthsをより分かりやすい形に変更
kutimiti1234 18b16c4
clean:rubocop
kutimiti1234 ceb6a3d
clean:build_rowsメソッドに既存のmainのロジックを切り出し、メインメソッドの見通しを向上
kutimiti1234 a418071
clean:正規表現に置き換えることで条件分岐を削除
kutimiti1234 758644c
clean:メソッドの再利用性を向上
kutimiti1234 3bc64b5
clean:build_rowsのメソッドに集計行の追加処理を含むとまとまりがよくなると判断したため
kutimiti1234 775cc20
clean:メソッドの条件分岐を3こう演算子で見やすく。また、定数の名前を実態に合わせる
kutimiti1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||||||||||||||||||||
| # ! /usr/bin/env ruby | ||||||||||||||||||||||||||||||||||||
| # frozen_string_literal: true | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| require 'optparse' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| STDIN_PATTERN = /^-$/ | ||||||||||||||||||||||||||||||||||||
| TOTAL = '合計' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def main | ||||||||||||||||||||||||||||||||||||
| options = parse_options(ARGV) | ||||||||||||||||||||||||||||||||||||
| rows = build_rows(options) | ||||||||||||||||||||||||||||||||||||
| show_rows(rows) | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def build_rows(options) | ||||||||||||||||||||||||||||||||||||
| rows = [] | ||||||||||||||||||||||||||||||||||||
| ARGF.each(nil) do |input_text| | ||||||||||||||||||||||||||||||||||||
| counts = {} | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| counts[:filename] = ARGF.filename | ||||||||||||||||||||||||||||||||||||
| counts[:line] = input_text.lines.count if options[:l] | ||||||||||||||||||||||||||||||||||||
| counts[:word] = input_text.split(/\s+/).size if options[:w] | ||||||||||||||||||||||||||||||||||||
| counts[:byte] = input_text.size if options[:c] | ||||||||||||||||||||||||||||||||||||
| rows << counts | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+16
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
空の配列を用意してループで詰め込むだけの処理はmapで置き換えられます。(チェリー本参照) あとfilenameは最初から入れてしまうのもアリです |
||||||||||||||||||||||||||||||||||||
| rows << calculate_total_rows(rows) if rows.size > 1 | ||||||||||||||||||||||||||||||||||||
| rows | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def calculate_total_rows(rows) | ||||||||||||||||||||||||||||||||||||
| total = rows.inject({}) do |result, counts| | ||||||||||||||||||||||||||||||||||||
| result.merge(counts) do |key, current_val, adding_value| | ||||||||||||||||||||||||||||||||||||
| current_val + adding_value unless key == :filename | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
| total[:filename] = TOTAL | ||||||||||||||||||||||||||||||||||||
| total | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def show_rows(rows) | ||||||||||||||||||||||||||||||||||||
| max_widths = calculate_max_widths(rows) | ||||||||||||||||||||||||||||||||||||
| rows.each do |counts| | ||||||||||||||||||||||||||||||||||||
| columns = counts.to_h do |name, value| | ||||||||||||||||||||||||||||||||||||
| text = name == :filename ? value.sub(STDIN_PATTERN, '') : value.to_s.rjust(max_widths[name]) | ||||||||||||||||||||||||||||||||||||
| [name, text] | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 参考: eachの代わりにto_hメソッドを使う方法もあります。 cells = row.to_h do |column_name, cell|
value = if column_name == :filename
# ...
[column_name, value]
end |
||||||||||||||||||||||||||||||||||||
| puts columns.values_at(:line, :word, :byte, :filename).join(' ') | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def calculate_max_widths(rows) | ||||||||||||||||||||||||||||||||||||
| %i[line word byte].to_h do |name| | ||||||||||||||||||||||||||||||||||||
| max_width = rows.map { |counts| counts[name].to_s.length }.max | ||||||||||||||||||||||||||||||||||||
| [name, max_width] | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def parse_options(argv) | ||||||||||||||||||||||||||||||||||||
| options = {} | ||||||||||||||||||||||||||||||||||||
| OptionParser.new do |opt| | ||||||||||||||||||||||||||||||||||||
| opt.on('-l') { |v| options[:l] = v } | ||||||||||||||||||||||||||||||||||||
| opt.on('-w') { |v| options[:w] = v } | ||||||||||||||||||||||||||||||||||||
| opt.on('-c') { |v| options[:c] = v } | ||||||||||||||||||||||||||||||||||||
| opt.parse!(argv) | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| options.empty? ? { l: true, w: true, c: true } : options | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| main | ||||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rowsを作る処理もメソッド化するとmainメソッドの見通しが良くなりそうです。