Ruby : Public, Private, Protected simplified

Like many parts of Ruby that look like special language features, Ruby’s privacy key-
words are actually methods. Yes access modifiers are simple method calls, they don’t create a new scope. public, private and protected are really methods, so they can take parameters. If you pass a Symbol to one of them, that method’s visibility is altered.

In this case, they’re methods of Module . When you call private , protected , or public , the current module (remember that a class is just a special kind of module) changes the rules it applies to newly defined methods from
that point on.

Private and protected methods work a little differently in Ruby than in most other
programming languages. Suppose you have a class called Foo and a subclass SubFoo .
In languages like Java, SubFoo has no access to any private methods defined by Foo .
As seen in the Solution, Ruby provides no way to hide a class’s methods from its sub-
classes. In this way, Ruby’s private works like Java’s protected .

Suppose further that you have two instances of the Foo class, A and B. In languages
like Java, A and B can call each other’s private methods. In Ruby, you need to use a
protected method for that. This is the main difference between private and pro-
tected methods in Ruby. Continue reading

Programming : Atomic analogy of OOP Concept

Let us consider the Lewis Structure for the Water molecule. Lets define Lewis structure first – “Lewis Structures are visual representations of the bonds between atoms and illustrate the lone pairs of electrons in molecules.


Oxygen has 6 valence electrons and Hydrogen has 1; altogether water molecule has 8 valence electrons. If we pay attention and analyze individual atom, we see every atom has fulfilled it octet – Hydrogen has 2 electrons and Oxygen has 8 electrons(counting 2 it got from 2 Hydrogen atoms).

In chemistry, a valence electron is an electron that is associated with an atom, and that can participate in the formation of a chemical bond; in a single covalent bond, both atoms in the bond contribute one valence electron in order to form a shared pair.

My point

Consider the valence electrons as public methods and non-valence electrons as private/protected methods. OOP suggests to keep data at the core so that it is always private. So, consider private data as nucleus of the atom.

When two unstable atoms bond with each other to form a rather stable structure sharing their valence electrons, then yields more usable stable chemical structure. Now lets make analogy:

  • bonding                      –> message passing
  • unstable atoms            –> Individual software components (say classes) that need to cope with other entities to form a usable software.
  • valence electrons        –> Public methods (public API) that take part in assembly
  • lone pair of electrons –> Public methods that might not be used in that software at all.
  • core electrons               –> Private methods that do not take part in
  • Atom nucleus              –> Private data of object; never shared directly with other
  • Yielded usable stable chemical structure  –> A releasable software component

Atomic analogy of OOP


OOP : Why are there access modifier in OOP? Public/Protected/Private

In my opinion, all of them have different purposes to fulfill. I have tried to list them below. If you want to understand the concept of OOP then first go through this post “Atomic Analogy of OOP Concept“.


  • Primarily, it is to converge / decrease the dependencies of outer world to these data/method. It makes sure that the developers are not going to use these data/methods and are safe to be modified if deemed.
  • So, it makes sure that its fine to make some tweak in this portion of code. It confirms that its safe and its not gonna trouble the library users.
  • Also, since outer world has no direct connect strings whatsoever, therefore, I can freely change the behavior and be confident that only the same class is gonna be affected.
  • I might(not) have test coverage for this.
    Continue reading