
The goal of this post is to shed some light on the topic of "Private Methods." This is a very basic overview on said topic, but I hope my findings will be enlightening, at least to some extent, for whomever views them.Ruby Classes
Ruby Methods
If you are reading this, then hopefully you already know what Classes and Methods are inRuby. If not, I recommend reading up on those topics before moving forward.
Public Vs Private
Private
methods are really only to be used in the Class in which they reside. A good way for me to know when I would needprivate
methods is this: if I have a set of methods that do a specific task, and I don't want to alter that set or make it available outside the confines of this particular Class, then I would make itprivate
. I still have access to the info that those methods give me, but now anyone who reads my code would know that this distinct set of codes are designed to work together to perform a specific function.
I like to think thatprivate
methods are more "limited access" rather than "private" or "restricted," which just force you to follow proper channels, in order to get the information that the method provides.Private
is not a keyword inRuby itself, but is actually a method that is already defined by theRuby language.Private
methods are only accessible by other methods from the same class, methods inherited from the parent class, and methods included from a module.
A classicRuby method will start out as apublic
method (easily accessible) by default and can be madeprivate
very easily with these 3 different syntaxes, all of which accomplish the same result.
By addingprivate
in your code,Ruby will read this and know to make all of the methods belowprivate
By using theprivate
method (“wrapper” syntax)
By using theprivate
method with explicit arguments
Another variant of the syntax ofprivate
methods is when we can explicitly pass method names as arguments.
You can make a alsoprivate
methodpublic
again with this syntax
Here's a metaphor that might help to better understand private methods:
Imagine you go to your local coffee shop (think of this coffee shop as aRuby Class). The seat you choose, what you order, and waiting in line to place that order are all "public methods." Then your order is placed and the barista starts making your latte. From this point, you can think of the making of your latte as a "private method." You told the barista what you want, and now it's up to the barista, and you don't get a say in how the order is completed, it is simply made for you.
Let's say you want to just accesschoose_milk
. It won't let you, and you will get a "NoMethodError," which would look a bit like this in IRB:
You can still get the information thatchoose_milk
provides, but you cannot break it up into just that one piece.
See, you just need to go through themake_drink
method and then the information thatchoose_milk
provides is there.
There you have it! This is what I have learned aboutprivate
methods, over the first week of coding inRuby. I hope that you found something useful in this article. Thank you for reading.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse