Skip to content

Instantly share code, notes, and snippets.

@scottlowe
Created August 6, 2011 21:50
Show Gist options
  • Save scottlowe/1129782 to your computer and use it in GitHub Desktop.
Save scottlowe/1129782 to your computer and use it in GitHub Desktop.
Memoize with a begin block
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