I have a confusion in Ruby's way of dealing with "private" instance methods.
"Private" means that the method is accessible only within the class, and it
is callable only in "function form," with self (implicit or explicit) as a
receiver." "Protected" gives access to the method in subclasses.
as is the case in Java or Obj C
However ... here is some very simple code that is not doing what I
expect ...
···
####################################################
# Example One
# Works as expected
####################################################
/usr/bin/ruby -w
class MyClass
protected
def say
puts "Hello"
end
public
def sayit
self.say
end
end
class AClass < MyClass
end
puts "Start"
r = MyClass.new
r.sayit
t = AClass.new
t.sayit
#########################################
salamanca:~/Hack/Hack04/Ruby1 jna$ ./int.rb
Start
Hello
Hello
That is exactly as I expected with the subclass having access to a super
"protected" method.
####################################################
# Example Two
# Not as expected
####################################################
/usr/bin/ruby -w
class MyClass
private
def say
puts "Hello"
end
public
def sayit
self.say
end
end
class AClass < MyClass
end
puts "Start"
r = MyClass.new
r.sayit
t = AClass.new
t.sayit
#########################################
salamanca:~/Hack/Hack04/Ruby1 jna$ ./int.rb
Start
../int.rb:10:in `sayit': private method `say' called for #<MyClass:0x26148>
(NoMethodError)
from ./int.rb:19
What I expected to see here is the first call to r.sayit retruning "Hello"
as it is calling it's own "private" method. I expected to see the error on
the second call ot t.sayit.
What am I missing? This is not to the best of my knowledge the way things
are in Java.
Thanks
john