Why doesn´t the call with self doesn´t work ? I know that private means private to the object, but the private methode call is within the same object (at least it´s what i think). Doesn´t self mean "the object himself" ? So why is this error occuring ?
test.rb:14:in `with_self': private method `private_methode' called for #<PrivateTest:0x2aaaaab00508> (NoMethodError)
[code]
class PrivateTest
def private_methode()
puts("I am private")
end
def without_self()
puts("without self")
private_methode()
end
def with_self()
puts("with self")
self.private_methode()
end
private :private_methode
end
test = PrivateTest.new
test.without_self()
test.with_self()
[/code]
Why doesn´t the call with self doesn´t work ? I know that private means
private to the object, but the private methode call is within the same
object (at least it´s what i think). Doesn´t self mean "the object
himself" ? So why is this error occuring ?
<snip target="code"/>
Just quoting from the Pixcake (or was it Pickaxe book
[...]
- *Private methods* cannot be called with an explicit receiver.
Because you cannot specify an object when using them, private methods can be
called only in the defining class and by direct descendents within that same
object.
The difference between ``protected'' and ``private'' is fairly subtle, and
is different in Ruby than in most common OO languages. If a method is
protected, it may be called by *any* instance of the defining class or its
subclasses
[...]
So in Ruby surprisingly one canot indicate an explicit reveiver to a private
method. Even if it is self which comes to a surprise at the beginning.
If I understand correctly this might change in Ruby2, and maybe ritefully so
;).
Cheers
Robert
···
On 8/20/06, Marinho Tobolla <marinho.tobolla@syncity.de> wrote:
--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.