# Accessor methods for class methods

**URL:** <https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836>\
**Category:** Ruby\
**Created:** [September 5, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836 "2008-09-05T21:42:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [September 5, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836/1 "2008-09-05T21:42:47Z")

</div>

It seems odd that cattr\_\* (i.e. cattr\_accessor, cattr\_reader,  
cattr\_writer) is not in the native Ruby and that you have to require  
active support and rubygems to get this to work. Including the rails  
stuff appears to slow things down a little. Why is this not built in the  
native Ruby source?

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [September 5, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836/2 "2008-09-05T21:55:17Z")

</div>

El Viernes, 5 de Septiembre de 2008, Jason L. escribiÃ³:

> It seems odd that cattr\_\* (i.e. cattr\_accessor, cattr\_reader,  
> cattr\_writer) is not in the native Ruby and that you have to require  
> active support and rubygems to get this to work. Including the rails  
> stuff appears to slow things down a little. Why is this not built in the  
> native Ruby source?

Note that you can do the same using:

* * *

class MyClass

class \<\< self  
attr\_accessor :class\_attribute\_1, :class\_attribute\_2  
end

## end

So you can use it:

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [September 5, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836/3 "2008-09-05T22:00:33Z")

</div>

Hi –

On Sat, 6 Sep 2008, Jason L. wrote:

> It seems odd that cattr\_\* (i.e. cattr\_accessor, cattr\_reader,  
> cattr\_writer) is not in the native Ruby and that you have to require  
> active support and rubygems to get this to work. Including the rails  
> stuff appears to slow things down a little. Why is this not built in the  
> native Ruby source?

I can’t answer that directly, but I can tell you why I’m glad it  
isn’t. It’s partly that class variables are a bit oddball to start  
with, and I’m not eager to see them used a lot more. But it’s also the  
terminology.

An “attribute” is, or should be, an attribute of an object. But class  
variables are not object-specific; they’re very promiscuous, visible  
to a class, its instances, and all the subclasses and all their  
instances.

Therefore, a class variable is not an appropriate choice for  
representing an “attribute”. The fit between instance variables, as a  
language-level construct, and “attribute”, as a concept, is very good;  
but class variables are very different from instance variables, and  
the “attr” terminology is very loose. (The methods may have uses, but  
the names are problematic.)

David

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [September 6, 2008, 12:26am UTC](https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836/4 "2008-09-06T00:26:32Z")

</div>

> It seems odd that cattr\_\* (i.e. cattr\_accessor, cattr\_reader,  
> cattr\_writer) is not in the native Ruby

I can not answer this but personally I have stopped using class  
variables after

- i had a smallish bug which was caused by a stupid usage I did long ago  
of a class variable
- there really is not a huge need to use a class var in the first place  
(in my opinion and experience).

So personally, for me, I have found that I can live perfectly happy  
without class vars, and in these times, whenever I can achieve the same  
with a very simple easy route, I use it. There may be a few valid use  
cases for a class var but I honestly have never seen them as being very  
important.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [September 6, 2008, 1:05am UTC](https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836/5 "2008-09-06T01:05:59Z")

</div>

Thanks. That makes sense. I want to do what is simplest, but it seems  
without cattr things would get worse. Here is what I’m trying to do:

I have one rb file used for inputs:

require ‘rubygems’  
require ‘activesupport’  
class Input  
cattr\_reader :y  
def self.y  
@y = 2  
end  
end

\*\*except I have lots of vars instead of just :y and I’m assigning them  
values here.

Then I go to a new rb file and do this:

require ‘firstfile.rb’  
class NewClass  
def do\_something  
Input.y \* 2.3  
end  
end

new = NewClass.new  
p new.do\_something

\*\*The fact that I need to say “Input.y” seems very bulky. Is there a  
better way to do this? Plus, if I get away from class vars, it will get  
bulkier. any ideas?

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [September 6, 2008, 1:20am UTC](https://www.ruby-forum.com/t/accessor-methods-for-class-methods/147836/6 "2008-09-06T01:20:43Z")

</div>

HI –

On Sat, 6 Sep 2008, Jason L. wrote:

> def self.y  
> class NewClass  
> bulkier. any ideas?  
> You’re not actually using any class variables in your example. If you  
> delete the cattr\_reader line, the code still runs. You’re writing a  
> “reader” method (Input.y) by hand, and it’s using an instance  
> variable, not a class variable (an instance variable belonging to the  
> class Input itself).

David
