Created
August 6, 2011 21:50
-
-
Save scottlowe/1129782 to your computer and use it in GitHub Desktop.
Memoize with a begin block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def expensive_stats(id) | |
@expensive_stats ||= [] | |
@expensive_stats[id] ||= begin | |
puts "Executing resource intensive method for ID: #{id}" | |
sleep 5 | |
Time.now | |
end | |
end | |
puts expensive_stats 3 | |
puts expensive_stats 3 | |
puts expensive_stats 3 | |
puts expensive_stats 7 | |
puts expensive_stats 7 | |
puts expensive_stats 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment