Created
November 29, 2010 11:21
-
-
Save zeppelin/719846 to your computer and use it in GitHub Desktop.
Ruby Hash to OpenStruct converter function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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