Question about private method

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)

"wang" <wwayya2004@hotmail.com> schrieb im Newsbeitrag
news:dc1b9b9.0406081833.6228ed2e@posting.google.com...

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

···

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)
$ _

···

On Tue, 08 Jun 2004 19:33:12 -0700, wang wrote:

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)

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

···

On Wed, Jun 09, 2004 at 04:08:38PM +0900, wang wrote:

class Test
  def func
    self.test #use self
  end
  
  private
  def test
    p "test"
  end
end

wwayya2004@hotmail.com (wang) wrote in message news:<dc1b9b9.0406081833.6228ed2e@posting.google.com>...

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?

"Robert Klemme" <bob.news@gmx.net> schrieb im Newsbeitrag
news:2insr4Fo72u5U1@uni-berlin.de...

"wang" <wwayya2004@hotmail.com> schrieb im Newsbeitrag
news:dc1b9b9.0406081833.6228ed2e@posting.google.com...
> 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

>
> 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)

Btw: interestingly enough you can do this:

class Foo
private
def test; p "ja"; end
end

=> nil

Foo.new.instance_eval { test }

"ja"
=> nil

Regards

    robert

Hi --

> 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.

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

···

On Thu, 10 Jun 2004, Paul Brannan wrote:

On Wed, Jun 09, 2004 at 04:08:38PM +0900, wang wrote:

--
David A. Black
dblack@wobblini.net

"wang" <wwayya2004@hotmail.com> schrieb im Newsbeitrag
news:dc1b9b9.0406100210.2305b0e@posting.google.com...

wwayya2004@hotmail.com (wang) wrote in message

news:<dc1b9b9.0406081833.6228ed2e@posting.google.com>...

> 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?

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).

class Foo; def test; p self; test2; end; private; def test2; p self; end;

end
=> nil

o=Foo.new

=> #<Foo:0x10181448>

o.test

#<Foo:0x10181448>
#<Foo:0x10181448>
=> nil

Regards

    robert