Return to Snippet

Revision: 15649
at July 13, 2009 00:25 by benweissmann


Initial Code
# Ex:
# > foo = OpenStruct.new
# > foo.add :bar
# > foo.bar.a = 10
# > foo.bar.a
# => 10
# > foo.add 'bar.baz.bax'
# > foo.bar.baz.bax.something = 100
# > foo.bar.baz.bax.something
# => 100

require 'ostruct'
class OpenStruct
	def add name
		last = self
		name.to_s.split('.').each do |n|
			o = OpenStruct.new
			last.send n.concat('=').intern, o
			last = o
		end
		
		return last
	end
end

Initial URL

                                

Initial Description

                                

Initial Title
Add arbitary levels to an OpenStruct - OpenStruct#add

Initial Tags

                                

Initial Language
Ruby