Skip to content

Instantly share code, notes, and snippets.

@timonv
Last active December 13, 2015 18:09
Show Gist options
  • Save timonv/4953224 to your computer and use it in GitHub Desktop.
Save timonv/4953224 to your computer and use it in GitHub Desktop.
# For pretty oneliners. It just memoizes on nil, but accepts TrueClass as a value
def memoize(&block)
key = caller[0][/`.*'/][1..-2] # Get the method name of the caller
@_memoized ||= {}
@_memoized[key].nil? ? @_memoized[key] = block.call : @_memoized[key]
end
# memoize do
# ExpensiveOperation
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment