3
3

More than 5 years have passed since last update.

特異メソッドからPrivateメソッドは呼べないけど同名のメソッド名を付けたい時

Posted at
class A
  def self.calls
    private_call
  end

  private

  def private_call
    p "get private!"
  end
end

A.calls
#=> NotForundLocalMethod (こんな感じのException)

こんな感じでエラーになるが、どうしてもcallsで呼び出したいときは、ほぼそのまま

class A
  def self.calls
    A.new.calls
  end

  def calls
    private_call
  end

  private

  def private_call
    p "get private!"
  end
end

A.calls
#=> get private!

これで呼べる

3
3
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
3
3