My understanding is the answer is no, you can’t call private methods
with explicit receivers, even self. See
However, the following example seems to counter that.
class A
def m2
self.m1 = “hello”
self.m3
end
private
def m1=(value) @m1 = value
puts “assigning #{value} to @m1”
end
def m3
puts “inside m3”
end
end
A.new.m2
The output is:
assigning hello to @m1
/tmp/t.rb:4:in m2': private method m3’ called for #<A:0x007fc4b404e568
@m1=“hehe”> (NoMethodError)
from /tmp/t.rb:19:in `’
Why can m2 call self.m1= which is a private method?
Why can m2 call self.m1= which is a private method?
Probably because it would be otherwise impossible (except for using #send), as m1 = "hello" would have been local variable assignment
and not method call.
Why can m2 call self.m1= which is a private method?
Probably because it would be otherwise impossible (except for using #send), as m1 = "hello" would have been local variable assignment
and not method call.
assigning hello to @m1
#<NoMethodError: private method m3' called for #<A:0x9ba3a0 @m1="hello">> #<NoMethodError: private method m3’ called for #<A:0x9b9ac0>>
inside m3
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.