From: Eric Hodel Date: 2004-04-07T19:01:46-07:00 Subject: Re: Preferred Instance Variable Access Method --+xNpyl7Qekk2NvDX Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Jean-Hugues ROBERT (jean_hugues_robert@yahoo.com) wrote: > Hi, >=20 > At 05:34 08/04/2004 +0900, you wrote: > What's the preferred way of accessing a class's instance variables? >=20 > I tend to use @var instead of self.var (I guess var() would work, and > var too). >=20 > I see two advantages: > 1) Faster (I never benchmarked however) > 2) Lines starting with @ are like methods with ! at the end =3D> > Draw the attention to probable side effect on obj's state. 3) Avoids setting local variables when you wanted to call a writer. > I do agree that typos can be an issue. There is warning however, > with ruby -w (that sometimes warns for some valid constructions > of mine, I guess defined? could help me avoid that). >=20 > Refering to a recent thread, I may add that self.var seems slightly > Python-ish. Without self., ruby may create a local variable rather than call the writer method when assigning: class Foo attr_accessor :bar def initialize @bar =3D "#bar=3D not called yet" end def meth bar =3D "#bar=3D called" puts self.bar end end Foo.new.meth # =3D> #bar=3D not called yet > I would personally prefer .var but languages with configurable style/ > syntax are rare these days (missing C's #define). --=20 Eric Hodel - drbrain@segment7.net - http://segment7.net All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --+xNpyl7Qekk2NvDX Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQFAdLKKMypVHHlsnwQRAhaWAJ9nF+yuU0a1qWt/8se/Z5w+jlBGMwCgtNx2 SIs/6bm/5jYxI4FxPvQ5xm0= =3BvA -----END PGP SIGNATURE----- --+xNpyl7Qekk2NvDX--