Skip to content

Instantly share code, notes, and snippets.

@swalkinshaw
Created July 21, 2017 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swalkinshaw/1c976033a7bd8d14139d57b2f5649716 to your computer and use it in GitHub Desktop.
Save swalkinshaw/1c976033a7bd8d14139d57b2f5649716 to your computer and use it in GitHub Desktop.
Ruby hash delegator that tracks which keys were accessed
class TrackedHash < SimpleDelegator
attr_reader :accessed_keys
def initialize(hash)
super
@accessed_keys = Set.new
end
def [](key)
@accessed_keys << key
super
end
def fetch(*args)
@accessed_keys << args[0]
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment