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 `<main>'
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.
-- Matma Rex
···
2012/7/5 Jingjing Duan <lists@ruby-forum.com>:
Why can m2 call self.m1= which is a private method?
assigning hello to @m1
#<NoMethodError: private method `m3' called for #<A:0x9ba3a0 @m1="hello">>
#<NoMethodError: private method `m3' called for #<A:0x9b9ac0>>
inside m3
···
On Thu, Jul 5, 2012 at 6:16 PM, Jingjing Duan <lists@ruby-forum.com> wrote:
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.