What private Really Means

I was reading the book of Ruby meta-programming. Three things that
author said about this concepts,which I didn’t understand.

What private Really Means

Now that you know about self, you can cast a new light over
Ruby’s private keyword. Private methods are governed by a
single simple rule: you cannot call a private method with an
explicit receiver. In other words, every time you call a private
method, it must be on the implicit receiver—self. Let’s see a cor-
ner case:

class C
def public_method
self.private_method
end
private
def private_method; end
end
C.new.public_method

NoMethodError: private method ‘private_method’ called […]

You can make this code work by removing the self keyword.
This contrived example shows that private methods come from
two rules working together: first, you need an explicit receiver
to call a method on an object that is not yourself, and second,
private methods can be called only with an implicit receiver. Put
these two rules together, and you’ll see that you can only call
a private method on yourself. You can call this the “private rule.”

Q. What does author mean by yourself ?

Can object x call a private methodon object y if the two objects share
the same class? The answer is no, because no matter which class you
belong to, you still need an explicit receiver to call another object’s
method.

Q. Could anyone explain the above answer of the author? I didn’t get his
point.

Can you call a private method that you inherited from a superclass?
The answer is yes, because you don’t need an explicit receiver
to call inherited methods on yourself.

Q.Could anyone explain the above answer of the author? I didn’t get his
point.

On Saturday, October 5, 2013 6:06:09 PM UTC+2, Love U Ruby wrote:

Q. What does author mean by yourself ?

Look at this code:

class C
def method1(other_object)
other_object.method2
end

def method2
  "method2() called"
end

end

obj1 = C.new
obj2 = C.new
obj1.method1(obj2) # => “method2() called”

In this case, an object is calling a method (method2()) on another
object. What if obj1 wants to call method2() on itself? You can do it
like
this:

class C
def method1
self.method2
end
# the rest of the code is the same as above

The “self” keyword is optional here. You could also write:

class C
def method1
method2
end
# etc.

In this last example, the “receiver” (the object that receives the
method2() call) is implicit. If you don’t specify the receiver, then you
mean: “I want to call method2() on “myself” — the same object that
method1() has been called on”.

The trick with “private” is that if method2 is private, then you can
only
use this last form to call it — the one with an implicit receiver. So
you
cannot say “other_object.method2()”, and weirdly, you cannot either say
“self.method2”. You cannot specify the receiver, so the receiver has to
be
the implicit “self”.

So, the sentence with “yourself” that you asked about means: “Code in an
object can only call a private method on the same object — not any
other
object”. It doesn’t matter whether the receiver has the same class as
the
caller or an entirely different class: you still can’t do it.

Can you call a private method that you inherited from a superclass?

The answer is yes, because you don’t need an explicit receiver
to call inherited methods on yourself.

Q.Could anyone explain the above answer of the author? I didn’t get his
point.

Look at this:

class C2
def method2
“method2() called”
end

private :method2

end

class C3 < C2
def method1
method2()
end
end

C3.new.method1 # => “method2() called”

So C3#method1 can call C2#method2, because the receiver is implicit (so
it
doesn’t violate the “private” rule). On the other hand, you wouldn’t be
able to do this:

class C3 < C2
def method1
self.method2()
end
end

the rest of the code is the same as above

The code above violates the rule that you cannot use an implicit
receiver
to call a “private” method — so it will give you an error.

The point of the entire sidebar is: Ruby uses a strange trick to make
“private” methods work. It just looks at the receiver of the call, and
forbids calls to private methods with an explicit receiver.

Hello,

I saw that no one replied so, my two cents:

On 5 Οκτ 2013, at 18:06 , Love U Ruby [email protected] wrote:

method, it must be on the implicit receiver—self. Let’s see a cor-

Q. What does author mean by yourself ?

Not 100% sure but as I understand it, he means the object you’re
currently working on. The own owning the methods that is called.

Can you call a private method that you inherited from a superclass?
The answer is yes, because you don’t need an explicit receiver
to call inherited methods on yourself.

Q.Could anyone explain the above answer of the author? I didn’t get his
point.


Posted via http://www.ruby-forum.com/.

Here is a working example on ‘private’ keyword:

https://gist.github.com/atmosx/6866485

It’s a very basic scheme, where you can see how this is suppose to work
out. This keyword should be used when you write a method that will be
used by your program internally and you don’t want users to mess with
it. So you keep it private. Inheritance rules apply, sure if the
superclass has it’s inherited as well.

Not sure if I was of any assistance.

Bye

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

Panagiotis A. wrote in post #1123729:

Hello,

Posted via http://www.ruby-forum.com/.

Here is a working example on ‘private’ keyword:

https://gist.github.com/atmosx/6866485

Thanks for helping me…

I think private is not a keyword,rather it is a method
http://www.ruby-doc.org/core-2.0.0/Module.html#method-i-private

Paolo Perrotta wrote in post #1123661:

On Saturday, October 5, 2013 6:06:09 PM UTC+2, Love U Ruby wrote:

The point of the entire sidebar is: Ruby uses a strange trick to make
“private” methods work. It just looks at the receiver of the call, and
forbids calls to private methods with an explicit receiver.

Thank you very much Paolo for this detailed answer. I was heavily
excited when I saw the answer was posted by the Author of the book…

Thanks again! :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs