Ruby - Invading the Privacy of Private Methods

Introduction

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

So, this won't work:

class X
    private
        def priv( aStr )
             puts("I'm private, " << aStr)
        end
end
ob = X.new
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