turn nested hashes into nested ostructs

M

michael greenly

[Note: parts of this message were removed to make it a legal post.]

I just thought I'd share this on the off chance anyone would find it useful.
It simply turns a nested hash into a nested ostruct.

require 'ostruct'
class NestedOstruct
def self.new(hash)
OpenStruct.new(hash.inject({}){|r,p| r[p[0]] = p[1].kind_of?(Hash) ?
NestedOstruct.new(p[1]) : p[1]; r })
end
end

Which allows you to do this...
=> 42

It's pretty basic but I found the NestedOstruct with YAML::load_file pretty
useful today.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top