From: David Alan Black Date: 2001-12-23T11:13:33+09:00 Subject: [ruby-talk:29335] Re: a better way? Hi -- On Sun, 23 Dec 2001, Carlos wrote: > > class Temp > > @@save = {} > > def fahrenheit(c) > > result = @@save[self] and return result > > puts "Performing calculation...." > > @@save[self] = c * 9 / 5 + 32 > > end > > end > > > > t = Temp.new > > > > puts t.fahrenheit(100) # "Performing calculation....\n212" > > puts t.fahrenheit(100) # "212" > > puts t.fahrenheit(300) # "212" > > Ooohh!! > ;) What's the matter? You've never heard of science fiction? :-) I knew I should have quit while I was ahead. My first little in-house proof-of-concept version didn't do anything useful but was actually correct. Then again, it would be hard to argue that the one I posted does anything useful :-) Well, just for my own peace of mind: class Integer @@save = {} def fahrenheit result = @@save[self] and return result puts "Performing calculation...." @@save[self] = self * 9 / 5 + 32 end end David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav