Skip to content

Instantly share code, notes, and snippets.

@zeppelin
Created November 29, 2010 11:21
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 zeppelin/719846 to your computer and use it in GitHub Desktop.
Save zeppelin/719846 to your computer and use it in GitHub Desktop.
Ruby Hash to OpenStruct converter function
# Found at http://www.dribin.org/dave/blog/archives/2006/11/17/hashes_to_ostruct/
require 'ostruct'
def hashes2ostruct(object)
return case object
when Hash
object = object.clone
object.each do |key, value|
object[key] = hashes2ostruct(value)
end
OpenStruct.new(object)
when Array
object = object.clone
object.map! { |i| hashes2ostruct(i) }
else
object
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment