Intro Into Private Methods
Tags: beginners, ruby, codenewbie
Published: 2021-08-13 14:16:12

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](https://www.rubyguides.com/2019/02/ruby-class/)
[Ruby Methods](https://www.w3resource.com/ruby/ruby-methods.php)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kt2fyum7zqc4ys5re8tx.png)
####By adding `private` in your code, *Ruby* will read this and know to make all of the methods below `private`
![Private](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4dqx5q7zu8ub91fal9l.png)
####By using the `private` method (“wrapper” syntax)
![“wrapper” syntax](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzbwku2oac6hqo1dc3bj.png)

####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlqrn4t6gjs8lwjz6za5.png)
#####You can make a also `private` method `public` again with this syntax
![Made Public](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhudc32v725trnwpt5td.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbloqkzqcg4kyg87ts00.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdehq4cizsj00169e7ya.png)
You can still get the information that `choose_milk` provides, but you cannot break it up into just that one piece.
![make_drink](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdwb8v0r4aicb38eqzng.png)

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.