From: "Shot (Piotr Szotkowski)" Date: 2009-09-02T20:00:15+09:00 Subject: Re: Private methods’ viability (was: What is the ruby conventions to name private method?) --/9DWx/yDrRhgMJTb Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Robert Klemme: > 2009/8/31 Shot (Piotr Szotkowski) : >> I also use protected attr_readers so that my objects can tell whether >> they =3D=3D others of the same kind without exposing their instance >> variables to the outside world; an attr_reader on an Array/Hash/Set >> does not guarantee that the given Array/Hash/Set is not modifiable from >> the outside world=C2=B9, hence I=E2=80=99d rather not expose such instan= ce variables >> more than necessary. > -0 :) > you cannot really prevent access to internals and if you include > attributes in the equivalence relation you usually want to access > them from the outside. I guess this is true in most cases. In my main case, though, I=E2=80=99m us= ing a math/logic representation of a finite state machine and the =E2=80=98outs= ide world=E2=80=99 should really only be interested in certain aggregated prope= rties of the FSM; at the same time, direct comparison of two FSMs=E2=80=99 instan= ce variables=E2=80=99 states is the best way of assessing their equality. In the case of FSMs I simply freeze the ivars upon instantiation, so it wouldn=E2=80=99t hurt to expose the attr_readers (although nothing else sho= uld really use them, and I don=E2=80=99t feel like writing specs for the access= ors), but there are other objects in my code where I can=E2=80=99t freeze the iva= rs (graphs change their vertices and edges over time), I don=E2=80=99t want to expose them and still the best way to compare two such objects to each other is to check their ivar state (and here protected attr_readers work lovely for me). (As for the =E2=80=98you cannot really prevent access to internals=E2=80=99= =E2=80=93 that=E2=80=99s true, but I can make sure it looks like a code smell when one accesses my classes=E2=80=99 internals directly.) >> =C2=B9 It=E2=80=99s one of the few places where Ruby diverges from the p= rinciple >> of least surprise; I can see certain lovely symmetry and simplicity >> in attr_reader working the same for all variables, but I found the name >> a bit misleading when I started to learn Ruby =E2=80=93 my thoughts were= that >> it should either not be called a reader or should return a dup of the >> variable in question=E2=80=A6 (I did get used to this since then, though= , and >> realised that other languages=E2=80=99 generic getters usually do the sa= me.) > This is not necessary as a general functionality because there > are immutable classes, others can be frozen and last but not least > sometimes you want to give someone access to internals. Yes, and to add another example, sometimes the ivars represent singleton objects which really shouldn=E2=80=99t be duped by attr_reader. I think my main point was that saying =E2=80=98attr_reader is for read acce= ss to an ivar and attr_writer is for write access to it=E2=80=99 (which is one of= the way of understanding the attr_* stuff) should be usually accompanied with the =E2=80=98instance.ivar << something=E2=80=99 example showing that = the =E2=80=98read access=E2=80=99 shouldn=E2=80=99t be taken as =E2=80=98read-o= nly access=E2=80=99, but rather as =E2=80=98here, look, that=E2=80=99s the object behind the ivar, you can = do anything to it=E2=80=99 access. =E2=80=94 Shot --=20 Seen on 12th Ave. today: Random car with a =E2=80=98I <3 Kilgore Books & Co= mics=E2=80=99 sticker. I <3 them too. I might even <4 them. [Wes Morgan] --/9DWx/yDrRhgMJTb Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkqeUDoACgkQi/mCfdEo8UpaswCdEGIdkteO6Yd3Q6Or1djCqaiV 2BQAoNOB8lK+QRx+Wdjpm8KNm6o5gsqB =scb3 -----END PGP SIGNATURE----- --/9DWx/yDrRhgMJTb--