Private and Protected: They might not mean what you think they mean
Ruby, like many other languages, provides a built-in way to change method visibility. Used properly it can help create a roadmap for other developers to follow. The problem is that markers of the same name can vary in meaning between languages. If you’ve taken up Ruby after learning another language like C++ or Java and you haven’t reexamined the meaning of “private” and “protected” then you’re almost certainly not using them the right way. Let’s spend a minute investigating their use in Ruby.
In Ruby marking a method as
private means that it can’t have an explicit receiver. Below we define a
Person class that takes a first name, last name, and an age. People don’t usually mind telling you their name but some are squeamish about revealing their age.
Let’s create a new
Person and ask them their name.
What happens if we ask about their age?
We get an error because
age is called with the explicit receiver
john. We can still access
age we just have to do it inside of
Person and without an explicit receiver. Let’s explore this by equiping
Person with a
Now we can call
birthday! to update their age.
age is a local variable and it’s defaulting to
nil. When using a setter in Ruby we’re supposed to use an explicit receiver like
self. Here we have a private method that doesn’t allow an explicit receiver. We appear to have reached an impasse. In this case it turns out Ruby breaks its own rule.
Now we’ve got it.
+= which means that
self.age is being called as a getter and a setter. The setter needs an explicit receiver but the getter can’t have one.
One more time.
Hurrah! Let’s see the final class.
What if we want to inherit from
private methods will be available in the child class just like they are in the parent. Remember, Ruby’s only concern is how the method is called, not who’s receiving the call.
protected also concerns itself with the receiver. Methods marked with
protected can only be called using
self, an instance of the same class as
self, or an instance of a subclass of the class of
Who’s up for ping pong?
First we’ll need two players.
If you ask Jane how skilled she is you’re not going to get an answer.
Play her and you’ll quickly find out.
Rule of Thumb
If you’re looking to remove something from your public interface then you’re looking for
private. If you’re looking to narrow the interface to other objects of the same type then you want
All code was run in Ruby 1.9.3.
Aaron Lasseigne strives to design comprehensive solutions that provide structural stability and a positive customer experience. He is a graduate of The Ohio State University with a degree in Computer and Information Science. Before coming to OrgSync, Aaron worked as a Developer for Monster Worldwide in their FastWeb division and then as a Manager of Application Development at HRsmart Inc. Outside of work Aaron likes to travel, read, cook, and is an avid fan of Chicago Bears and Buckeye football.
comments powered by Disqus