# Attr\_read, attr\_writer, etc

**URL:** <https://rubytalk.org/t/attr-read-attr-writer-etc/39863>\
**Category:** ruby-talk\
**Created:** [13 August 2007 14:35 UTC](https://rubytalk.org/t/attr-read-attr-writer-etc/39863 "2007-08-13T14:35:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Meyer](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@Frank\_Meyer](https://rubytalk.org/u/Frank_Meyer)\
**Post date:** [13 August 2007 14:35 UTC](https://rubytalk.org/t/attr-read-attr-writer-etc/39863/1 "2007-08-13T14:35:36Z")

</div>

Hello,  
are these keywords or are they methods which create get/set methods for  
the given attribute?

I suppose it's a mix of both, because I tried to write my own  
attr\_accessor but I couldn't call it in the class definition, only on  
an existing object.

Turing.

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

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [13 August 2007 14:51 UTC](https://rubytalk.org/t/attr-read-attr-writer-etc/39863/2 "2007-08-13T14:51:39Z")

</div>

Alle lunedì 13 agosto 2007, Frank Meyer ha scritto:

> Hello,  
> are these keywords or are they methods which create get/set methods for  
> the given attribute?
> 
> I suppose it's a mix of both, because I tried to write my own  
> attr\_accessor but I couldn't call it in the class definition, only on  
> an existing object.
> 
> Turing.

They're instance method of the Module class. You can write them, if you want:

class Module  
&nbsp;&nbsp;  
&nbsp;&nbsp;def my\_attr\_reader \*args  
&nbsp;&nbsp;&nbsp;&nbsp;args.each do |a|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(a) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'this method was generated by my\_attr\_reader';  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_get("@#{a}")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

end

class C  
&nbsp;&nbsp;my\_attr\_reader :x

&nbsp;&nbsp;def initialize x  
&nbsp;&nbsp;&nbsp;&nbsp;@x = x  
&nbsp;&nbsp;end  
end

res = C.new(2).x  
=\> this method was generated by my\_attr\_reader  
puts res  
=\> 2

Defining my\_attr\_reader in class Module will make it availlable in all classes  
and modules. You can define it in class Class to make it availlable only in  
classes, or you can define them in a single class. In this case, however, it  
should be defined as a class method, not as an instance method:

class MyClass  
&nbsp;&nbsp;def self.my\_attr\_reader  
&nbsp;&nbsp;...

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Frank\_Meyer](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@Frank\_Meyer](https://rubytalk.org/u/Frank_Meyer)\
**Post date:** [13 August 2007 18:03 UTC](https://rubytalk.org/t/attr-read-attr-writer-etc/39863/3 "2007-08-13T18:03:13Z")

</div>

Thanks for your explanation, but how would you write a attr\_writer  
method?  
I don't know how I can pass a parameter list to define\_method.

Turing.

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [13 August 2007 18:24 UTC](https://rubytalk.org/t/attr-read-attr-writer-etc/39863/4 "2007-08-13T18:24:33Z")

</div>

Hi --

> Thanks for your explanation, but how would you write a attr\_writer  
> method?  
> I don't know how I can pass a parameter list to define\_method.

Give the block a parameter:

&nbsp;&nbsp;&nbsp;def my\_attr\_writer(\*names)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;names.each do |name|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method("#{name}=") {|x| instance\_variable\_set("@#{name}", x) }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

David

> **···**
>
> On Tue, 14 Aug 2007, Frank Meyer wrote:
> 
> --  
> \* Books:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
