Triangles are my favorite shape. Three points where two lines meet.


Toe to toe, back to back, let's go, my love, it's very late.

CLASS VISIBILITY AND THE THREE 'P's.

LITTLE P, LITTLE P, LET ME IN.

Flavour of the week this week was Ruby classes. Classes provide an excellent way of organizing your code and are like a blueprint for some specific object. They contain instruction guides (class methods) for defining the behaviour and function of the Ruby class object they sit in, as well as what I think of as "class children" (derived from the parent blueprint class) - what is referred to in Ruby as "instances of the class".

Ruby has a couple of cool tricks in restricting or shutting off access to these class methods which means they can't be called or used in other areas of your code. This is what Ruby calls "class visibility" - a toolbelt of three different methods (belonging to module) known as private, protected and public. The first two are essentially limited zones that are robust and don't let any old tom, dick or harry in. They are the brick house to extend the metaphor of the three little pigs (but with a sealed chimney - nothing is getting in there). They dynamically alter the visibility of your class methods, and as a result influence the visibility of all following declarations until a new visibility is set or the end of the declaration-body is reached.

Let's take a deeper look.

PUBLIC

PRIVATE

PROTECTED

VISIBILITY vs SECURITY

It's easy to think of visibility as a type of security. That you can lock the internal state of your objects away and that it can't be accessed because private methods are barring the way. This just isn't true. Private methods exist to separate interface from function. There's nothing really stopping a user from accessing private methods, should they really want to.


(In the words of Sesame Street) This blog was brought to you by the letter p.



READ MORE | Blog 1 | Blog 2 | Blog 3 | Blog 4 | Blog 5 | Blog 6 | Blog 7 | Blog 8 | Blog 9 | Blog 10 | Blog 11 | Blog 12 | Blog 13