Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 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.
Public

By addingprivate in your code,Ruby will read this and know to make all of the methods belowprivate

Private

By using theprivate method (“wrapper” syntax)

“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.
explicit arguments

You can make a alsoprivate methodpublic 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 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.
Coffee Shop Example

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:

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

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

With a diverse background I am comfortable in all aspects of business. My primary objectives are to stay with Software Engineering and continue to grow as a Web Developer.
  • Education
    Flatiron Fullstack Web Development
  • Joined

More fromAaronDski

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp