Recursive OpenStruct

B

benny

Dear list,


didn't find anything like that so I thought, I could be useful to somebody:

You'll find a little recursive OpenStruct class below. It allows you to
generate an (pseudo)attribut based hierarchy on the fly. the calling of the
"attribut" is sufficious to generate it (you don't have to set it to a
certain value).

the object might later go back through this tree. might be useful for
settings as an alternative way to recursive hashes.

regards, benny



require 'ostruct'

class ROpenStruct < OpenStruct
def method_missing(mid, *args)
mname = mid.id2name
len = args.length
if len == 0
@table[mname.intern] = ROpenStruct.new
self.new_ostruct_member(mname)
@table[mname.intern]
else
super
end
end
end

test = ROpenStruct.new
test.p = "hello"
test.t.s.p.t.z.y.x = "hi"
test.xyz

p test
 

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

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top