クラスメソッドしかないクラスよりモジュールを使う

2025/08/01

Ruby Style Guideで推奨されている。

クラスはインスタンスを生成することに意味がある時にのみ使われるべき、

# 悪い例
class SomeClass
  def self.some_method
    # body omitted
  end

  def self.some_other_method
    # body omitted
  end
end

# 良い例
module SomeModule
  module_function

  def some_method
    # body omitted
  end

  def some_other_method
    # body omitted
  end
end

関連ページ

クラス、モジュール、トップレベル関数の使い分け

参考