From: ara.t.howard@... Date: 2006-06-30T23:45:08+09:00 Subject: Re: Useful inconcistency in calling foo vs. foo= private methods. On Fri, 30 Jun 2006, Alder Green wrote: > Hi > > This is an interesting sort of useful gotcha I recently stumbled upon: > > class Foo > private > > def bar > p '#bar called' > end > def bar=(val) > p '#bar= called' > end > end > > irb(main):002:0> foo = Foo.new > irb(main):003:0> foo.instance_eval { self.bar } > NoMethodError: private method `bar' called for # > irb(main):004:0> foo.instance_eval { self.bar = 1 } > "#bar= called" > > Formally, the the last line should have thrown an exception as well; > afaik private methods can't by definition be called with an explicit > receiver. It seems the assignment methods are exempt from this rule. > This is actually useful, since it overrides the higher precendence of > local variable assignment over method calls; if self.bar= didn't work, > I don't see any way short of self.send to call the private #bar=. a useful idiom class Foo private def bar *a if a.size == 0 @bar else __send__ 'bar=', a.shift end end def bar= val @bar = val end end Foo.new.instance_eval{ bar 42 } it avoids the issue somewhat. regards. -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama