From: David Alan Black Date: 2001-12-23T10:43:41+09:00 Subject: [ruby-talk:29332] Re: a better way? Hi -- On Sun, 23 Dec 2001, Hal E. Fulton wrote: > From: David Alan Black > > > Hi -- > > > > On Sun, 23 Dec 2001, Ron Jeffries wrote: > > > > > I was writing a little code that cached a function value, and wound up > > > with this: > > > > > > result = @@save[self] > > > return result if result > > > > > > I don't see a way to do it in one line unless I'm willing to do the > > > lookup twice. Am I missing something? > > > > You could do: > > > > result = @@save[self] and return result > > Or the C-like: > > if result=@@save[self] then return result end > > I don't fully understand your application, but I'm > assuming you *don't* want to return a nil and > check for it in the caller... otherwise, of course, > you could just do: > > return @@save[self] I think the design is that some calculation will only be performed once, then cached -- one of these guys: 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" David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav