# Attr\_reader + attr\_writer?

**URL:** <https://rubytalk.org/t/attr-reader-attr-writer/31853>\
**Category:** ruby-talk\
**Created:** [13 October 2006 00:31 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853 "2006-10-13T00:31:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Ruby](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Joe\_Ruby](https://rubytalk.org/u/Joe_Ruby)\
**Post date:** [13 October 2006 00:31 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853/1 "2006-10-13T00:31:58Z")

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

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

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [13 October 2006 00:34 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853/2 "2006-10-13T00:34:12Z")

</div>

class Foo  
attr\_accessor :bar  
end

foo = Foo.new

foo.bar = 7  
puts foo.bar

=\> 7

Kirk Haines

> **···**
>
> On Fri, 13 Oct 2006, Joe Ruby 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.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Woertink](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@Jeremy\_Woertink](https://rubytalk.org/u/Jeremy_Woertink)\
**Post date:** [13 October 2006 01:03 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853/3 "2006-10-13T01:03:10Z")

</div>

Joe Ruby 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?

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

---

<div class="post-metadata">

**Author:** ![jyzhang](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jyzhang](https://rubytalk.org/u/jyzhang)\
**Post date:** [13 October 2006 08:52 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853/4 "2006-10-13T08:52:35Z")

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

Jeremy Woertink wrote:

> **···**
>
> > Joe Ruby 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?
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [13 October 2006 12:35 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853/5 "2006-10-13T12:35:37Z")

</div>

Jeremy Woertink wrote:

> Joe Ruby 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. 😉

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 October 2006 12:31 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853/6 "2006-10-13T12:31:41Z")

</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
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David A. Black | dblack@wobblini.net  
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [13 October 2006 15:25 UTC](https://rubytalk.org/t/attr-reader-attr-writer/31853/7 "2006-10-13T15:25:10Z")

</div>

dblack@wobblini.net wrote:

> HI --
> 
> \> 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.

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.

> **···**
>
> > On Fri, 13 Oct 2006, jyzhang wrote:
