Tuesday, September 30, 2008

Ruby Private Methods

I have started learning ruby very recently and came across a very interesting visibility issue. We can never invoke private methods with an explicit receiver. For e.g.


In the above example the call to any_method would fail because we are trying to invoke any_private_method using a receiver. We cannot do that even if the receiver is self.

However following code would work.

In the above example we are invoking the private method without any receiver from within the object and hence it works.
Have some Fun!