Public method vs Private method in Ruby
In the world of Ruby, all the methods are either public method or private/protected method. Protected methods act pretty much the same with private methods other than the fact that it can be called in subclasses of the class where it is defined.
Get Kailey(Kyeonga) Lee’s stories in your inbox
Join Medium for free to get updates from this writer.
Here is a table showing the differences between public methods and private methods:
Let’s see an example of class!
class Dog attr_accessor :name, :breed, :gender @@name = ["Littles", "Ada", "Katie", "Rex", "Rukie"]
@@breed = ["chihuahua", "pug", "bulldog", "beagle", "pomeranian"]
@@gender = ["male", "female"] def favorite
puts "I am everyone's favorite dog!"
my_name
my_breed
my_gender
end private
def my_name
@name = @@name.sample
puts "My name is #{@name}!"
end
def my_breed
@breed = @@breed.sample
puts "My breed is #{@breed}."
end def my_gender
@gender = @@gender.sample
if @gender == "male"
puts "I am a boy!"
else
puts "I am a girl!"
end
endend
- This Dog class has one public method: favorite
- This Dog class has three private methods: my_name, my_breed, my_gender
- These three private methods are called in the context of class, called in the method favorite which is located in the scope of class
new_dog = Dog.new
new_dog.favorite
=> I am everyone's favorite dog!
My name is Littles!
My breed is bulldog.
I am a girl!- new_dog is a new instance of Dog class and favorite method was able to be called on new_dog since favorite is a public method
- new_dog is an explicit receiver of the favorite method
new_dog = Dog.new
new_dog.my_breed
=> in `<main>': private method `my_breed' called for
#<Dog:0x00007fe67b853b58> (NoMethodError)- my_breed method was not able to be called on the instance new_dog since it is a private method. It could not be called on explicit receiver, also outside the scope of class
When can private methods be useful?
- When to limit the scope of method within a class: excluding to call the method from outside class
- When the method should be only able to function with help of other methods in class
- When to increase the flexibility of codes in class
How can we make methods to private methods?
- By placing the methods in the ‘private’ area such as an example of the Dog class above
- By declaring the methods with ‘private’ command
class Dog attr_accessor :name, :breed, :gender @@name = ["Littles", "Ada", "Katie", "Rex", "Rukie"]
@@breed = ["chihuahua", "pug", "bulldog", "beagle", "pomeranian"]
@@gender = ["male", "female"] def favorite
puts "I am everyone's favorite dog!"
my_name
my_breed
my_gender
end def my_name
@name = @@name.sample
puts "My name is #{@name}!"
end
def my_breed
@breed = @@breed.sample
puts "My breed is #{@breed}."
end def my_gender
@gender = @@gender.sample
if @gender == "male"
puts "I am a boy!"
else
puts "I am a girl!"
end
end private :my_name, :my_breed, :my_genderend
3. Exception — initialize method
: although it is not specified, initialize method of class is always private method
new_dog = Dog.new
new_dog.initialize
=> in `<main>': private method `initialize' called for
#<Dog:0x00007f869284f9e8> (NoMethodError)








