Created
July 21, 2017 19:22
-
-
Save swalkinshaw/1c976033a7bd8d14139d57b2f5649716 to your computer and use it in GitHub Desktop.
Ruby hash delegator that tracks which keys were accessed
This file contains hidden or 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
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