From: Robert Klemme Date: 2013-04-12T20:41:25+09:00 Subject: Re: using an instance variable inside a method --00235446ffecf3a41d04da28612a Content-Type: text/plain; charset=ISO-8859-1 On Fri, Apr 12, 2013 at 10:37 AM, Ryan Davis wrote: > > On Apr 11, 2013, at 21:38 , Matthew Kerwin wrote: > > > * note: it also defines a method called `p=` which assigns to `@p`, so > > you can use `p = []` instead of `@p = []` if you want. C.f. attr_reader > > and attr_writer > > No. you can use `self.p = []`, not `p = []`. That's the gotcha because the > latter is treated as a local variable assign. > One more remark: it's probably not too good to name an attribute "p" because the accessor will hide method "p" which is used for debug printing. Also, the name "p" doesn't really reveal any semantics. I usually restrict those names to internal use only but use more telling names for public APIs. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --00235446ffecf3a41d04da28612a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Fri, Apr 12, 2013 at 10:37 AM, Ryan Davis <= ryand-ruby@ze= nspider.com> wrote:

On Apr 11, 2013, at 21:38 , Matthew Kerwin <lists@ruby-forum.com> wrote:

> * note: it also defines a method called `p=3D` which assigns to `@p`, = so
> you can use `p =3D []` instead of `@p =3D []` if you want. C.f. attr_r= eader
> and attr_writer

No. you can use `self.p =3D []`, not `p =3D []`. That's the gotch= a because the latter is treated as a local variable assign.

One more remark: it's proba= bly not too good to name an attribute "p" because the accessor wi= ll hide method "p" which is used for debug printing. =A0Also, the= name "p" doesn't really reveal any semantics. =A0I usually r= estrict those names to internal use only but use more telling names for pub= lic APIs.

Kind regard= s

robe= rt

--
remember.guy do |as, often| as.yo= u_can - without end
http://blog.rubybestpractice= s.com/
--00235446ffecf3a41d04da28612a--