Skip to content

Instantly share code, notes, and snippets.

@labocho
Created December 7, 2021 06:55
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 labocho/88c8389e5b5372eb3a2daa60d73d1a00 to your computer and use it in GitHub Desktop.
Save labocho/88c8389e5b5372eb3a2daa60d73d1a00 to your computer and use it in GitHub Desktop.
module StrictAccessor
# replace `(Array|Hash)#[]` with `fetch` recursively
def self.strictify(o)
case o
when Array
o.map {|e| strictify(e) }.extend(self)
when Hash
o.each_with_object({}) {|(k, v), h| h[k] = strictify(v) }.extend(self)
else
o
end
end
def [](key_or_index)
fetch(key_or_index)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment