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