Ruby quiz (intermediate) #2

question mark


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:


Got a comment?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: