# Dynamically define attr\_accessors on objects?

**URL:** <https://rubytalk.org/t/dynamically-define-attr-accessors-on-objects/38614>\
**Category:** ruby-talk\
**Created:** [25 June 2007 16:23 UTC](https://rubytalk.org/t/dynamically-define-attr-accessors-on-objects/38614 "2007-06-25T16:23:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [25 June 2007 16:23 UTC](https://rubytalk.org/t/dynamically-define-attr-accessors-on-objects/38614/1 "2007-06-25T16:23:44Z")

</div>

How can I dynamically define attr\_accessors on an object?

EX:;:  
class Person  
&nbsp;&nbsp;attr\_accessor :firstname  
end  
p = Person.new  
p.firstname = 'aaron'  
p.instance\_variable\_set(:@lastname, 'smith')  
puts p.inspect  
puts p.firstname  
puts p.lastname

the last line generates an "undefined method lastname" error..

any ideas?  
thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [25 June 2007 16:28 UTC](https://rubytalk.org/t/dynamically-define-attr-accessors-on-objects/38614/2 "2007-06-25T16:28:46Z")

</div>

> How can I dynamically define attr\_accessors on an object?
> 
> EX:;:  
> class Person  
> &nbsp;&nbsp;attr\_accessor :firstname  
> end  
> p = Person.new  
> p.firstname = 'aaron'

class \<\< p  
&nbsp;&nbsp;attr\_accessor :lastname  
end  
p.lastname = 'smith'

> puts p.inspect  
> puts p.firstname  
> puts p.lastname

You have to explicitly create the accessor methods; you don't get them  
automatically when setting an instance variable.

-mental

> **···**
>
> On Tue, 26 Jun 2007 01:23:44 +0900, Aaron Smith \<beingthexemplary@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 June 2007 17:30 UTC](https://rubytalk.org/t/dynamically-define-attr-accessors-on-objects/38614/3 "2007-06-25T17:30:04Z")

</div>

Use OpenStruct.

&nbsp;&nbsp;robert

> **···**
>
> On 25.06.2007 18:23, Aaron Smith wrote:
> 
> > How can I dynamically define attr\_accessors on an object?
> > 
> > EX:;:  
> > class Person  
> > &nbsp;&nbsp;attr\_accessor :firstname  
> > end  
> > p = Person.new  
> > p.firstname = 'aaron'  
> > p.instance\_variable\_set(:@lastname, 'smith')  
> > puts p.inspect  
> > puts p.firstname  
> > puts p.lastname
> > 
> > the last line generates an "undefined method lastname" error..
> > 
> > any ideas?

---

<div class="post-metadata">

**Author:** ![Phil\_Tomson1](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil\_Tomson1](https://rubytalk.org/u/Phil_Tomson1)\
**Post date:** [26 June 2007 00:53 UTC](https://rubytalk.org/t/dynamically-define-attr-accessors-on-objects/38614/4 "2007-06-26T00:53:11Z")

</div>

I suppose you could do something like:

&nbsp;&nbsp;class Person  
&nbsp;&nbsp;&nbsp;def def\_accessor name, val=nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.class\_eval { attr\_accessor name.intern }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set ( "@#{name}".intern, val )  
&nbsp;&nbsp;&nbsp;end  
end

Then you could do something like:  
h = { "fooname"=\>"Foo", "age"=\>28 }  
h.each {|key,value| p.def\_accessor key, value }

...but the other suggestion to use OpenStruct is probably a better idea.

Phil

> **···**
>
> On 6/25/07, Aaron Smith \<beingthexemplary@gmail.com\> wrote:
> 
> > How can I dynamically define attr\_accessors on an object?
> > 
> > EX:;:  
> > class Person  
> > attr\_accessor :firstname  
> > end  
> > p = Person.new  
> > p.firstname = 'aaron'  
> > p.instance\_variable\_set(:@lastname, 'smith')  
> > puts p.inspect  
> > puts p.firstname  
> > puts p.lastname
> > 
> > the last line generates an "undefined method lastname" error..
> > 
> > any ideas?  
> > thanks
> > 
> > --

---

<div class="post-metadata">

**Author:** ![Aaron\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Aaron\_Smith](https://rubytalk.org/u/Aaron_Smith)\
**Post date:** [26 June 2007 03:32 UTC](https://rubytalk.org/t/dynamically-define-attr-accessors-on-objects/38614/5 "2007-06-26T03:32:06Z")

</div>

thanks for the suggestions.. went with the openstruct.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
