I always thought it was odd that a Ruby object can call another object’s protected methods if they are of the same class, mostly because I couldn’t think of a circumstance where this would be a desirable behavior. The example in the article of using this behavior while implementing equality operators is something I hadn’t thought of.
I always thought it was odd that a Ruby object can call another object’s protected methods if they are of the same class, mostly because I couldn’t think of a circumstance where this would be a desirable behavior. The example in the article of using this behavior while implementing equality operators is something I hadn’t thought of.
That’s pretty much the only use case I can think of. Even then, I implement equality operators so infrequently that I’ll forget!
It’s useful for any kind of comparison (
<
,>
,<=>
,==
,===
). I only use protected for methods that are used in comparison (most commonly, accessors).