Friday, August 20, 2010

Why does Ruby have both private and protected methods?

Programmer Question

Before I read this article, I thought access control in Ruby worked like this:




  • public - can be accessed by any object (e.g. Obj.new.public_method)

  • protected - can only be accessed from within the object itself, as well as any subclasses

  • private - same as protected, but the method doesn't exist in subclasses



However, it appears that protected and private act the same, except for the fact that you can't call private methods with an explicit receiver (i.e. self.protected_method works, but self.private_method doesn't).



What's the point of this? When is there a scenario when you wouldn't want your method called with an explicit receiver?



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails