Skip to content

Instantly share code, notes, and snippets.

@adam12
Created June 20, 2016 19:15
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 adam12/a722172021778fcc3055abb366624037 to your computer and use it in GitHub Desktop.
Save adam12/a722172021778fcc3055abb366624037 to your computer and use it in GitHub Desktop.
require "delegate"
class HashDigger < SimpleDelegator
def method_missing(method_sym, *arguments, &block)
if __getobj__.has_key?(method_sym)
value = __getobj__[method_sym]
if value.is_a?(Hash)
self.class.new(value)
else
value
end
else
super
end
end
def respond_to?(method_sym, include_private = false)
__getobj__.has_key?(method_sym) || super
end
end
obj = HashDigger.new({ key1: { key2: [1, 2, 3] } })
puts obj.key1.key2[2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment