From: Intransition Date: 2010-08-24T10:51:49+09:00 Subject: Re: Your thoughts on #memo ? On Aug 23, 12:47 pm, Caleb Clausen wrote: > On 8/23/10, Intransition wrote: > > > The use case for #memo is an alternative to: > > >   def m > >     @m ||= .... > >   end > > > But differs in that it avoids the need for an instance variable. I > > tend to use the "@m ||=" idiom a lot, though I wonder about it's > > threading implications, and whether something like #memo would be more > > robust. > > Your #memo method is no more re-entrant than the instance variable > example you gave above. If you want it to be re-entrant, you need to > have a mutex. Ah, thanks Caleb. So to be on the "safe" side I will have to use a mutex for any memoization code I might devise --there's no way around it. > Also, I seem to recall that methods defined via define_method are > slower than methods defined via def. But I couldn't see a way to > rewrite your code using def instead... Well, speed wise, I imagine the mutex will really take care of that ;-)