Thinking of optimizing the small script I wrote long ago to analyze
the geographical distribution of eigenclass' readership, I bumped into yet another instance
of the "hash with default block as memoized function" idiom. I've been finding
that quite often when reviewing older code of mine (or at least feel
that way).
That script uses GeoIP's
command-line utility to determine which country an IP address is associated
to. Calling an external program is really slow so I absolutely want to cache
those results:
iptocountry=Hash.newdo|h,ip|h[ip]= `geoiplookup#{ip}`.chomp.gsub(/^GeoIP Country Edition: /,"")endiptocountry.updateMarshal.load(File.read("geo.cache"))rescue{}
One minor inconvenience about hashes with default procs is that they cannot be
serialized, so it would seem one needs something like