0x15 – public privacy protection: ruby privacy flags explained

…or: There is a difference between methodname(args) and self.methodname(args)!

The way how ruby deals with private, protected and public messages was a bit unclear to me. So I decided to dig a bit into it and to find out how it is really working. Finally I have irb installed: so lets go and explore.

This pastie shows the source code that I used for that, and here are the results.

Now, what does it mean?

  1. Public methods can (unsurprisingly) be called from anywhere
  2. Protected methods on an object can be called from that object and from other objects of the same or an derived class
  3. Private methods cannot be called via <obj>.<methodname>, not even via self.<methodname>! Consequently they can only be called from the very same object, which for example rules out private attributes.

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s