From: "Shot (Piotr Szotkowski)" Date: 2008-02-21T04:01:45+09:00 Subject: Re: Object#freeze as a basis for caching of method results? --JGRpDW+vuEVQ9uQb Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Robert Klemme: > 2008/2/19, Shot (Piotr Szotkowski) : >> Is there an idiomatic way to do method-results caching in Ruby? > http://raa.ruby-lang.org/project/memoize/ Ah, perfect, thanks. From both the gem=E2=80=99s docs and the source itself, it looks like it nicely handles the caching, provided that either the memoized method=E2=80=99s result does not depend on the object state or the object state doesn=E2=80=99t change, right? > An alternative approach would be to use current state as cache key, > i.e. create an immutable copy and stuff that along with calculation > results into a Hash. Hm, that=E2=80=99s actually an approach worth considering. I do need to take memory use into account, unfortunately, but it seems this is worth testing. I assume this approach highly depends on (a) making sure #freeze freezes also all referenced objects (like in my original example) and (b) #=3D=3D, #eql? and #hash are sensibly implemented (because Hash uses them for key comparison), right? > I suggest you do not inherit Set. In that case it's easy: > *you* define which methods change the state of your class. Hm, that=E2=80=99s true; also, if I get all this right, the ones that change the state could simply clear Memoize=E2=80=99s cache. I=E2=80=99ll see how much of the stuff inherited from Set I actually use = =E2=80=93 quite a bit, I assume, but then I could simply make an instance variable of @set and pass all these method calls to it (while selectively invalidating cache)=E2=80=A6 Thanks a *ton*, Robert; as usual, your reply is both invaluable and makes me think in the right direction. -- Shot --=20 <+aeonite> is there any diet plan that does not involve fucking cottage cheese? <@LordCrank> some of them involve eating it instead --JGRpDW+vuEVQ9uQb Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFHvG3ci/mCfdEo8UoRAjVTAKCSCjbxxRbxZAAKiKrmtd55kYh5awCfVyr8 p/9cac2oz+4TVVXWpp/MsZM= =CgNX -----END PGP SIGNATURE----- --JGRpDW+vuEVQ9uQb--