From: "David A. Black" Date: 2008-06-30T04:40:08+09:00 Subject: Re: implicit vs explicit self in private methods --1926193751-64260073-1214768568=:5677 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-64260073-1214768568=:5677" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-64260073-1214768568=:5677 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 30 Jun 2008, dusty wrote: > On Jun 29, 3:19=A0pm, Stefano Crocco wrote: >> On Sunday 29 June 2008, dusty wrote: >> >> >> >>> I'm trying to understand the difference between implicitly and >>> explicitly calling a private method and am hoping someone could shed >>> some light on this for me. >> >>> class Tester >>> =A0 def public_hello_good >>> =A0 =A0 say_hello >>> =A0 end >>> =A0 def public_hello_bad >>> =A0 =A0 self.say_hello >>> =A0 end >>> =A0 private >>> =A0 def say_hello >>> =A0 =A0 "hello" >>> =A0 end >>> end >> >>> irb(main):001:0> require 'tester' >>> =3D> true >>> irb(main):002:0> a =3D Tester.new >>> =3D> # >>> irb(main):003:0> a.public_hello_good >>> =3D> "hello" >>> irb(main):004:0> a.public_hello_bad >>> NoMethodError: private method `say_hello' called for # >>> =A0 =A0from ./tester.rb:6:in `public_hello_bad' >>> =A0 =A0from (irb):4 >>> =A0 =A0from :0 >> >>> What I'm wondering is why does this not work when I call >>> self.say_hello, but does work when I call say_hello? =A0Since self is >>> the receiver in both cases, it seems that they would both work. >> >>> Anyone have any insight that they'd be willing to share on why it >>> works like that? >> >>> Thanks >> >> It's at the same time quite simple and quite confusing. It's simple beca= use >> the rule determining how a private method can be called is very short an= d >> clear: you can only call a private method using the implicit receiver. I= t's >> confusing because it seems natural to interpret the previous statement s= o that >> it reads: you can only call a private method if the receiver is equal (i= n the >> eql? sense) as the implicit receiver. However, this interpretation is wr= ong. >> The words "only using the implicit receiver" should be taken literaly: i= f a >> method is private, you can't call it using the dot notation >> (receiver.method_name), even when the implicit and the explicit receiver= are >> the same thing. >> >> I hope this helps >> >> Stefano > > Thanks for the response. > > I did find that it does work with an explicit receiver on a setter. > In the case below, I can call the private hello=3D method, with > self.hello=3D. I know that is required to state that I'm not setting a > local variable, but instead calling a method. But, it did work, so > somewhere in the code it is reading that self and understanding what I > mean by it, then perhaps dropping it and following the rules for the > rest of it? > > I guess I don't really need to know why, just understand that it is > what it is. Perhaps the why is outside my scope of understanding > right now anyway. But, I am still curious. I think it's just a necessary compromise. The local assignment syntax can't really change in a private method, so the only thing left is the explicit receiver. It does make the private rule a bit more tricky but in practice it ends up being OK. David --=20 Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS July 21-24 Edison, NJ See http://www.rubypal.com for details and updates! --1926193751-64260073-1214768568=:5677-- --1926193751-64260073-1214768568=:5677--