Search code examples
ruby-on-railsrubyaccess-specifier

When should we consider using private or protected?


Just wondering, when should we actually must use private or protected for some methods in the model?

Sometimes I can't not be bothered to group my methods in private nor protected. I just leave it as it is. But I know it must be a bad practice, otherwise these 2 groupings won't be created in programming.

Thanks.


Solution

    • If you plan to call a method externally, record.method(), then "public"
    • If it will be used only internally, self.method(), then "private"
    • If you plan to use it internally, but also in descendants, self.method() # in subclass, then "protected"