The following procedure has kept me in the middle of a long time, the concept of private in Ruby is really strange ...
Class Test Private
def test_print
puts ' test '
end
class Test2 < Test
def Test_print2
# Self.test_print #=> here Plus self cannot invoke, private method ' Test_print ' called for # (Nomethoderror)
test_print #=& Gt You can call end end Test2.new.test_print2 without self
Why not add self, private can also invoke the method of the parent class?
Originally in Ruby, Private is not the same as Java or other languages, subclasses can be invoked, but callers cannot be specified.
"The Ruby Way," the book says:
Private: Both classes and subclasses can be invoked, but private methods cannot specify the caller, and the default is self.
Protected: Both classes and subclasses can be invoked to specify the caller.
This explains why, in the above code, a call with self would be an error without self being able to execute correctly.