Dlaczego Ruby nie pozwala mi określić siebie jako odbiorcy w prywatnej metodzie?
Ruby jako język zorientowany obiektowo. Co to oznacza, niezależnie od tego, jaką wiadomość wysyłam, wysyłam ją na jakimś obiekcie / instancji klasy.
Przykład:
class Test
def test1
puts "I am in test1. A public method"
self.test2
end
def test2
puts "I am in test2. A public Method"
end
end
ma sens, nazywam metodętest2
naself
obiekt
Ale nie mogę tego zrobić
class Test
def test1
puts "I am in test1. A public method"
self.test2 # Don't work
test2 # works. (where is the object that I am calling this method on?)
end
private
def test2
puts "I am in test2. A private Method"
end
end
Gdytest2
jestpublic method
Mogę to wezwaćself
(dość słusznie, metoda wysłana do własnego obiektu). Ale kiedytest2
jestprivate method
Nie mogę tego nazwać sobą. Więc gdzie jest obiekt, na który wysyłam metodę?