From: Simon Strandgaard Date: 2007-01-05T19:01:59+09:00 Subject: Re: How can I do this better? On 1/5/07, Sam Kong wrote: > I find myself to use the following idiom frequently. > > def foo i > return @cache[i] if @cache[i] > @cache[i] = some_method_that_takes_long_time(i) > end > > I like the post-modifying if statement. > But I feel uncomfortable with calling @cache[i] twice. [snip] how about? irb(main):001:0> a = nil => nil irb(main):002:0> a ||= 42 => 42 irb(main):003:0> a ||= 666 => 42 irb(main):004:0> a => 42 -- Simon Strandgaard http://opcoders.com/