DEV Community

Cover image for Intro Into Private Methods
AaronDski
AaronDski

Posted on

Intro Into Private Methods

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 in Ruby. 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 need private 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 it private. 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 that private 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 in Ruby itself, but is actually a method that is already defined by the Ruby 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 classic Ruby method will start out as a public method (easily accessible) by default and can be made private very easily with these 3 different syntaxes, all of which accomplish the same result.
Public

By adding private in your code, Ruby will read this and know to make all of the methods below private

Private

By using the private method (“wrapper” syntax)

“wrapper” syntax

By using the private method with explicit arguments

Another variant of the syntax of private methods is when we can explicitly pass method names as arguments.
explicit arguments

You can make a also private method public again with this syntax

Made Public

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 a Ruby 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.
Coffee Shop Example

Let's say you want to just access choose_milk. It won't let you, and you will get a "NoMethodError," which would look a bit like this in IRB:

NoMethodError
You can still get the information that choose_milk provides, but you cannot break it up into just that one piece.
make_drink

See, you just need to go through the make_drink method and then the information that choose_milk provides is there.

There you have it! This is what I have learned about private methods, over the first week of coding in Ruby. I hope that you found something useful in this article. Thank you for reading.

Top comments (0)