1 2 3 4 5 6 7 8 9 10 11 12
def cool @result ||= begin 10_000_000.times { 1 + 1 } # spend lots of time 5 end end p Time.now p cool # takes 4s p Time.now p cool # instant p Time.now
1 2 3 4 5
Thu Dec 01 10:34:58 +0000 2011 5 Thu Dec 01 10:35:02 +0000 2011 5 Thu Dec 01 10:35:02 +0000 2011