Before Ruby 2.7
Before Ruby 2.7, calling a private write/assignment method with literal self
as the receiver was allowed, but calling any other private method with self
would throw a NoMethodError
error.
As shown above, calling writer method self.count = 0
does not raise an error.
But, calling reader method self.count
raises an error.
This is inconsistent behavior.
A more practical example would be something as below.
Initializing the Counter
object works fine, but when we try to increment the counter, we get an error.
To understand why we get the error, let’s expand the code within Counter#increment
;
self.count += 1
is equivalent to self.count = self.count + 1
. self.count
is not legal.
After Ruby 2.7
Ruby 2.7 aims at standardizing the interaction between self
and private
methods.
The above inconsistency has been
fixed
in Ruby 2.7.
If we execute the above example in Ruby 2.7, then we won’t get any error.
This feature was requested and discussed on RubyLang at Feature #16123 and Feature #11297