What is a private method in Ruby?
It’s a type of method that you can ONLY call from inside the class where it’s defined.
This allows you to control access to your methods.
A Ruby method can be:
- private
- public (default)
- protected
By default ALL your methods are public
.
Anyone can use them!
But you can change this, by making a method private
or protected
.
Why is this useful?
Because you can make these methods easier to change.
Imagine you’re writing a code library that is going to be used in a few different projects at work.
When these projects implement your library, they’re going to be calling methods on the classes that you’ve created.
Now:
You release a new version…
But you decided to change the name on a public
method.
That’s going to produce errors on every project that is using this method!
Remember this:
By having LESS public methods you INCREASE the freedom of change inside your class.
Let’s see a few code examples!
Understanding Private Methods in Ruby
Have you ever seen the “private method called” error message?
This one:
self.puts 123 # NoMethodError: private method `puts' called
Then you have tried to use a private
method incorrectly.
You can only use a private method by itself.
Example:
puts 123
It’s the same method, but you have to call it like this.
Private methods are always called within the context of self
.
In other words…
You can only use private methods with:
- Other methods from the same class
- Methods inherited from the parent class
- Methods included from a module
This means you can’t call private methods from outside the class that defines them.
Because that would require an “explicit receiver”.
Unless…
You use a method like send
to bypass this rule.
send(:puts, "apple")
How do you define a private
method?
Like this:
def bacon "private bacon" end private :bacon
The word private
itself is not a keyword, it’s a method defined on the Kernel
module.
Where to Put Your Private Methods
It’s normal for a class to have more than one private method.
Where do you place these methods?
Do this:
class Food def public_method end private def bacon end def orange end def coconut end end
Every instance method after private
becomes a private method.
You can use
private_class_method :method_name
if you want to define a class method as private
It’s a common pattern to define all your public methods first, then define your private
methods together at the end of the class.
Public Methods
Public is the default method visibility in Ruby.
Here’s an example:
def orange "Vitamin C" end
If you have an object food
that defines orange
, you can call it like this:
food.orange
If a method has been made private or protected, you can make it public again.
Like this:
public :orange
What is A Protected Method?
Protected methods are less common.
They are like private
methods, but you can call them on an object & not just directly.
If you try this example with private you’ll get an error:
class Food def initialize(name) @name = name end def ==(other) name == other.name end protected attr_reader :name end food = Food.new("chocolate") puts food == food
You get the error because name
would be private so you can’t do other.name
.
But with protected
this code works!
Private vs Protected Methods
That’s the difference, the fact that protected
keeps the method private
, but it also allows you to call that method on an object.
With private you can only do name
, with protected you can do object.name
.
When should you use protected?
Only if you have a very specific case, like the equals (==
) method.
The Ruby documentation recommends using private
instead of protected
whenever possible.
And there’s this note:
“A protected method is slow because it can’t use inline cache.”
I was curious about this so I ran some benchmarks:
public: 2813891.9 i/s private: 2699273.8 i/s protected: 2572122.0 i/s
That’s a difference of 8.5% in performance.
Video Tutorial
[responsive_video type=’youtube’ hide_related=’0′ hide_logo=’0′ hide_controls=’0′ hide_title=’1′ hide_fullscreen=’0′ autoplay=’0′]https://www.youtube.com/watch?v=PX2nYhKlVv4[/responsive_video]
Summary
You’ve learned about Ruby method visibility, public, private & protected methods. These aren’t Ruby keywords, they are methods themselves defined on the Module
class.
Please share this post so more people can understand this topic!
Thanks for reading 🙂
I think you made a typo here:
“The Ruby documentation recommends using private instead of private whenever possible.”
You probably thought “instead of protected”.
Nice article btw, keep up the good work!
You’re correct! Thank you, it should be fixed now 🙂