# Attr\_reader + attr\_writer?

**URL:** <https://www.ruby-forum.com/t/attr-reader-attr-writer/71983>\
**Category:** Ruby\
**Created:** [October 13, 2006, 2:31am UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983 "2006-10-13T02:31:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 13, 2006, 2:31am UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983/1 "2006-10-13T02:31:56Z")

</div>

Is there a class method that combines attr\_reader and attr\_writer? I  
thought I remember seeing some “cattr\_\*” method, but don’t know what  
that was.

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 13, 2006, 3:03am UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983/2 "2006-10-13T03:03:06Z")

</div>

Joe R. MUDCRAP-CE wrote:

> Is there a class method that combines attr\_reader and attr\_writer? I  
> thought I remember seeing some “cattr\_\*” method, but don’t know what  
> that was.
> 
> Thanks,  
> Joe

I’m still new to this whole ruby and programming stuff, but doesn’t  
attr\_accessor do both of those?

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 13, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983/3 "2006-10-13T14:35:31Z")

</div>

Jeremy W. wrote:

> Joe R. MUDCRAP-CE wrote:
> 
> > Is there a class method that combines attr\_reader and attr\_writer? I  
> > thought I remember seeing some “cattr\_\*” method, but don’t know what  
> > that was.
> > 
> > Thanks,  
> > Joe
> 
> I’m still new to this whole ruby and programming stuff, but doesn’t  
> attr\_accessor do both of those?

yes, attr\_accessor does this.

I was also toying with an attr\_reader that allows ? for  
read-vars … ruby triggers crazy ideas. A haskell guy once said  
that he liked ? at end of method names in Ruby. 😉

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983/4 "2006-10-18T10:39:37Z")

</div>

On Fri, 13 Oct 2006, Joe R. MUDCRAP-CE wrote:

> Is there a class method that combines attr\_reader and attr\_writer? I  
> thought I remember seeing some “cattr\_\*” method, but don’t know what  
> that was.

class Foo  
attr\_accessor :bar  
end

foo = Foo.new

foo.bar = 7  
puts foo.bar

=\> 7

Kirk H.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983/5 "2006-10-18T10:40:47Z")

</div>

HI –

On Fri, 13 Oct 2006, jyzhang wrote:

> attr\_accessor defines both a writer and a reader
> 
> cattr\_\* is for class variables… you know… the kind that looks like  
> @@var

Ruby doesn’t have any cattr\_\* methods, though. Also, if you write a  
wrapper around class variables, it would be better to call them  
something else, as the “attr” in “cattr” implies that you’re dealing  
with an object attribute. Since class variables are shared by many  
objects, they’re not actually representing attributes.

David

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983/6 "2006-10-18T10:40:27Z")

</div>

attr\_accessor defines both a writer and a reader

cattr\_\* is for class variables… you know… the kind that looks like  
@@var

James Zhang

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/attr-reader-attr-writer/71983/7 "2006-10-18T10:44:01Z")

</div>

removed\_email\_address@domain.invalid wrote:

> wrapper around class variables, it would be better to call them  
> something else, as the “attr” in “cattr” implies that you’re dealing  
> with an object attribute. Since class variables are shared by many  
> objects, they’re not actually representing attributes.

To be clear cattr methods create both instance level and class level  
accessors using @@vars for storage. What would be a better name then  
class attribute?

T.
