Before I read this article, I thought access control in Ruby worked like this:
public - can be accessed by any object (e.g. Obj.new.public_method)
protected - can only be accessed from within the object itself, as well as any subclasses
private - same as protected, but the method doesn't exist in subclasses
However, it appears that protected and private act the same, except for the fact that you can't call private methods with an explicit receiver (i.e. self.protected_method works, but self.private_method doesn't).
What's the point of this? When is there a scenario when you wouldn't want your method called with an explicit receiver?
protected methods can be called by any instance of the defining class or its subclasses.
private methods can be called only from within the calling object. You cannot access another instance's private methods directly.
Here is a quick practical example:
def compare_to(x)
self.some_method <=> x.some_method
end
some_method cannot be private here. It must be protected because you need it to support explicit receivers. Your typical internal helper methods can usually be private since they never need to be called like this.
It is important to note that this is different from the way Java or C++ works. private in Ruby is similar to protected in Java/C++ in that subclasses have access to the method. In Ruby, there is no way to restrict access to a method from its subclasses like you can with private in Java.
Visibility in Ruby is largely a "recommendation" anyways since you can always gain access to a method using send:
irb(main):001:0> class A
irb(main):002:1> private
irb(main):003:1> def not_so_private_method
irb(main):004:2> puts "Hello World"
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> foo = A.new
=> #<A:0x31688f>
irb(main):009:0> foo.send :not_so_private_method
Hello World
=> nil
The difference
Anyone can call your public methods.
You can call your protected methods, or another member of your class (or a descendant class) can call your protected methods from the outside. Nobody else can.
Only you can call your private methods, because they can only be called with an implicit receiver of self. Even you cannot call self.some_private_method; you must call private_method with self implied.
iGEL points out: "There is one exception, however. If you have a private method age=, you can (and have to) call it with self to separate it from local variables."
Since Ruby 2.7 the self receiver can be explicit, self.some_private_method is allowed. (Any other explicit receiver is still disallowed, even if the runtime value is the same as self.)
In Ruby, these distinctions are just advice from one programmer to another. Non-public methods are a way of saying "I reserve the right to change this; don't depend on it." But you still get the sharp scissors of send and can call any method you like.
A brief tutorial
# dwarf.rb
class Dwarf
include Comparable
def initialize(name, age, beard_strength)
#name = name
#age = age
#beard_strength = beard_strength
end
attr_reader :name, :age, :beard_strength
public :name
private :age
protected :beard_strength
# Comparable module will use this comparison method for >, <, ==, etc.
def <=>(other_dwarf)
# One dwarf is allowed to call this method on another
beard_strength <=> other_dwarf.beard_strength
end
def greet
"Lo, I am #{name}, and have mined these #{age} years.\
My beard is #{beard_strength} strong!"
end
def blurt
# Not allowed to do this: private methods can't have an explicit receiver
"My age is #{self.age}!"
end
end
require 'irb'; IRB.start
Then you can run ruby dwarf.rb and do this:
gloin = Dwarf.new('Gloin', 253, 7)
gimli = Dwarf.new('Gimli', 62, 9)
gloin > gimli # false
gimli > gloin # true
gimli.name # 'Gimli'
gimli.age # NoMethodError: private method `age'
called for #<Dwarf:0x007ff552140128>
gimli.beard_strength # NoMethodError: protected method `beard_strength'
called for #<Dwarf:0x007ff552140128>
gimli.greet # "Lo, I am Gimli, and have mined these 62 years.\
My beard is 9 strong!"
gimli.blurt # private method `age' called for #<Dwarf:0x007ff552140128>
Private methods in Ruby:
If a method is private in Ruby, then it cannot be called by an explicit receiver (object). It can only be call implicitly. It can be called implicitly by the class in which it has been described in as well as by the subclasses of this class.
The following examples will illustrate it better:
1) A Animal class with private method class_name
class Animal
def intro_animal
class_name
end
private
def class_name
"I am a #{self.class}"
end
end
In this case:
n = Animal.new
n.intro_animal #=>I am a Animal
n.class_name #=>error: private method `class_name' called
2) A subclass of Animal called Amphibian:
class Amphibian < Animal
def intro_amphibian
class_name
end
end
In this case:
n= Amphibian.new
n.intro_amphibian #=>I am a Amphibian
n.class_name #=>error: private method `class_name' called
As you can see, private methods can be called only implicitly. They cannot be called by explicit receivers. For the same reason, private methods cannot be called outside the hierarchy of the defining class.
Protected Methods in Ruby:
If a method is protected in Ruby, then it can be called implicitly by both the defining class and its subclasses. Additionally they can also be called by an explicit receiver as long as the receiver is self or of same class as that of self:
1) A Animal class with protected method protect_me
class Animal
def animal_call
protect_me
end
protected
def protect_me
p "protect_me called from #{self.class}"
end
end
In this case:
n= Animal.new
n.animal_call #=> protect_me called from Animal
n.protect_me #=>error: protected method `protect_me' called
2) A mammal class which is inherited from animal class
class Mammal < Animal
def mammal_call
protect_me
end
end
In this case
n= Mammal.new
n.mammal_call #=> protect_me called from Mammal
3) A amphibian class inherited from Animal class (same as mammal class)
class Amphibian < Animal
def amphi_call
Mammal.new.protect_me #Receiver same as self
self.protect_me #Receiver is self
end
end
In this case
n= Amphibian.new
n.amphi_call #=> protect_me called from Mammal
#=> protect_me called from Amphibian
4) A class called Tree
class Tree
def tree_call
Mammal.new.protect_me #Receiver is not same as self
end
end
In this case:
n= Tree.new
n.tree_call #=>error: protected method `protect_me' called for #<Mammal:0x13410c0>
Consider a private method in Java. It can be called from within the same class, of course, but it can also be called by another instance of that same class:
public class Foo {
private void myPrivateMethod() {
//stuff
}
private void anotherMethod() {
myPrivateMethod(); //calls on self, no explicit receiver
Foo foo = new Foo();
foo.myPrivateMethod(); //this works
}
}
So -- if the caller is a different instance of my same class -- my private method is actually accessible from the "outside", so to speak. This actually makes it seem not all that private.
In Ruby, on the other hand, a private method really is meant to be private only to the current instance. This is what removing the option of an explicit receiver provides.
On the other hand, I should certainly point out that it's pretty common in the Ruby community to not use these visibility controls at all, given that Ruby gives you ways to get around them anyway. Unlike in the Java world, the tendency is to make everything accessible and trust other developers not to screw things up.
What's the difference?
Private Methods Explained
#freddie = Person.new
#freddie.hows_it_going?
# => "oh dear, i'm in great pain!"
class Person
# public method
def hows_it_going?
how_are_your_underpants_feeling?
end
private
def how_are_your_underpants_feeling? # private method
puts "oh dear, i'm in great pain!"
end
end
We can ask Freddie how things are going given it's a public method. That's perfectly valid. And it's normal and accepted.
But...the only person who can know how Freddie's underpants situation is, is Freddie himself. It would not do for random strangers to reach into Freddy's underpants - no, no -- it's very, very private, and we don't want to expose what's private to the outside world. In other words, we may not want to expose mutable data, to any caller in the world. Someone could mutate a value, and we'd be in a world of pain trying to discover where the bug came from.
#freddie.how_are_your_underpants_feeling?
# => # NoMethodError: private method `how_are_your_underpants_feeling?' called
Protected Methods Explained
Consider this:
class Person
protected
def hand_over_the_credit_card! # protected method
puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"
end
end
class Rib < Person
end
class Wife < Rib # wife inherits from Rib
def i_am_buying_another_handbag_with_your_card(husband)
husband.hand_over_the_credit_card! # equalityInAction
end
end
#husband = Person.new
#mrs = Wife.new
#mrs.i_am_buying_another_handbag_with_your_card(#husband)
# => puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"
We're somewhat ok with mrs getting our credit card details, given mrs is flesh of our flesh, inherited from Person, so she can blow it on some shoes etc., but we don't want random individuals getting access to our credit card details.
If we tried to do that outside the subclass, it would fail:
#mrs = Wife.new
#mrs.gimme_your_credit_card!
# => protected method hand_over_the_credit_card! called for #<Wife:0x00005567b5865818> (NoMethodError)
Summary
private methods can only be called from within, and without "an explicit receiver". (Strictly speaking, you can access a private method using a little bit of ruby magic, but I'm going to ignore that for the moment).
protected methods can be called within sub-classes.
I used the examples/analogies to help you vividly remember.
Part of the reason why private methods can be accessed by subclasses in Ruby is that Ruby inheritance with classes is thin sugarcoating over Module includes - in Ruby, a class, in fact, is a kind of module that provides inheritance, etc.
http://ruby-doc.org/core-2.0.0/Class.html
What this means is that basically a subclass "includes" the parent class so that effectively the parent class's functions, including private functions, are defined in the subclass as well.
In other programming languages, calling a method involves bubbling the method name up a parent class hierarchy and finding the first parent class that responds to the method. By contrast, in Ruby, while the parent class hierarchy is still there, the parent class's methods are directly included into the list of methods of the subclass has defined.
Comparison of access controls of Java against Ruby: If method is declared private in Java, it can only be accessed by other methods within the same class. If a method is declared protected it can be accessed by other classes which exist within the same package as well as by subclasses of the class in a different package. When a method is public it is visible to everyone. In Java, access control visibility concept depends on where these classes lie's in the inheritance/package hierarchy.
Whereas in Ruby, the inheritance hierarchy or the package/module don't fit. It's all about which object is the receiver of a method.
For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver.
This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.
class Test1
def main_method
method_private
end
private
def method_private
puts "Inside methodPrivate for #{self.class}"
end
end
class Test2 < Test1
def main_method
method_private
end
end
Test1.new.main_method
Test2.new.main_method
Inside methodPrivate for Test1
Inside methodPrivate for Test2
class Test3 < Test1
def main_method
self.method_private #We were trying to call a private method with an explicit receiver and if called in the same class with self would fail.
end
end
Test1.new.main_method
This will throw NoMethodError
You can never call the private method from outside the class hierarchy where it was defined.
Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class".
class Test1
def main_method
method_protected
end
protected
def method_protected
puts "InSide method_protected for #{self.class}"
end
end
class Test2 < Test1
def main_method
method_protected # called by implicit receiver
end
end
class Test3 < Test1
def main_method
self.method_protected # called by explicit receiver "an object of the same class"
end
end
InSide method_protected for Test1
InSide method_protected for Test2
InSide method_protected for Test3
class Test4 < Test1
def main_method
Test2.new.method_protected # "Test2.new is the same type of object as self"
end
end
Test4.new.main_method
class Test5
def main_method
Test2.new.method_protected
end
end
Test5.new.main_method
This would fail as object Test5 is not subclass of Test1
Consider Public methods with maximum visibility
Summary
Public: Public methods have maximum visibility
Protected: Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class".
Private: For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver. This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.
First Three types of access specifiers and those define thier scope.
Public -> Access anywhere out side the class.
Private -> Can not access outside the class.
Protected -> This Method not access anywhere this method define
scope.
But I have a solution for this problem for all method how to access explain in depth.
class Test
attr_reader :name
def initialize(name)
#name = name
end
def add_two(number)
#number = number
end
def view_address
address("Anyaddress")
end
private
def address(add)
#add = add
end
protected
def user_name(name)
# p 'call method'
#name = name
end
end
class Result < Test
def new_user
user_name("test355")
end
end
Object List
p test = Test.new("test")
p test.name
p test.add_two(3)
List item
p test.view_address
p r = Result.new("")
p r.new_user
Related
I'm wondering about definition of method in top level.
puts RUBY_VERSION #=> 2.1.5
def greet
"hello, world"
end
class Object
greet #=> "hello, world"
end
Why does greet act like private class method?
puts RUBY_VERSION #=> 2.1.5
def greet
"good morning"
end
# case 1
Object.private_methods(false).grep /greet/ #=> []
# case 2
Object.private_instance_methods(false).grep /greet/ #=> [:greet]
# case 3
Object.private_methods(true).grep /greet/ #=> [:greet]
In case 3, I found that greet is a private class method. But I'm wondering which class owns greet as a private class method. Object inherits itself?
Edit
I update my question.
Question #1
Does definition of method mean adding some methods in Object as private instance method.
Is this correct ?
Question #2
Object is an instance of Class. So, Object owns private class methods. These methods as private instance methods in Class.
Is this correct ?
Question #3
depends on question #1 and #2. Class inherits Object. So, Class owns greet as private class method and private instance method.
Is this correct ?
Why does greet act like private class method?
It doesn't. It acts like a private instance method. (In fact, there are no class methods in Ruby, only instance methods. The question is not whether a method is an instance method or a class method, rather it's what class the instance method is defined in.)
Methods defined at the top-level become private instance methods of Object.
# case 1
Object.private_methods(false).grep /greet/ #=> []
# case 2
Object.private_instance_methods(false).grep /greet/ #=> [:greet]
# case 3
Object.private_methods(true).grep /greet/ #=> [:greet]
In case 3, I found that greet is a private class method.
Like I said above: there are no class methods in Ruby.
greet is a private instance method of Object. Object is an instance of Class, Class is a subclass of Module, Module is a subclass of Object, ergo Object is an instance of Object i.e. itself.
Put another way: methods defined in Object are available for all objects. Classes are objects, too, ergo methods defined in Object are available for all classes, including Object.
But I'm wondering which class owns greet as a private class method.
None. There are no class methods in Ruby. greet is a private instance method of Object.
Object inherits itself?
No. Object is an instance of itself.
Question #1
Does definition of method mean adding some methods in Object as private instance method.
Is this correct ?
Methods defined at the top-level become private instance methods of Object.
Question #2
Object is an instance of Class. So, Object owns private class methods. These methods as private instance methods in Class.
Is this correct ?
I cannot parse your question, sorry.
However, there are no private instance methods of Class in your examples. The only method in your example is greet, which is a private instance method of Object, not Class.
If you want to know who owns a method, you can just ask Ruby:
method(:greet).owner
# => Object
Question #3
depends on question #1 and #2. Class inherits Object. So, Class owns >greet as private class method and private instance method.
Is this correct ?
No. There are no class methods in Ruby, only instance methods. And greet is not owned by Class, it is a private instance method of Object:
method(:greet).owner
# => Object
Using irb, defining greet at the top-level does NOT define greet as a private method, at least using ruby 2.x:
$ irb
> RUBY_VERSION
=> "2.3.0"
> def greet; "hi"; end
=> :greet
> Object.methods.grep /greet/
=> [:greet]
> Object.private_methods.grep /greet/
=> []
> Object.private_instance_methods.grep /greet/
=> []
In ruby, a "private method" is one that cannot be called with an explicit "receiver" in the usual way. (See e.g. this SO page.) Since self.greet and Object.greet both produce the same result as greet, it follows that greet (defined at the top-level in irb) cannot be a private method.
The situation is different when using the ruby compiler.
Consider the following example in Ruby:
class ParentClass
private def method
puts "private method"
end
end
class ChildClass < ParentClass
def method
puts "overridden, but should be private too"
end
end
ParentClass.new.method #=> raises exception
ChildClass.new.method #=> produces "overridden, but should be private too"
If I have no control over the code of ChildClass, is it possible to make it inherit method visibility from ParentClass?
Your question has nothing to do with inheritance. What is relevant here is the timing of the class method private. See the simplified example below. When called, this method turns the relavant method existing at that point into a private method.
class A
def foo; "foo1" end
private :foo
new.foo # => NoMethodError: private method `foo' called for #<A:0x007f321204fec0>
end
When you alter the method after private has applied, then a new method is defined with the same name, and the effect of private that applied to the previous definition is gone.
class A
def foo; "foo2" end
new.foo # => "foo2"
end
When you call private again on it, then it becomes private:
class A
private :foo
new.foo # => NoMethodError: private method `foo' called for #<A:0x007f3211ff6de8>
end
To illustrate this using your original example...
class ChildClass
private :method
end
... can be done after the initial definition of ChildClass and will make the method method private.
To summarize, visibility is a property of a (defined) method, not a property of a method name. So you cannot redefine a method but have the private status of a previous method with the same name but you can change the private status of the current method.
All objects, first prefer to their own defined methods, even if their parent classes has also the same named method. But if you really want in some ocaasion, to call the parent class method, then you can take a help from #class_eval for the same :
class ParentClass
private
def biz
puts "private method"
end
end
class ChildClass < ParentClass
def biz
puts "overridden, but should be private too"
end
end
ChildClass.class_eval do
remove_method :biz
self.new.biz
end
# private method `biz' called for #<ChildClass:0x8c3532c> (NoMethodError)
Now, see ChildClass object is calling the private method from the ParentClass. Now to make the call successful, you need to use #send.
ChildClass.class_eval do
remove_method :biz
self.new.send :biz # => private method
end
Another way, I used in most cases is :-
unbind = ParentClass.instance_method(:biz)
unbind.bind(ChildClass.new).call # => private method
Really, when you do inherit or mixin, whatever methods are not physically don't come in to the child or mixed in class respectively. They(parent class or mixed in module) are actually added to the method lookup chain of the class ChildClass.
How is it possible that if I define a method in the Object class (which is defined as private), I am able to call it from inside another class? I mean, when I call say_hello from inside the class Dog, how is it resolved if say_hello is a top-level defined method and therefore is private to the Object class? I know the Object class is in the method lookup of all the classes, but if the method is private it should not be accessible right?
def say_hello
p "Hello"
end
class Dog
def test_hello
say_hello
end
end
prova = Dog.new
prova.test_hello
I guess an easier explanation of my doubt would be: Why can I call a private method of a parent from a child?
class Animal
private
def prova
p "hello"
end
end
class Dog < Animal
def test_hello
prova
end
end
prova = Dog.new
prova.test_hello
Remember Dog inherits from Object, giving it access to Objects methods. You can extend the Object class all you want.
See answer to: How to extend class Object in Rails?
but if the method is private it should not be accessible right?
What makes you think that?
Private methods can only be called with an implicit receiver. You are calling it with an implicit receiver. Ergo, it should work … and it does.
I'm learning Ruby, and have come up to a point where I am confused.
The book I am using is talking about private, public, and protected methods, but I am still a bit confused. What are the differences between each?
Public - can be called from anywhere
Private - The method cannot be called outside class scope. The object can only send the message to itself
ex: the baker has bake method as public but break_eggs is private
Protected - You can call an object's protected methods as long as the default object self is an instance of the same class as the object whose method you're calling
ex: with n protected method, c1 can ask c2 to execute c2.n, because c1 and c2 are both instances of the same class
And last but not least:
Inheritance: Subclasses inherit the method-access rules of their superclass
if "class D < C", then D will exhibit the same access behaviour as instances of C
reference: http://www.amazon.com/Ruby-Rails-Techniques-Developers/dp/1932394699
public methods are open to everyone. As for private versus protected, I refer to "Ruby Private Methods vs. Protected Methods":
What is the difference between 'private' and 'protected' methods in
Ruby? In Ruby, the primary difference between a 'private' and
'protected' method is that a private method cannot be called with an
explicit receiver, while a protected method can. What is an
'explicit receiver', you ask? An explicit receiver is the object that
is receiving a message. In the following example, we have a receiver
('parent') and a method ('get_name'). The 'parent' object is
receiving the instruction to perform the 'get_name' method.
Check out "Ruby Programming/Syntax/Classes" for a detailed example and explanation.
Put simply, the differences between private, public, and protected methods are visibility of that method in the program, kinda like read-only, read and write, and near invisible.
Unlike some of the other languages, you can't completely hide a Ruby private method, you can only access private methods for your instance of object and not for any other object instance of a class.
Public, of course, is total accessibility and methods are usually defaulted to public with some exceptions.
Protected methods are accessible from objects of the same class or even children, which is not the case for a private method.
Let me explain
Private and protected methods work a little differently in Ruby than in most other
programming languages. Suppose you have a class called Foo and a subclass SubFoo .
In languages like Java, SubFoo has no access to any private methods defined by Foo .
As seen in the Solution, Ruby provides no way to hide a class’s methods from its sub-
classes. In this way, Ruby’s private works like Java’s protected.
Suppose further that you have two instances of the Foo class, a and b. In languages
like Java, a and b can call each other’s private methods. In Ruby, you need to use a
protected method for that. This is the main difference between private and protected methods in Ruby.
class Foo
private
def pri
'hey I am private of Foo'
end
protected
def prot
'Hey I am protected of Foo'
end
end
Now subclass of Foo
class SubFoo < Foo
def call_pri_of_foo
pri
end
def call_prot_of_foo
prot
end
end
Now calling the accessors within SubFoo
> sub_foo = SubFoo.new
=> #<SubFoo:0x00000002b56ad8>
> sub_foo.call_pri_of_foo
=> "hey I am private of Foo"
> sub_foo.call_prot_of_foo
=> "Hey I am protected of Foo"
Up to here; there seem to be no difference
next_sub_foo = SubFoo.new
=> #<SubFoo:0x00000002b1a0b0>
def next_sub_foo.access_private(child_of_sub_foo)
child_of_sub_foo.pri
end
def next_sub_foo.access_protected(child_of_sub_foo)
child_of_sub_foo.prot
end
Now calling the accessor
> next_sub_foo.access_private(sub_foo)
# => NoMethodError: private method `pri' called for #<SubFoo:0x00000002b56ad8>
but it can access the protected methods of its siblings
> next_sub_foo.access_protected(sub_foo)
# => "Hey I am protected of Foo"
You can also see #tenderlove's blog for more clear picture http://tenderlovemaking.com/2012/09/07/protected-methods-and-ruby-2-0.html
The difference will be on Visibility and how they are affected by Inheritance :
Visibility :
|| Anywhere || Public can be accessed from inside and outside the class.
|| Inside the class || Both Private and Protected can only be accessed from inside the class.
The similarity between Protected and Private :
Both can be accessed from outside the class through a public method.
The differences between Protected and Private are :
Private method can not be called with a receiver (not even with #self). UNLESS ... calling a PRIVATE SETTER method. If you try to remove the receiver, Ruby will create a local variable. Self is a must in this case.
Protected may or may not use self.
Protected can access another object's protected method that comes from the same class, Private can't.
When it comes to Inheritance :
Private methods can only be called on subclasses implicitly (simply just the name of the method) but not explicitly (using #self).
Protected can be called both ways (with or without #self || implicitly or explicitly).
Example with code below :
class Dog
attr_accessor :name, :age
def initialize(n, a)
self.name = n
self.age = a
end
def accessing_private
"#{self.name} in human years is #{human_years}. This is secret!"
end
def accessing_protected
"Will this work? " + a_protected_method
end
def eat_more_than(other)
# accessing other instance's protected method from the same class
daily_diet < other.daily_diet
"#{name} eats more than #{other.name}"
end
def boy
gender_method("boy") # accessing private setter method
end
protected
def daily_diet
age * 2 # the younger, the more they have to eat
end
def a_protected_method
"Yes, I'm protected!"
end
private
attr_writer :gender
def gender_method(gender)
self.gender = gender # private setter method requires self
"#{name} is a #{gender}"
end
def human_years
age * 8
end
end
# Create the first object of Dog
blake = Dog.new("Blake", 5)
p blake.accessing_private # "Blake in human years is 16. This is secret!"
p blake.accessing_protected # "Will this work? Yes, I'm protected!"
# Create the second object of Dog
jackson = Dog.new("Jackson", 1)
# Below, protected methods from different objects of the same type/class
# are proven to share access
p jackson.eat_more_than(blake) # true -> "Jackson eats more than Blake"
# Below, accessing private setter method through a public method.
p blake.boy # Blake is a boy
I think breaking down an explicit receiver is what is important if your having trouble grasping the concept.
An explicit receiver is an object that is accepting a message.
person.get_name
person is the receiver and the method "get_name" is giving instructions to the object "person" to perform the method "get_name".
class Person
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
#first_name = first_name
#last_name = last_name
puts "And #{phone_number}" # Private method called when initialized
end
private
def phone_number
return "XXX-XXX-XXXX"
end
end
p p1 = Person.new("mike", "jones")
p p1.phone_number # Not within the context of the object instance.
When a method is private, it can only be used by other methods inside the object in whose class it is defined.
Studying the information I've taken from here, I extended explanations through errors, and for my opinion, helps to understand why and how to use protected and not private.
1) Protected:
The line num 12 crash because the parameter received is from another class, the error message is clear:
v.rb:12:in `==': undefined method `sku' for "Object of another class ==> crash":String (NoMethodError)
2) Private:
If remove self from line 8 and 12, and I change protected for private, crash because in line 12, other doesn't know what sku is:
v.rb:12:in `==': private method `sku' called for #<Product:0x00000001574e68 #name="Bread", #quantity=1> (NoMethodError)
The program:
class Product
attr_accessor :name, :quantity
def initialize(name)
#name = name
#quantity = 1
puts "The SKU is #{self.sku}"
end
def == (other)
self.sku == other.sku
end
protected
def sku
name.crypt("yo")
end
end
milk1 = Product.new("Milk")
milk2 = Product.new("Milk")
bread = Product.new("Bread")
puts milk1 == bread
puts milk1 == milk2
puts milk1 == "Object of another class ==> crash"
Before I read this article, I thought access control in Ruby worked like this:
public - can be accessed by any object (e.g. Obj.new.public_method)
protected - can only be accessed from within the object itself, as well as any subclasses
private - same as protected, but the method doesn't exist in subclasses
However, it appears that protected and private act the same, except for the fact that you can't call private methods with an explicit receiver (i.e. self.protected_method works, but self.private_method doesn't).
What's the point of this? When is there a scenario when you wouldn't want your method called with an explicit receiver?
protected methods can be called by any instance of the defining class or its subclasses.
private methods can be called only from within the calling object. You cannot access another instance's private methods directly.
Here is a quick practical example:
def compare_to(x)
self.some_method <=> x.some_method
end
some_method cannot be private here. It must be protected because you need it to support explicit receivers. Your typical internal helper methods can usually be private since they never need to be called like this.
It is important to note that this is different from the way Java or C++ works. private in Ruby is similar to protected in Java/C++ in that subclasses have access to the method. In Ruby, there is no way to restrict access to a method from its subclasses like you can with private in Java.
Visibility in Ruby is largely a "recommendation" anyways since you can always gain access to a method using send:
irb(main):001:0> class A
irb(main):002:1> private
irb(main):003:1> def not_so_private_method
irb(main):004:2> puts "Hello World"
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> foo = A.new
=> #<A:0x31688f>
irb(main):009:0> foo.send :not_so_private_method
Hello World
=> nil
The difference
Anyone can call your public methods.
You can call your protected methods, or another member of your class (or a descendant class) can call your protected methods from the outside. Nobody else can.
Only you can call your private methods, because they can only be called with an implicit receiver of self. Even you cannot call self.some_private_method; you must call private_method with self implied.
iGEL points out: "There is one exception, however. If you have a private method age=, you can (and have to) call it with self to separate it from local variables."
Since Ruby 2.7 the self receiver can be explicit, self.some_private_method is allowed. (Any other explicit receiver is still disallowed, even if the runtime value is the same as self.)
In Ruby, these distinctions are just advice from one programmer to another. Non-public methods are a way of saying "I reserve the right to change this; don't depend on it." But you still get the sharp scissors of send and can call any method you like.
A brief tutorial
# dwarf.rb
class Dwarf
include Comparable
def initialize(name, age, beard_strength)
#name = name
#age = age
#beard_strength = beard_strength
end
attr_reader :name, :age, :beard_strength
public :name
private :age
protected :beard_strength
# Comparable module will use this comparison method for >, <, ==, etc.
def <=>(other_dwarf)
# One dwarf is allowed to call this method on another
beard_strength <=> other_dwarf.beard_strength
end
def greet
"Lo, I am #{name}, and have mined these #{age} years.\
My beard is #{beard_strength} strong!"
end
def blurt
# Not allowed to do this: private methods can't have an explicit receiver
"My age is #{self.age}!"
end
end
require 'irb'; IRB.start
Then you can run ruby dwarf.rb and do this:
gloin = Dwarf.new('Gloin', 253, 7)
gimli = Dwarf.new('Gimli', 62, 9)
gloin > gimli # false
gimli > gloin # true
gimli.name # 'Gimli'
gimli.age # NoMethodError: private method `age'
called for #<Dwarf:0x007ff552140128>
gimli.beard_strength # NoMethodError: protected method `beard_strength'
called for #<Dwarf:0x007ff552140128>
gimli.greet # "Lo, I am Gimli, and have mined these 62 years.\
My beard is 9 strong!"
gimli.blurt # private method `age' called for #<Dwarf:0x007ff552140128>
Private methods in Ruby:
If a method is private in Ruby, then it cannot be called by an explicit receiver (object). It can only be call implicitly. It can be called implicitly by the class in which it has been described in as well as by the subclasses of this class.
The following examples will illustrate it better:
1) A Animal class with private method class_name
class Animal
def intro_animal
class_name
end
private
def class_name
"I am a #{self.class}"
end
end
In this case:
n = Animal.new
n.intro_animal #=>I am a Animal
n.class_name #=>error: private method `class_name' called
2) A subclass of Animal called Amphibian:
class Amphibian < Animal
def intro_amphibian
class_name
end
end
In this case:
n= Amphibian.new
n.intro_amphibian #=>I am a Amphibian
n.class_name #=>error: private method `class_name' called
As you can see, private methods can be called only implicitly. They cannot be called by explicit receivers. For the same reason, private methods cannot be called outside the hierarchy of the defining class.
Protected Methods in Ruby:
If a method is protected in Ruby, then it can be called implicitly by both the defining class and its subclasses. Additionally they can also be called by an explicit receiver as long as the receiver is self or of same class as that of self:
1) A Animal class with protected method protect_me
class Animal
def animal_call
protect_me
end
protected
def protect_me
p "protect_me called from #{self.class}"
end
end
In this case:
n= Animal.new
n.animal_call #=> protect_me called from Animal
n.protect_me #=>error: protected method `protect_me' called
2) A mammal class which is inherited from animal class
class Mammal < Animal
def mammal_call
protect_me
end
end
In this case
n= Mammal.new
n.mammal_call #=> protect_me called from Mammal
3) A amphibian class inherited from Animal class (same as mammal class)
class Amphibian < Animal
def amphi_call
Mammal.new.protect_me #Receiver same as self
self.protect_me #Receiver is self
end
end
In this case
n= Amphibian.new
n.amphi_call #=> protect_me called from Mammal
#=> protect_me called from Amphibian
4) A class called Tree
class Tree
def tree_call
Mammal.new.protect_me #Receiver is not same as self
end
end
In this case:
n= Tree.new
n.tree_call #=>error: protected method `protect_me' called for #<Mammal:0x13410c0>
Consider a private method in Java. It can be called from within the same class, of course, but it can also be called by another instance of that same class:
public class Foo {
private void myPrivateMethod() {
//stuff
}
private void anotherMethod() {
myPrivateMethod(); //calls on self, no explicit receiver
Foo foo = new Foo();
foo.myPrivateMethod(); //this works
}
}
So -- if the caller is a different instance of my same class -- my private method is actually accessible from the "outside", so to speak. This actually makes it seem not all that private.
In Ruby, on the other hand, a private method really is meant to be private only to the current instance. This is what removing the option of an explicit receiver provides.
On the other hand, I should certainly point out that it's pretty common in the Ruby community to not use these visibility controls at all, given that Ruby gives you ways to get around them anyway. Unlike in the Java world, the tendency is to make everything accessible and trust other developers not to screw things up.
What's the difference?
Private Methods Explained
#freddie = Person.new
#freddie.hows_it_going?
# => "oh dear, i'm in great pain!"
class Person
# public method
def hows_it_going?
how_are_your_underpants_feeling?
end
private
def how_are_your_underpants_feeling? # private method
puts "oh dear, i'm in great pain!"
end
end
We can ask Freddie how things are going given it's a public method. That's perfectly valid. And it's normal and accepted.
But...the only person who can know how Freddie's underpants situation is, is Freddie himself. It would not do for random strangers to reach into Freddy's underpants - no, no -- it's very, very private, and we don't want to expose what's private to the outside world. In other words, we may not want to expose mutable data, to any caller in the world. Someone could mutate a value, and we'd be in a world of pain trying to discover where the bug came from.
#freddie.how_are_your_underpants_feeling?
# => # NoMethodError: private method `how_are_your_underpants_feeling?' called
Protected Methods Explained
Consider this:
class Person
protected
def hand_over_the_credit_card! # protected method
puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"
end
end
class Rib < Person
end
class Wife < Rib # wife inherits from Rib
def i_am_buying_another_handbag_with_your_card(husband)
husband.hand_over_the_credit_card! # equalityInAction
end
end
#husband = Person.new
#mrs = Wife.new
#mrs.i_am_buying_another_handbag_with_your_card(#husband)
# => puts "Lawd have mercy. Whatever. Here it is: 1234-4567-8910"
We're somewhat ok with mrs getting our credit card details, given mrs is flesh of our flesh, inherited from Person, so she can blow it on some shoes etc., but we don't want random individuals getting access to our credit card details.
If we tried to do that outside the subclass, it would fail:
#mrs = Wife.new
#mrs.gimme_your_credit_card!
# => protected method hand_over_the_credit_card! called for #<Wife:0x00005567b5865818> (NoMethodError)
Summary
private methods can only be called from within, and without "an explicit receiver". (Strictly speaking, you can access a private method using a little bit of ruby magic, but I'm going to ignore that for the moment).
protected methods can be called within sub-classes.
I used the examples/analogies to help you vividly remember.
Part of the reason why private methods can be accessed by subclasses in Ruby is that Ruby inheritance with classes is thin sugarcoating over Module includes - in Ruby, a class, in fact, is a kind of module that provides inheritance, etc.
http://ruby-doc.org/core-2.0.0/Class.html
What this means is that basically a subclass "includes" the parent class so that effectively the parent class's functions, including private functions, are defined in the subclass as well.
In other programming languages, calling a method involves bubbling the method name up a parent class hierarchy and finding the first parent class that responds to the method. By contrast, in Ruby, while the parent class hierarchy is still there, the parent class's methods are directly included into the list of methods of the subclass has defined.
Comparison of access controls of Java against Ruby: If method is declared private in Java, it can only be accessed by other methods within the same class. If a method is declared protected it can be accessed by other classes which exist within the same package as well as by subclasses of the class in a different package. When a method is public it is visible to everyone. In Java, access control visibility concept depends on where these classes lie's in the inheritance/package hierarchy.
Whereas in Ruby, the inheritance hierarchy or the package/module don't fit. It's all about which object is the receiver of a method.
For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver.
This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.
class Test1
def main_method
method_private
end
private
def method_private
puts "Inside methodPrivate for #{self.class}"
end
end
class Test2 < Test1
def main_method
method_private
end
end
Test1.new.main_method
Test2.new.main_method
Inside methodPrivate for Test1
Inside methodPrivate for Test2
class Test3 < Test1
def main_method
self.method_private #We were trying to call a private method with an explicit receiver and if called in the same class with self would fail.
end
end
Test1.new.main_method
This will throw NoMethodError
You can never call the private method from outside the class hierarchy where it was defined.
Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class".
class Test1
def main_method
method_protected
end
protected
def method_protected
puts "InSide method_protected for #{self.class}"
end
end
class Test2 < Test1
def main_method
method_protected # called by implicit receiver
end
end
class Test3 < Test1
def main_method
self.method_protected # called by explicit receiver "an object of the same class"
end
end
InSide method_protected for Test1
InSide method_protected for Test2
InSide method_protected for Test3
class Test4 < Test1
def main_method
Test2.new.method_protected # "Test2.new is the same type of object as self"
end
end
Test4.new.main_method
class Test5
def main_method
Test2.new.method_protected
end
end
Test5.new.main_method
This would fail as object Test5 is not subclass of Test1
Consider Public methods with maximum visibility
Summary
Public: Public methods have maximum visibility
Protected: Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class".
Private: For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver. This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.
First Three types of access specifiers and those define thier scope.
Public -> Access anywhere out side the class.
Private -> Can not access outside the class.
Protected -> This Method not access anywhere this method define
scope.
But I have a solution for this problem for all method how to access explain in depth.
class Test
attr_reader :name
def initialize(name)
#name = name
end
def add_two(number)
#number = number
end
def view_address
address("Anyaddress")
end
private
def address(add)
#add = add
end
protected
def user_name(name)
# p 'call method'
#name = name
end
end
class Result < Test
def new_user
user_name("test355")
end
end
Object List
p test = Test.new("test")
p test.name
p test.add_two(3)
List item
p test.view_address
p r = Result.new("")
p r.new_user