# Where and how is defined "attr\_accessor"?

**URL:** <https://www.ruby-forum.com/t/where-and-how-is-defined-attr-accessor/136238>\
**Category:** Ruby\
**Created:** [April 30, 2008, 1:04am UTC](https://www.ruby-forum.com/t/where-and-how-is-defined-attr-accessor/136238 "2008-04-30T01:04:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 30, 2008, 1:04am UTC](https://www.ruby-forum.com/t/where-and-how-is-defined-attr-accessor/136238/1 "2008-04-30T01:04:43Z")

</div>

Hi, I’ve done some custom “field\_accessor” (similar to “attr\_accessor”  
but  
with some neccessary difference). It works perfectly if I  
defined “field\_accessor” in the same class:

class Header  
def self.field\_accessor(name)  
module\_eval %{ def #{name}() … end }  
end  
end

class From \< Header  
field\_accessor :user, :domain  
end

But I’d like to have field\_accessor definition out of “Header” class,  
maybe in  
a module but don’t get it working. If I do:

module FieldAccessor  
module\_eval %{ def #{name}() … end }  
end

class From  
include FieldAccessor  
field\_accessor :user, :domain  
end

Then I get:  
NoMethodError: undefined method `field\_accessor’ for From:Class

Later I’ve tryed with the following and it seems to work:

module FieldAccessor  
module\_eval %{ def #{name}() … end }  
# It also works with “class\_eval”.  
end

class From  
extend FieldAccessor  
field\_accessor :user, :domain  
end

So, while I was writting this mail I found the solution, but would like  
to  
know if it’s the correct way. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 30, 2008, 1:16am UTC](https://www.ruby-forum.com/t/where-and-how-is-defined-attr-accessor/136238/2 "2008-04-30T01:16:24Z")

</div>

Hi –

On Wed, 30 Apr 2008, IÃ±aki Baz C. wrote:

> class From \< Header
> 
> Later I’ve tryed with the following and it seems to work:
> 
> module FieldAccessor  
> module\_eval %{ def #{name}() … end }  
> # It also works with “class\_eval”.

module\_eval and class\_eval are the same method. Here’s where those two  
names are bound to the method:

rb\_define\_method(rb\_cModule, “module\_eval”, rb\_mod\_module\_eval, -1);  
rb\_define\_method(rb\_cModule, “class\_eval”, rb\_mod\_module\_eval, -1);

> know if it’s the correct way. Thanks a lot.  
> The last two definitions of FieldAccessor look wrong; you haven’t  
> defined the field\_accessor method. But assuming that you correct that,  
> then yes, extending your class is the best way.

To answer the question in your subject line: attr\_accessor is an  
instance method of Module. That’s why all modules and classes can call  
it. You could add field\_accessor to Module, but extend is cleaner.

David

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 30, 2008, 1:27am UTC](https://www.ruby-forum.com/t/where-and-how-is-defined-attr-accessor/136238/3 "2008-04-30T01:27:31Z")

</div>

El MiÃ©rcoles, 30 de Abril de 2008, David A. Black escribiÃ³:

> The last two definitions of FieldAccessor look wrong; you haven’t  
> defined the field\_accessor method. But assuming that you correct that,  
> then yes, extending your class is the best way.

Yes, sorry, what I did is:

```
    module FieldAccessor
def self.field_accessor(name)
              module_eval %{ def #{name}() .... end }
              # It also works with "class_eval".
end
    end

    class From
            extend FieldAccessor
            field_accessor :user, :domain
    end

```

> To answer the question in your subject line: attr\_accessor is an  
> instance method of Module. That’s why all modules and classes can call  
> it. You could add field\_accessor to Module, but extend is cleaner.

Nice to know, so I was in the good way 😉

Thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 1, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/where-and-how-is-defined-attr-accessor/136238/4 "2008-05-01T16:10:56Z")

</div>

On Tue, 29 Apr 2008 18:26:39 -0500, IÃ±aki Baz C. wrote:

> El MiÃ©rcoles, 30 de Abril de 2008, David A. Black escribiÃ³:
> 
> > The last two definitions of FieldAccessor look wrong; you haven’t  
> > defined the field\_accessor method. But assuming that you correct that,  
> > then yes, extending your class is the best way.

Another reasonable place is in class Module. See Ruby Q. #67:  
Metakoans, which involved creating an attr\_accessor like method.  
[http://www.rubyquiz.com/quiz67.html](http://www.rubyquiz.com/quiz67.html)

–Ken
