# Attr\_accessor

**URL:** <https://rubytalk.org/t/attr-accessor/73989>\
**Category:** ruby-talk\
**Created:** [9 January 2017 10:30 UTC](https://rubytalk.org/t/attr-accessor/73989 "2017-01-09T10:30:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [9 January 2017 10:30 UTC](https://rubytalk.org/t/attr-accessor/73989/1 "2017-01-09T10:30:17Z")

</div>

Hi  
\* is there an attr\_accessor for class methods?

\* how can I use attr\_accessor in initialize() ?  
(The args to attr\_accessor are given when calling it)

\* How can I see (with ruby-code) from which object/module the attr\_accessor method comes (is implemented in) ?

thank yoiu  
Opti

---

<div class="post-metadata">

**Author:** ![Andy\_Jones](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/andy_jones/32/1829_2.png) [@Andy\_Jones](https://rubytalk.org/u/Andy_Jones)\
**Post date:** [9 January 2017 11:05 UTC](https://rubytalk.org/t/attr-accessor/73989/2 "2017-01-09T11:05:27Z")

</div>

> \* is there an attr\_accessor for class methods?

Depends what you mean. attr\_accessor works with instance attributes. What do you consider a class attribute?

You can certainly do this:

&nbsp;&nbsp;&nbsp;&nbsp;class Foo

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;end

But what you will be getting and setting is the instance attribute of the eigenclass(?) and certainly not a @@bar class attribute. (At the Foo instance level you would access it using `myfoo.class.bar`.)

On the other hand, since class attributes are rather compromised, this might arguably be better...

On the third hand, however -- any complex structure on the class itself should really live in a separate object; it's considered by some to be a code smell. So maybe a rethink might be in order.

Click here to view Company Information and Confidentiality Notice.\<[http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt](http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt);

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [9 January 2017 15:12 UTC](https://rubytalk.org/t/attr-accessor/73989/3 "2017-01-09T15:12:20Z")

</div>

> \* is there an attr\_accessor for class methods?

This question doesn't make much sense to me...  
attr\_accessor is used with instance variables.  
Instance variables cannot be accessed from class methods.

What is the "attr\_accessor for class methods"  
supposed to do?

I suspect you are not very clear about basic concepts  
(classes, instances, instance variables, constants,  
instance vs. class methods, ...) and would suggest you  
read some introductory tutorials on these topics and  
how to create classes in Ruby.

> \* how can I use attr\_accessor in initialize() ?  
> (The args to attr\_accessor are given when calling it)

Are you sure you need to dynamically create instance variables?  
Maybe you could simply use a hash internally (@attributes, @fields),  
and use the arguments in initialize as hash keys.  
You could define # and #= methods to access those fields.

BTW: please use more descriptive subject lines.

Regards,  
Marcus

> **···**
>
> Am 09.01.2017 um 11:30 schrieb Die Optimisten:
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A
