# Is there a way to add attributes to instances?

**URL:** <https://rubytalk.org/t/is-there-a-way-to-add-attributes-to-instances/21392>\
**Category:** ruby-talk\
**Created:** [7 October 2005 02:36 UTC](https://rubytalk.org/t/is-there-a-way-to-add-attributes-to-instances/21392 "2005-10-07T02:36:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ako](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@ako](https://rubytalk.org/u/ako)\
**Post date:** [7 October 2005 02:36 UTC](https://rubytalk.org/t/is-there-a-way-to-add-attributes-to-instances/21392/1 "2005-10-07T02:36:51Z")

</div>

hello,

is there a way to add properties to instances and access them later  
just like if they were defined in the object's class?

a.newproperty = 123  
puts a.newproperty

i know how to dynamically add newproperty to the class of instance 'a',  
but is there a way to do it per instance? or do i have to maintain an  
explicit hash table on my objects and access these attributes through  
the table?

thanks for the help  
konstantin

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [7 October 2005 02:47 UTC](https://rubytalk.org/t/is-there-a-way-to-add-attributes-to-instances/21392/2 "2005-10-07T02:47:41Z")

</div>

Here's two different ideas to get you going:

irb(main):001:0\> require "ostruct"  
=\> true  
irb(main):002:0\> a = OpenStruct.new  
=\> \<OpenStruct\>  
irb(main):003:0\> a.new\_property = 123  
=\> 123  
irb(main):004:0\> a.property  
=\> nil

irb(main):018:0\> class Magic  
irb(main):019:1\> def method\_missing( meth, \*args, &block )  
irb(main):020:2\> if meth.to\_s =~ /^(\w+)=$/  
irb(main):021:3\> instance\_variable\_set("@#{$1}", \*args)  
irb(main):022:3\> elsif instance\_variables.include? "@#{meth}"  
irb(main):023:3\> instance\_variable\_get("@#{meth}")  
irb(main):024:3\> else  
irb(main):025:3\* super  
irb(main):026:3\> end  
irb(main):027:2\> end  
irb(main):028:1\> end  
=\> nil  
irb(main):029:0\> b = Magic.new  
=\> #\<Magic:0x1b0764\>  
irb(main):030:0\> b.new\_property = 123  
=\> 123  
irb(main):031:0\> b.new\_property  
=\> 123

Hope that helps.

James Edward Gray II

> **···**
>
> On Oct 6, 2005, at 9:36 PM, ako... wrote:
> 
> > hello,
> > 
> > is there a way to add properties to instances and access them later  
> > just like if they were defined in the object's class?
> > 
> > a.newproperty = 123  
> > puts a.newproperty
> > 
> > i know how to dynamically add newproperty to the class of instance 'a',  
> > but is there a way to do it per instance? or do i have to maintain an  
> > explicit hash table on my objects and access these attributes through  
> > the table?

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [7 October 2005 02:48 UTC](https://rubytalk.org/t/is-there-a-way-to-add-attributes-to-instances/21392/3 "2005-10-07T02:48:59Z")

</div>

ako... wrote:

> hello,
> 
> is there a way to add properties to instances and access them later  
> just like if they were defined in the object's class?
> 
> a.newproperty = 123  
> puts a.newproperty
> 
> i know how to dynamically add newproperty to the class of instance 'a',  
> but is there a way to do it per instance? or do i have to maintain an  
> explicit hash table on my objects and access these attributes through  
> the table?

class \<\<a  
&nbsp;&nbsp;&nbsp;attr\_accessor :newproperty  
end

James

> **···**
>
> --
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - The Ruby Documentation Site  
> [http://www.rubyxml.com](http://www.rubyxml.com) - News, Articles, and Listings for Ruby & XML  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys

---

<div class="post-metadata">

**Author:** ![Rob\_Rypka](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@Rob\_Rypka](https://rubytalk.org/u/Rob_Rypka)\
**Post date:** [7 October 2005 03:18 UTC](https://rubytalk.org/t/is-there-a-way-to-add-attributes-to-instances/21392/4 "2005-10-07T03:18:10Z")

</div>

I think OpenStruct is what you want to use. If you feel you have to  
implement it yourself, or it's not quite right, think about using  
method\_missing. It is perhaps the most awsomely useful concepts in  
Ruby.

> **···**
>
> On 10/6/05, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Oct 6, 2005, at 9:36 PM, ako... wrote:  
> > \> is there a way to add properties to instances and access them later  
> > \> just like if they were defined in the object's class?
> > 
> > Here's two different ideas to get you going:
> > 
> > irb(main):001:0\> require "ostruct"  
> > =\> true  
> > irb(main):002:0\> a = OpenStruct.new  
> > =\> \<OpenStruct\>  
> > irb(main):003:0\> a.new\_property = 123  
> > =\> 123  
> > irb(main):004:0\> a.property  
> > =\> nil
> 
> --  
> Rob
