class Hash

Public Instance Methods

to_deep_ostruct() click to toggle source

Stolen and refactored from stackoverflow.com/a/11137694

# File lib/omu_support/core_ext/hash.rb, line 7
def to_deep_ostruct
  internal_hashes = {}
  each do |key, value|
    internal_hashes[key] = value if value.is_a?(Hash)
  end

  return OpenStruct.new self if internal_hashes.empty?

  duplicate = dup
  internal_hashes.each do |key, value|
    duplicate[key] = value.to_deep_ostruct
  end
  OpenStruct.new(duplicate)
end