Sometimes you want to test a private method. You can make your method public and ignore the principle of encapsulation, or use this nice trick.
As expected, a private method cannot be accessed.
Wait a second. How does that work? Ruby allows you to define methods for an object, a so called singleton method. After instantiating an object, you can define a method for it. Within the singleton method, self is the instantiated object, so you can access its private methods.
Mystery solved.
No comments:
Post a Comment