Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all singleton methods public?

Is a singleton method necessarily public? If not, when would a private/protected singleton method be useful?

like image 855
sawa Avatar asked Mar 17 '23 05:03

sawa


1 Answers

Singleton methods do not necessarily need to be public. Private/protected singleton methods are useful in the same situations as regular private/protected methods - for example as a helper method that you do not intend to be called outside of the class.

class Foo
end

f = Foo.new

class << f
  def foo
    helper
    # other stuff
  end

  private
  def helper
  end
end
like image 80
Max Avatar answered Mar 19 '23 18:03

Max