Skip to content

Lsコマンドオブジェクト指向版 初回提出#11

Open
Miya096jp wants to merge 24 commits intomainfrom
my_ls_object
Open

Lsコマンドオブジェクト指向版 初回提出#11
Miya096jp wants to merge 24 commits intomainfrom
my_ls_object

Conversation

@Miya096jp
Copy link
Copy Markdown
Owner

よろしくお願いいたします。

テストとテスト用のfixturesも作成しています。

Copy link
Copy Markdown

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

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

コメントしました!

@@ -0,0 +1,59 @@
# frozen_string_literal: true

class LsLong < LsFormatter
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • 特に同じインターフェースを共有したりするのでなければ、継承を使わなくてもよいのではと思います。
    • インターフェースを共有する場合、
       def some_method
          raise "Not Implemented Yet!"
       end
      のようにエラーをraiseするだけのメソッドを親クラスに宣言しておいて、実装漏れを減らす方法があります。
  • たとえば LongFormatter とするなど、クラス名単体で役割がわかるほうがわかりやすいかなと思います。

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.

自分でも必要ないかな〜と迷いながら書いていました。インターフェースの共有を一つの指針にして継承の必要性を考えてみます。~Formatterにクラス名を変更しました。こちらのほうがわかりやすいですね。

Comment on lines +14 to +23

FILETYPE = {
'1' => 'p',
'2' => 'c',
'4' => 'd',
'6' => 'b',
'10' => '-',
'12' => 'l',
'14' => 's'
}.freeze
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

このあたり、long formatのときだけしか使わないと思うので、そちらのクラスに移動したほうがよいかなと思います。クラス内にも定数を宣言でき、場合によってはprivateとすることも可能です。

https://docs.ruby-lang.org/ja/latest/method/Module/i/private_constant.html

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.

承知しました、そうやって整理していくんですね。移動してprivateにしました。

def run
paths = parse_paths(@pathname, @options)
entries = parse_entries(paths)
ls = select_formatter(entries, @options)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

formatterという変数名のほうが直感的に思います。
https://bootcamp.fjord.jp/pages/readable-variable-name

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.

formatterに変更しました。以前読んだ記事ですが無意識にズレていました😵 改めてしっかり読んでまとめました、ありがとうございます。

# frozen_string_literal: true

class LsLong < LsFormatter
def parse
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

parse というのはおおくの場合、何かしらの文字列を入力とし、何かの構造を出力とする場合が多いです。たとえばプログラミング言語のparserはプログラムのソースコードを入力として、ASTと呼ばれるツリー構造のプログラムの構造を出力とする場合が多いです。
今回はそのようなものではないと思うので、名前に違和感があります。

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.

そのような意味があるんですね、よく目にしていたので深く考えずに使っていました。理解できたと思います、ありがとうございます。format_outputに変更してみました。


require 'etc'

class Entry
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ちょっと名前が抽象的すぎるかなと思います。 FileStat のラッパーのようになっていると思うので、 FileInfo とか FileMetadata とかファイルの情報をあらわしているよ、というのがわかるようにしましょう。

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.

承知しました。FileMetadataに変更しました。

@options = options
end

def parse
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここもparseではないように思います。
また、そもそも initialize 時点でここまでやってもよいのかなと思います。オブジェクト指向において、オブジェクトの構成要素として大事なのはオブジェクトの状態/データとロジックです。そのオブジェクトがあらわしているモノやコトが内部的にもっている状態やデータは何でしょうか?
Paths となっているので、何らかのパス(複数)を持つのが自然なように思います。

このままだとあまりこのクラスの存在意義がなく、ただメソッドを別ファイルにわけただけ、に近いように思います。

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.

クラスの存在理由はPathsという情報を持つことなのに、外部からparseメソッドを呼んでわざわざPathsを生成するのであれば、ただ処理を分けただけになりますね💡 理解が深まりました、ありがとうございます!

Copy link
Copy Markdown

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

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

おおむねよさそうです。細かいところだけコメントしました。

class Paths
attr_reader :paths

def initialize(pathname, options)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

修正する必要まではないと思いますが、ここでは dot_match?reverse? のオプションしか使ってないので、明示的にその2つを渡してもよいと思います。
関係ないものも含んだ構造に依存してしまうことをスタンプ結合とよびます。興味あれば調べてみてください。

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.

オブジェクトをそのまま渡した方が効率的に思えたのですが、それも依存のうちなんですね。依存について感度を高めたいので修正してみました。スタンプ結合についても調べてみました。いろいろな結合の分類があることを初めて知りました。奥が深いです🤔

Comment on lines +94 to +98
if file_metadata.setuid?
[SUID_SGID[user], REGULAR_MODE[group], REGULAR_MODE[others]]
elsif file_metadata.setgid?
[REGULAR_MODE[user], SUID_SGID[group], REGULAR_MODE[others]]
elsif file_metadata.sticky?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

オブジェクト指向関係ない指摘になってしまって申し訳ないのですが、SUIDとSGIDは両方ONになることがあるので、たとえば、

-rwsr-sr-x 1 yoshitsugu yoshitsugu  0  2月  9  2023 test8.txt

みたいな状況もあり得ます。
スティッキービットも同様です。それぞれ独立して計算できるようにしたほうがよいです。

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.

同時に設定はできないものだと思い込んでいました。仕様のリサーチをしっかりやらなければと思います。修正いたしました。

Copy link
Copy Markdown

@yoshitsugu yoshitsugu left a comment

Choose a reason for hiding this comment

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

OKです!

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.

2 participants