From: "ara.t.howard" Date: 2007-11-15T12:27:53+09:00 Subject: Re: adding an attribute to an object On Nov 14, 2007, at 7:10 PM, Mark Volkmann wrote: > I know how to add new methods to an existing object, but is there a > way to add an attribute? > I know I can do it if the object was created with ObjectStruct, but > what if it is a "normal" Ruby object? > > R. Mark Volkmann > Object Computing, Inc. > cfp:~ > cat a.rb require 'attributes' ### gem install attributes o = Object.new o.instance_eval{ attribute 'foobar' => 42 } p o.foobar #=> 42 o.foobar = 'forty-two' p o.foobar #=> "forty-two" p o.foobar? #=> "forty-two" (true) o.foobar 42.0 p o.foobar #=> 42.0 cfp:~ > ruby a.rb 42 "forty-two" "forty-two" 42.0 a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama