From: Tim Pease Date: 2007-01-20T03:33:11+09:00 Subject: Re: Declaring instance variables dynamically On 1/19/07, Ryan Davis wrote: > > On Jan 19, 2007, at 9:55 AM, Alex Schearer wrote: > > > Coming from PHP I want to take a class say, > > > > class User > > @name > > end > > > > And call it: > > u = User.new > > > > And then add a second class variable previously not declared: > > u.mood = "happy" > > > > PHP being what it is, it let you declare public class variables in > > that > > manner. Is there a way to acheive the same in Ruby? > > ri OpenStruct > http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html To elucidate a little more, OpenStruct is a great class that does what you want ... u = OpenStuct.new u.name = 'bob' u.mood = 'surly' p u.name p u.mood u.freeze u.age = 59 #=> raises TypeError Blessings, TwP