forked from fjordllc/ruby-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
WCコマンド作成 #9
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
jepe-w
wants to merge
13
commits into
main
Choose a base branch
from
feature/wc-command
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コマンド作成 #9
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9f7bd50
wcコマンドの作成
jepe-w 1d7bd40
前回コミットから特に変更なし -m 改善の余地がある気はとてもするが、どう改善すべきかわからず断念
jepe-w 5abe3bf
result_of_optionsメソッドのさくせい。番号指定パラメータを削除。ハッシュの変数を複数形に修正。破壊的メソッドを削除。無味無…
jepe-w 32af58f
変数の調整。obtained_option_valueメソッドの修正
jepe-w 2ee7570
require 'debug'の削除
jepe-w 4b7b832
処理を小分けにしてメソッド化
jepe-w f763389
変数調整
jepe-w fcc03cd
変数調整
jepe-w 68ede5f
メソッドの順番を調整。変数名を調整。全体的にリファクタリング
jepe-w 0b7f2ee
display_valuesメソッドの改善。optionをハッシュのまま使用する方法に変更
jepe-w f1ccf04
optionsの値をすべてfalseだった場合にtrueへするように修正。標準入力で受け取った値の変数への格納方法を修正。二人三脚コード(…
jepe-w 0e94747
無駄、冗長コードの修正。メソッド名の変更
jepe-w 04125bb
calc_column_widthメソッドの修正。その他指摘箇所修正
jepe-w 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,74 @@ | ||||||
| #!/usr/bin/env ruby | ||||||
| # frozen_string_literal: true | ||||||
|
|
||||||
| require 'optparse' | ||||||
|
|
||||||
| def main | ||||||
| options = ARGV.getopts('lwc') | ||||||
| options.transform_values!(&:!) if options.values.none? | ||||||
| filenames = ARGV | ||||||
|
|
||||||
| if filenames.any? | ||||||
| handle_multiple_files(filenames, options) | ||||||
| else | ||||||
| text = readlines.join | ||||||
| handle_standard_input(text, options) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| def handle_standard_input(text, options) | ||||||
| detail_hashes = create_detail_hash(text) | ||||||
| display_values([detail_hashes], options) | ||||||
| end | ||||||
|
|
||||||
| def handle_multiple_files(files, options) | ||||||
| detail_hashes = files.map do |file| | ||||||
| text = File.read(file) | ||||||
| detail = create_detail_hash(text) | ||||||
| detail['name'] = file | ||||||
| detail | ||||||
| end | ||||||
| if files.length >= 2 | ||||||
| total = { 'name' => 'total' } | ||||||
| options.each_key do |option| | ||||||
| total[option] = detail_hashes.sum { |hash| hash[option] } | ||||||
| end | ||||||
| detail_hashes << total | ||||||
| end | ||||||
| width = calc_column_width(detail_hashes, options) | ||||||
| display_values(detail_hashes, options, width) | ||||||
| end | ||||||
|
|
||||||
| def calc_column_width(detail_hashes, options) | ||||||
| options_count = options.count do |_key, value| | ||||||
| value == true | ||||||
| end | ||||||
|
|
||||||
| if detail_hashes.length <= 1 && options_count <= 1 | ||||||
| 0 | ||||||
| else | ||||||
jepe-w marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| detail_hashes[-1]['c'].to_s.length | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| def create_detail_hash(text, filename = '') | ||||||
| { | ||||||
| 'name' => filename, | ||||||
| 'c' => text.bytesize, | ||||||
| 'l' => text.lines.length, | ||||||
| 'w' => text.split.count | ||||||
| } | ||||||
| end | ||||||
|
|
||||||
| def display_values(file_details, options, width = 0) | ||||||
| file_details.each do |file_detail| | ||||||
| columns = [] | ||||||
| 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'] != '' | ||||||
|
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
と書くのもありです(英語っぽく読める) |
||||||
| puts columns.join(' ') | ||||||
| end | ||||||
| end | ||||||
jepe-w marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| 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.
vが真のカウントを取得すればいいので、
== trueはなくせるのと、これぐらいなら1行で書いても問題ないと思います