question about private method

W

wang

In ruby FAQ :
The visibility keyword private makes a method callable only in a
function form, and so it can only have self as a receiver.
But,why i can't write like below?

class Test
def func
self.test #use self
end

private
def test
p "test"
end
end

t=Test.new
t.func >>in `func': private method `test' called for
#<Test:0x2ac7318>NoMethodError)
 
R

Robert Klemme

wang said:
In ruby FAQ :
The visibility keyword private makes a method callable only in a
function form, and so it can only have self as a receiver.
But,why i can't write like below?

I think, this way enforcing the privacy is made easier (for the
implementor of the runtime).

Different question: Why would you need to call it like this?

Regards

robert
 
R

Robert Klemme

Robert Klemme said:
I think, this way enforcing the privacy is made easier (for the
implementor of the runtime).

Different question: Why would you need to call it like this?

Regards

robert

Btw: interestingly enough you can do this:
"ja"
=> nil

Regards

robert
 
G

greg

In ruby FAQ :
The visibility keyword private makes a method callable only in a
function form, and so it can only have self as a receiver.
But,why i can't write like below?

class Test
def func
self.test #use self
end

private
def test
p "test"
end
end

t=Test.new
t.func >>in `func': private method `test' called for
#<Test:0x2ac7318>NoMethodError)

class Test
def func
test # DON'T use self
end

private
def test
p "test"
end
end

t=Test.new
t.func
t.test # but you only can call test "from self"

$ ruby test.rb
"test"
test.rb:14: private method `test' called for #<Test:0x402a4ef8>
(NoMethodError)
$ _
 
P

Paul Brannan

class Test
def func
self.test #use self
end

private
def test
p "test"
end
end

If you want to do this, use protected instead of private.

I guess this means you can't have private accessors:

class Foo
def initialize
self.foo = 1
end

private
attr_accessor :foo
end

but I've never seen a case where I wanted to do that.

Paul
 
D

David A. Black

Hi --

If you want to do this, use protected instead of private.

I guess this means you can't have private accessors:

class Foo
def initialize
self.foo = 1
end

private
attr_accessor :foo
end

but I've never seen a case where I wanted to do that.

For some reason it does seem to allow this though:

class Foo
private
def x; end
attr_accessor :foo

public

def initialize
self.x rescue puts "x didn't work" # just for benchmark
self.foo = 1
p foo
end
end

Foo.new # x didn't work
# 1


(ruby 1.8.1 (2003-12-25) [i686-linux])

Maybe the self in self.foo= serves as a "this isn't a variable"
marker, but then is somehow discarded, so that it doesn't serve as a
"this method call has an explicit receiver" marker?


David
 
W

wang

In ruby FAQ :
The visibility keyword private makes a method callable only in a
function form, and so it can only have self as a receiver.
But,why i can't write like below?

class Test
def func
self.test #use self
end

private
def test
p "test"
end
end

t=Test.new
t.func >>in `func': private method `test' called for
#<Test:0x2ac7318>NoMethodError)

Thanks all!
Maybe i wasn't express my thought clearly.
I read "...it can only have self as a receiver..." in FAQ, so i call
"test" with "self" but wrong. What the really mean of "self" in FAQ?
 
R

Robert Klemme

wang said:
(e-mail address removed) (wang) wrote in message

Thanks all!
Maybe i wasn't express my thought clearly.
I read "...it can only have self as a receiver..." in FAQ, so i call
"test" with "self" but wrong. What the really mean of "self" in FAQ?

The receiver is implicitely 'self' if you invoke a method without
explicitely stating the receiver. There's a simple rule: there are no
functions in Ruby, only methods. So 'self' points to a valid instance under
all circumstances (even in methods belonging to nil, which is different than
Java's 'null' and C's (void*)0).
end
=> nil#<Foo:0x10181448>
#<Foo:0x10181448>
=> nil

Regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top