What is the difference between the
private access modifier in Java versus Ruby?
In Ruby, you can only call a private method from within the class in which it is defined. The receiver of a private method needs to be the class in which that private method was defined.
In other words:
If a method is private, it may be called only within the context of the calling object – it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller. – Programming Ruby, 2nd edition, p. 35
This means that you can do that in Java:
But the (arguably contrived) equivalent in Ruby does not work:
And this does not work either: