1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】クラス内要素の記述順序

Posted at

クラス内要素の記述順序

  • クラスメソッド
  • 初期化メソッド
  • パブリックなインスタンスメソッド
  • プライベートメソッド
class Sample
  # publicなクラスメソッドはその次にインライン形式で書く(class << selfでもよい: 後述)
  def self.some_method
  end

  # 初期化メソッドはクラスメソッドと他のインスタンスメソッドの間に書く
  def initialize
  end

  # publicなインスタンスメソッドはその後に書く
  def some_method
  end

  # protectedメソッドやprivateメソッドは末尾にまとめる
  protected

  def some_protected_method
  end

  private

  def some_private_method
  end
end
  • クラスメソッドが複数ある場合はclass << selfを使うほうが向いてる
class Sample
  class << self
    def first_method
    end

    def second_method
    end
  end
end

参考

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?