0
0

More than 3 years have passed since last update.

Ruby CLIにて、実行部をクラス内でコードする

Last updated at Posted at 2020-11-25

ロジックをクラス内で実装し、処理はメインメソッドにまとめたいときに使用した書き方です。
Rubyで使い捨てCLIを作成したときにお世話になりました。

class SomethingTool
  def run
  end
end

if __FILE__ == $0
  something_tool = SomethingTool.new
  something_tool.run
end

if __FILE__ == $0は、明示的に「ここがメインの処理」と示すために使ってます。
詳しい説明は下記の記事を参考にして下さい。
[Ruby] if FILE == $0 ってなんなの!?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0