# Attr\_read, attr\_writer, etc

**URL:** <https://www.ruby-forum.com/t/attr-read-attr-writer-etc/111929>\
**Category:** Ruby\
**Created:** [August 13, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/attr-read-attr-writer-etc/111929 "2007-08-13T16:29:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dizzyone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dizzyone](https://www.ruby-forum.com/u/dizzyone)\
**Post date:** [August 13, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/attr-read-attr-writer-etc/111929/1 "2007-08-13T16:29:52Z")

</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.

---

<div class="post-metadata">

**Author:** ![dizzyone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dizzyone](https://www.ruby-forum.com/u/dizzyone)\
**Post date:** [August 13, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/attr-read-attr-writer-etc/111929/2 "2007-08-13T20:03:16Z")

</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.

---

<div class="post-metadata">

**Author:** ![dizzyone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dizzyone](https://www.ruby-forum.com/u/dizzyone)\
**Post date:** [August 13, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/attr-read-attr-writer-etc/111929/3 "2007-08-13T16:53:22Z")

</div>

Alle lunedÃ¬ 13 agosto 2007, Frank M. ha scritto:

> Turing.

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

class Module

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

end

class C  
my\_attr\_reader :x

def initialize x  
@x = x  
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  
def self.my\_attr\_reader  
…

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![dizzyone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dizzyone](https://www.ruby-forum.com/u/dizzyone)\
**Post date:** [August 13, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/attr-read-attr-writer-etc/111929/4 "2007-08-13T21:07:50Z")

</div>

Hi –

On Tue, 14 Aug 2007, Frank M. wrote:

> 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:

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

David
