Never use instance variables directly. Only ever use accessors. You can define the reader as public and the writer private by:
class Foo
attr_reader :bar
private
attr_writer :bar
end
However, keep in mind that private
and protected
do not mean what you think they mean. Public methods can be called against any receiver: named, self, or implicit (x.baz
, self.baz
, or baz
). Protected methods may only be called with a receiver of self or implicitly (self.baz
, baz
). Private methods may only be called with an implicit receiver (baz
).
Long story short, you're approaching the problem from a non-Ruby point of view. Always use accessors instead of instance variables. Use public
/protected
/private
to document your intent, and assume consumers of your API are responsible adults.