Skip to content

Instantly share code, notes, and snippets.

@zishe
Created July 29, 2014 17:36
Show Gist options
  • Save zishe/820129f2890d1d0722f2 to your computer and use it in GitHub Desktop.
Save zishe/820129f2890d1d0722f2 to your computer and use it in GitHub Desktop.
require 'ostruct'
class DeepStruct < OpenStruct
def initialize(hash=nil)
@table = {}
@hash_table = {}
@array = []
if hash
hash.each do |k, v|
if v.is_a? Hash
@table[k.to_sym] = self.class.new(v)
elsif v.is_a? Array
@table[k.to_sym] = []
v.each { |a| @table[k.to_sym] << self.class.new(a) }
else
@table[k.to_sym] = v
end
@hash_table[k.to_sym] = v
new_ostruct_member(k)
end
end
end
def to_h
@hash_table
end
def method_missing(_, *args)
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment