Visibility and the "self" receiver

M

M. Maniak

Hello,

I'm new to Ruby (coming from Java) and puzzled
by the behaviour of "self" when defining a private method:

-->Snip
class TestClass

def initialize(args)
@myString = args
self.print()
end

private
def print()
puts @myString
end
end
-->Snip

This produces a NameError when instantiated/executed.
Without reference to itself (without "self") everything works...

I guess print() is then treated as a function and not as
a method (whatever that means in OOP -> maybe no return value).

I know that in Java "this" is a somehow comparable reference
with "self" in Ruby and denotes the actual object. I use it
quite often...

Has anyone an explanation about this specific behaviour (I know
it's not a bug)? Maybe I don't get the principle of "private"
or Visibility in general in Ruby.

Thank you very much for a hint.

Greets,
M. Maniak
 
D

David A. Black

Hi --

Hello,

I'm new to Ruby (coming from Java) and puzzled
by the behaviour of "self" when defining a private method:

-->Snip
class TestClass

def initialize(args)
@myString = args
self.print()
end

private
def print()
puts @myString
end
end
-->Snip

This produces a NameError when instantiated/executed.
Without reference to itself (without "self") everything works...

I guess print() is then treated as a function and not as
a method (whatever that means in OOP -> maybe no return value).

In Ruby everything you define with "def" is a method, and has a return
value.
I know that in Java "this" is a somehow comparable reference
with "self" in Ruby and denotes the actual object. I use it
quite often...

Has anyone an explanation about this specific behaviour (I know
it's not a bug)? Maybe I don't get the principle of "private"
or Visibility in general in Ruby.

"private" basically means: this method can only be called *without* an
explicit receiver. "self" is considered an explicit receiver, even
though it's the current object.

There are some cases where you must use "self", and you're allowed to
do that even for a private method. (Specifically, you always need an
explicit receiver for methods that end with "=", because otherwise
they'll be parsed as assignment to a local variable.)

But other than those cases, if you have to specify a receiver, you are
not in a scope where you can call that receiver's private methods.


David
 
M

Mark Hubbart

Hello,
=20
I'm new to Ruby (coming from Java) and puzzled
by the behaviour of "self" when defining a private method:
try:

-->Snip
class TestClass
=20
def initialize(args)
@myString =3D args
print()
end
=20
private
def print()
puts @myString
end
end
-->Snip

... which should work.
This produces a NameError when instantiated/executed.
Without reference to itself (without "self") everything works...
=20
I guess print() is then treated as a function and not as
a method (whatever that means in OOP -> maybe no return value).
=20
I know that in Java "this" is a somehow comparable reference
with "self" in Ruby and denotes the actual object. I use it
quite often...
=20
Has anyone an explanation about this specific behaviour (I know
it's not a bug)? Maybe I don't get the principle of "private"
or Visibility in general in Ruby.

Private methods can only be called without an explicit receiver. This
means that they can only be called when the implicit receiver is
"self", and no receiver is specified:

class A
private
def a() :a end
end

A.new.a #=3D=3D> error, a is private and called with an explicit receiver.
A.new.instance_eval do #=3D=3D> changes self to A.new for this block
self.a #=3D=3D> error, explicit receiver
a #=3D=3D> no error, implicit receiver is self
end

HTH,
Mark
 
M

M. Maniak

Oh well, that seems to explain it...

Thank you very much kind people of the Ruby Mailing list.

Greetings from Switzerland
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top