Private attr methods in Ruby
You’re probably well aware that you can use the
attr_ family of methods to create getters and setters for class attributes. You may not know that you can actually make these private without having to write your own.
Here’s a simple class with one attribute to demonstrate.
Here, the API allows
@title to be read via the
title instance method, created by
@title may not be written to by the user of the
You can still use
#title= inside the
Post class as you’d expect though.