Skip to content

Conversation

@shibainurou
Copy link
Owner

lsコメントをクラス化しました。


view =
if options.list_mode
ListView.new(file_names)
Copy link

Choose a reason for hiding this comment

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

ここは .new(file_names) が重複しているので、クラス名の部分のみ分岐させるとコードがシンプルになりますね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど。クラス名を変数に入れる書き方はRebuilding Railsという本でも書かれていました。
klass = options.list_mode ? ListView : ColumnViewに書き直しました。


def display_file_info
@file_names.map do |f|
d = Struct::Display.new(f)
Copy link

Choose a reason for hiding this comment

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

ブロック引数などでスコープが1〜数行に限られている場合は1文字の変数でも意図がつかめるのですが、この規模のブロックとなると少しコードが読みづらいです。 ffile_named は display` に置き換えてはどうでしょうか?

Copy link
Owner Author

@shibainurou shibainurou May 7, 2025

Choose a reason for hiding this comment

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

確かにそうですね。特に"f"はすごく読みづらいです。
おすすめの修正方法 「f は file_name」 、 「d は display」に修正しました。
名称を合わせるためにStructもfilename -> file_nameに修正しました。

def row_max_lenght(display_file_infos)
max_len = Struct::MaxLenght.new(0, 0, 0, 0, 0, 0, 0, 0)
display_file_infos.each do |v|
max_len.filename = [max_len.filename, v.filename.length].max
Copy link

Choose a reason for hiding this comment

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

ここで行っていることは一定の規則性を持った繰り返し処理なので、イテレータを用いて重複をなくしたいですね。
Structを使っていることで少しイテレータを用いづらくなっているかもしれません。場合によってはStructではなくHashを用いることを検討しても良さそうです。

Copy link

Choose a reason for hiding this comment

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

順番が常に同じ、という意味では配列でもいいかもしれません。これは Struct::Display についても同様です。

Copy link
Owner Author

Choose a reason for hiding this comment

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

Structから配列に変更しました。
配列のindexをenumっぽいmoduleで実装しましたがenumが存在しないRubyでは少し強引な修正方法でしたでしょうか?

require_relative 'base_view'

class ColumnView < BaseView
COLUMNS = 3
Copy link

Choose a reason for hiding this comment

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

カラム数を ColumnView.new の引数で渡せるようにすると、より使いやすいクラスになりますね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

カラム数を ColumnView.new の引数で渡せるように修正しました。

print "#{file.timestamp.rjust(row_max_len.timestamp)} "
print file.filename
print " -> #{File.readlink(file.filename)}" if file.type == 'l'
print file[DisplayIndex::TYPE].rjust(row_max_len[DisplayIndex::TYPE])
Copy link

Choose a reason for hiding this comment

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

この使い方をするのであれば fileHash になるようにして

file[:type].rjust(row_max_len[:type])

のように書いた方がコードもスッキリすると思いました。
これであれば定数の定義も不要になりますね。いかがでしょう?

Copy link
Owner Author

@shibainurou shibainurou May 28, 2025

Choose a reason for hiding this comment

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

hashに修正しました!

そうですね。。。私もこの書き方をするならばhashの方が良さそうだなぁと思いながら書いてました。
先のコメントにあった配列での実装にする場合はどのような実装を想定していたのか教えていただけると嬉しいです。
私では配列にした場合の実装方法しか思い浮かばず。

Copy link

Choose a reason for hiding this comment

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

こちらのコメントの件ですよね?
#11 (comment)

これはこちらのコメントで提案したような実装をイメージしていました 👍 今回はHashで進めるとのことでしたのでこのままでもOKです。
#11 (comment)

@file_names.map do |file_name|
display = []
stat = File.lstat(file_name)
display[DisplayIndex::FILE_NAME] = file_name
Copy link

Choose a reason for hiding this comment

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

ここは次のように書けますね。

@file_names.map do |file_name|
  [
    file_name,
    FILE_TYPE_STR[stat.ftype],
    print_permission(stat.mode, PERMISSION_STR),
    # 以降省略
  ]
end

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに配列なのでそうすれば順番に詰め込まれるので左辺が省略できてシンプルです。インデックスの書き間違えが無いのもメリットに感じます。
今回はhashに変えたので以下のように書き換えました。

@file_names.map do |file_name|
  stat = File.lstat(file_name)
  display = {
    file_name: file_name,
    type: FILE_TYPE_STR[stat.ftype],
    # 以降省略
  }
  display
end

@@ -0,0 +1,155 @@
#!/usr/bin/env ruby
Copy link

Choose a reason for hiding this comment

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

このファイルはおそらく不要なファイルですよね?

Copy link
Owner Author

Choose a reason for hiding this comment

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

不要なファイルです 💦 削除しました。
lsテスト用ファイルは.gitignoreに書いて対象外としました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants