From: Trans Date: 2007-01-14T02:46:06+09:00 Subject: Re: return_on Pit Capitain wrote: > Trans schrieb: > > I don't really have any examples that are repleat with it, but as to > > insight it's especially convenient when caching a return value: > > > > def x > > return_on @cache[:x] > > # do stuff > > @cache[:x] = result_of_stuff > > end > > > > this gets rid of an extraneous variable too b/c otherwise, the more > > efficient impl. is: > > > > def x > > r = @cache[:x] > > return r if r > > # do stuff > > @cache[:x] = result_of_stuff > > end > > You could also implement this as > > def x > @cache[:x] ||= ( > # do stuff > result_of_stuff > ) > end That pretty good. I rarely use ( ) as local encapsulator and probably should consider it more often. I'm generally partial to less indention when I can get it though. Thanks, T.