Ruby - Invading the Privacy of Private Methods


A private method cannot be called from outside the scope of the object to which it belongs.

So, this won't work:

class X
        def priv( aStr )
             puts("I'm private, " << aStr)
ob =
ob.priv( "hello" )      # This fails

Ruby has a method called send for you to call private method.

The send method invokes the method whose name matches that of a symbol, which is passed as the first argument to send like this:

ob.send( :priv, "hello" )    # This succeeds

Related Topic