Answer the question
In order to leave comments, you need to log in
Calling private methods?
Hello. Please tell me, there's something I can't figure out. Does the private write method work fine, but does the private read method throw an error?
test.rb:8:in `read': private method `session_lang' called for #<Tst:0x000000000302e4e8>
class Tst
def write
self.session_lang = 'ru'
end
def read
self.session_lang
end
private
def session_lang
p 'read'
end
def session_lang= (value)
p 'write'
end
end
tst = Tst.new
tst.write
tst.read
def read
self.session_lang
end
def write
session_lang = 'ru'
end
def write
self.session_lang ||= 'ru'
end
Answer the question
In order to leave comments, you need to log in
because you call a public method on an object, but it doesn't exist. It doesn't matter that self and the current object are the same. Ruby sees the syntax for calling a public method, calls it and does not find it, says that there is the same, but private.
It is normal behavior that write methods require self.
Precisely for the fact that such garbage as you did not exist.
But since they are called via self, private methods are not available.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question