Skip to content

Instantly share code, notes, and snippets.

@rob-murray
Created September 7, 2015 13:59
Show Gist options
  • Save rob-murray/43a2aa27eaefe39600fb to your computer and use it in GitHub Desktop.
Save rob-murray/43a2aa27eaefe39600fb to your computer and use it in GitHub Desktop.
Ruby cache hashe
# a method that is a simple hash acting as a cache to remote service, db call, etc
#
def a_cache
@a_cache ||= Hash.new do |cache, reference|
cache[reference] = Service.fetch(reference)
end
end
# usage
# a_cache[reference]
> a_cache["abc012012"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment