Ruby: Hash -> Structの変換
こんな感じで書けます
def deep_struct(hash) foo = hash.values.map do |v| case v when Hash deep_struct(v) when Array v.map { |x| deep_struct(x) } else v end end Struct.new(*hash.keys).new(*foo) end
OOPっぽく書きたければHash, Arrayに deep_struct
のようなメソッドを拡張するのもアリです