From: "Shot (Piotr Szotkowski)" Date: 2008-02-20T01:01:30+09:00 Subject: Object#freeze as a basis for caching of method results? --hOJnIwsuFPqlboSx Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello, ruby-talk. After profiling my code I figured out I might want to attempt some caching of results obtained in =E2=80=98heavy=E2=80=99 methods. Is there an= idiomatic way to do method-results caching in Ruby? The first thing that came to my mind as a mean to ensure the cache=E2=80=99s =E2=80=98freshness=E2=80=99 is to freeze the object in question. Does this = make sense? For example, assume that a Blanket is a Set of Blocks, and that there=E2=80=99s a computation-intesive method =E2=80=98separations=E2= =80=99: class Blanket < Set def separations seps =3D Set[] # some =E2=80=98heavy=E2=80=99 code that builds seps seps end end Would the below make sense? class Blanket < Set def freeze each { |block| block.freeze } super end def separations return @seps if @seps @seps =3D Set[] # the =E2=80=98heavy=E2=80=99 code from that builds @seps this time freeze @seps end end I guess my question boils down to what does exactly Object#freeze prevent from being modified =E2=80=93 simply all the properties? If so, does it mean I can only have one method like the above (because if some other method did any freezing, I couldn=E2=80=99t initialise the @s= eps cache when the first call to separations occurs)? Is there any =E2=80=98best practice=E2=80=99 in Ruby with regards to caching method results? Is there any obvious other approach to such caching? (I thought about having a @cache instance variable that would get reset on object changes, but I=E2=80=99m not sure how to obtain all the list of methods that can change an object =E2=80=93 i.e., methods that throw error when a given object is frozen.) Thanks in advance for any replies/suggestions/insights! -- Shot --=20 Q is for quota, a Berkeley-type fable, and R is for ranlib, for sorting ar table. S is for spell, which attempts to belittle, while T is for true, which does very little. --hOJnIwsuFPqlboSx Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFHuvuxi/mCfdEo8UoRAl8sAJ95H9ZNXITRrECvEP+sY7/NsvXipACfcBBg ErVRIk4PGrbUZkKyyR37GDg= =NAvq -----END PGP SIGNATURE----- --hOJnIwsuFPqlboSx--