convert hash to object with OpenStruct
require 'ostruct'class Objectdef hash_to_ostruct(visited = []) selfendendclass Arraydef hash_to_ostruct(visited = []) map { |x| x.hash_to_ostruct(visited) }endendclass Hash def hash_to_ostruct(visited = []) os = OpenStruct.new each do |k, v| item = visited.find { |x| x.first.object_id == v.object_id } if item os.send("#{k}=", item.last) else os.send("#{k}=", v.hash_to_ostruct(visited + [ ])) end end os endend hash = { "country" => {:a => "a"}, :population => 20_000_000 }p hash.hash_to_ostruct
页:
[1]