# Defining a Class Accessor

**URL:** <https://rubytalk.org/t/defining-a-class-accessor/17277>\
**Category:** ruby-talk\
**Created:** [30 March 2005 14:53 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277 "2005-03-30T14:53:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [30 March 2005 14:53 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/1 "2005-03-30T14:53:25Z")

</div>

I was playing around with an idea in another thread and ran into a surprise. Can anyone explain the following to me?

irb(main):015:0\> class Module  
irb(main):016:1\> def cattr\_accessor( \*symbols )  
irb(main):017:2\> symbols.each do |sym|  
irb(main):018:3\* eval "def self.#{sym}( ) @@#{sym} end  
irb(main):019:3" def self.#{sym}=( value ) @@#{sym} = value end"  
irb(main):020:3\> end  
irb(main):021:2\> end  
irb(main):022:1\> end  
=\> nil  
irb(main):023:0\> class Accessor  
irb(main):024:1\> cattr\_accessor :one, :two  
irb(main):025:1\> def self.fetch\_one( )  
irb(main):026:2\> @@one  
irb(main):027:2\> end  
irb(main):028:1\> end  
=\> nil  
irb(main):029:0\> Accessor.one = "James"  
=\> "James"  
irb(main):030:0\> Accessor.two = "Gray"  
=\> "Gray"  
irb(main):031:0\> Accessor.one  
=\> "James"  
irb(main):032:0\> Accessor.two  
=\> "Gray"  
irb(main):033:0\> Accessor.fetch\_one  
NameError: uninitialized class variable @@one in Accessor  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):26:in `fetch\_one'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):33  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (null):0

Everything works as expected, save fetch\_one(). Why does it not return "James"?

Thanks.

James Edward Gray II

---

<div class="post-metadata">

**Author:** ![Martin\_Ankerl](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Martin\_Ankerl](https://rubytalk.org/u/Martin_Ankerl)\
**Post date:** [30 March 2005 15:14 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/2 "2005-03-30T15:14:45Z")

</div>

> Everything works as expected, save fetch\_one(). Why does it not return "James"?

The problem is that the class variables are defined for Module, and not for the class Accessor:

class Module  
&nbsp;&nbsp;&nbsp;def cattr\_accessor( \*symbols )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;symbols.each do |sym|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "def self.#{sym}() @@#{sym} end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.#{sym}=(value) @@#{sym} = value end"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;&nbsp;cattr\_accessor :one  
end

class B  
&nbsp;&nbsp;&nbsp;cattr\_accessor :one  
end

A.one = 1; p A.one # prints 1  
B.one = 42; p A.one # prints 42!

martinus

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [30 March 2005 15:20 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/3 "2005-03-30T15:20:08Z")

</div>

I have been playing with this myself, your @@one is in Module not in  
Accessor -- try changing your eval to class\_eval:

class Module  
&nbsp;&nbsp;def cattr\_accessor( \*symbols )  
&nbsp;&nbsp;&nbsp;&nbsp;symbols.each do |sym|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class\_eval \<\<EVAL  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.#{sym}()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@#{sym}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.#{sym}=( value )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@#{sym} = value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
EVAL  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

> **···**
>
> On Wed, 30 Mar 2005 23:53:25 +0900, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > I was playing around with an idea in another thread and ran into a  
> > surprise. Can anyone explain the following to me?
> > 
> > irb(main):015:0\> class Module  
> > irb(main):016:1\> def cattr\_accessor( \*symbols )  
> > irb(main):017:2\> symbols.each do |sym|  
> > irb(main):018:3\* eval "def self.#{sym}( ) @@#{sym} end  
> > irb(main):019:3" def self.#{sym}=( value ) @@#{sym} =  
> > value end"  
> > irb(main):020:3\> end  
> > irb(main):021:2\> end  
> > irb(main):022:1\> end  
> > =\> nil  
> > irb(main):023:0\> class Accessor  
> > irb(main):024:1\> cattr\_accessor :one, :two  
> > irb(main):025:1\> def self.fetch\_one( )  
> > irb(main):026:2\> @@one  
> > irb(main):027:2\> end  
> > irb(main):028:1\> end  
> > =\> nil  
> > irb(main):029:0\> Accessor.one = "James"  
> > =\> "James"  
> > irb(main):030:0\> Accessor.two = "Gray"  
> > =\> "Gray"  
> > irb(main):031:0\> Accessor.one  
> > =\> "James"  
> > irb(main):032:0\> Accessor.two  
> > =\> "Gray"  
> > irb(main):033:0\> Accessor.fetch\_one  
> > NameError: uninitialized class variable @@one in Accessor  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):26:in `fetch\_one'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):33  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (null):0
> > 
> > Everything works as expected, save fetch\_one(). Why does it not return  
> > "James"?
> > 
> > Thanks.
> > 
> > James Edward Gray II

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [30 March 2005 15:29 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/4 "2005-03-30T15:29:45Z")

</div>

James Edward Gray II wrote:

> I was playing around with an idea in another thread and ran into a surprise. Can anyone explain the following to me?
> 
> irb(main):015:0\> class Module  
> irb(main):016:1\> def cattr\_accessor( \*symbols )  
> irb(main):017:2\> symbols.each do |sym|  
> irb(main):018:3\* eval "def self.#{sym}( ) @@#{sym} end  
> irb(main):019:3" def self.#{sym}=( value ) @@#{sym} = value end"  
> irb(main):020:3\> end  
> irb(main):021:2\> end  
> irb(main):022:1\> end

Do you really need to use @@variables? From my experience they tend to cause trouble.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [30 March 2005 15:44 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/5 "2005-03-30T15:44:45Z")

</div>

"James Edward Gray II" \<james@grayproductions.net\> schrieb im Newsbeitrag news:07549561ed849e497e65fdc423a92c88@grayproductions.net...

> I was playing around with an idea in another thread and ran into a surprise. Can anyone explain the following to me?
> 
> irb(main):015:0\> class Module  
> irb(main):016:1\> def cattr\_accessor( \*symbols )  
> irb(main):017:2\> symbols.each do |sym|  
> irb(main):018:3\* eval "def self.#{sym}( ) @@#{sym} end  
> irb(main):019:3" def self.#{sym}=( value ) @@#{sym} = value end"  
> irb(main):020:3\> end  
> irb(main):021:2\> end  
> irb(main):022:1\> end  
> =\> nil  
> irb(main):023:0\> class Accessor  
> irb(main):024:1\> cattr\_accessor :one, :two  
> irb(main):025:1\> def self.fetch\_one( )  
> irb(main):026:2\> @@one  
> irb(main):027:2\> end  
> irb(main):028:1\> end  
> =\> nil  
> irb(main):029:0\> Accessor.one = "James"  
> =\> "James"  
> irb(main):030:0\> Accessor.two = "Gray"  
> =\> "Gray"  
> irb(main):031:0\> Accessor.one  
> =\> "James"  
> irb(main):032:0\> Accessor.two  
> =\> "Gray"  
> irb(main):033:0\> Accessor.fetch\_one  
> NameError: uninitialized class variable @@one in Accessor  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):26:in `fetch\_one'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):33  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (null):0
> 
> Everything works as expected, save fetch\_one(). Why does it not return "James"?

I'm a bit stunned, too. Maybe this is a hint:

?\> Accessor.class\_variables  
=\>

> > Module.class\_variables

=\> ["@@one", "@@two"]

Although:

> > Accessor.class.ancestors

=\> [Class, Module, Object, Kernel]

So, since Accessor is an instance of Class, both should be accessible IMO. Weired...

For me it's one more reason to never use class variables. They are simply too awkward.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

PS: But I'm curios, too, why this does not work.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [30 March 2005 17:13 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/6 "2005-03-30T17:13:59Z")

</div>

Makes sense. Thank you.

James Edward Gray II

> **···**
>
> On Mar 30, 2005, at 9:20 AM, Patrick Hurley wrote:
> 
> > I have been playing with this myself, your @@one is in Module not in  
> > Accessor -- try changing your eval to class\_eval:
> > 
> > class Module  
> > &nbsp;&nbsp;def cattr\_accessor( \*symbols )  
> > &nbsp;&nbsp;&nbsp;&nbsp;symbols.each do |sym|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class\_eval \<\<EVAL  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.#{sym}()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@#{sym}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.#{sym}=( value )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@#{sym} = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > EVAL  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [30 March 2005 17:20 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/7 "2005-03-30T17:20:24Z")

</div>

> I'm a bit stunned, too. Maybe this is a hint:
> 
> ?\> Accessor.class\_variables  
> =\>
> 
> > > Module.class\_variables
> 
> =\> ["@@one", "@@two"]
> 
> Although:
> 
> > > Accessor.class.ancestors
> 
> =\> [Class, Module, Object, Kernel]

Right, because I put the code in Module. It makes sense, when you let it sink in.

> So, since Accessor is an instance of Class, both should be accessible IMO. Weired...
> 
> For me it's one more reason to never use class variables. They are simply too awkward.
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;robert
> 
> PS: But I'm curios, too, why this does not work.

Just switching to class\_eval(), to make sure the code is executed in the right context fixes the issue.

James

> **···**
>
> On Mar 30, 2005, at 9:44 AM, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [30 March 2005 17:47 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/8 "2005-03-30T17:47:18Z")

</div>

Could you explain why they would cause trouble?

Thanks,  
Navin.

> **···**
>
> Florian Gross \<flgr@ccan.de\> wrote:
> 
> > Do you really need to use @@variables? From my experience they tend to  
> > cause trouble.

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [30 March 2005 17:44 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/9 "2005-03-30T17:44:30Z")

</div>

Not a problem, I feel like less of a ruby newbie today 🙂

> **···**
>
> On Thu, 31 Mar 2005 02:13:59 +0900, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > Makes sense. Thank you.

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [30 March 2005 19:54 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/10 "2005-03-30T19:54:44Z")

</div>

Navindra Umanee wrote:

> > Do you really need to use @@variables? From my experience they tend to cause trouble.
> 
> Could you explain why they would cause trouble?

I thought the original post would answer that well enough already, but in my own Ruby experience I have so far being surprised when using them. Why should they be used when you can use far simpler constructs with clearer rules?

Generally they seem to be used most as a quick replacement for instance variables when something goes wrong without further questioning or actually finding the root of the problem. I've seen them used quite frequently in this case:

module StackMixIn  
&nbsp;&nbsp;&nbsp;def push(item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@items \<\< item  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def pop()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@items.pop  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;# I'd like to initialize @items, but this will not work:  
&nbsp;&nbsp;&nbsp;@items =

&nbsp;&nbsp;&nbsp;# If I just replace every '@items' with '@@items' it will magically  
&nbsp;&nbsp;&nbsp;# work. It's an obvious fix. It must be in Ruby exactly as a fix for  
&nbsp;&nbsp;&nbsp;# this kind of situation.  
end

(And yes, I know that they are going to be simplified in Rite, but I'm not sure if that is going to make them completely non-surprising to most people...)

> **···**
>
> > Florian Gross \<flgr@ccan.de\> wrote:

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [30 March 2005 23:22 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/11 "2005-03-30T23:22:26Z")

</div>

> I thought the original post would answer that well enough already, but

Well, someone clarified what the problem was... he was assigning to  
the class variables for Module instead of for the class itself. I  
guess "self" is what is confusing.

> in my own Ruby experience I have so far being surprised when using them.  
> Why should they be used when you can use far simpler constructs with  
> clearer rules?

Well, the idea is to have shared storage for all objects of a  
particular class, right? Class variables fit exactly that  
requirement. I'm not sure which simpler constructs you're referring  
to in this context... but perhaps I'm still too much in the Java  
mindset.

Cheers,  
Navin.

> **···**
>
> Florian Gross \<flgr@ccan.de\> wrote:

---

<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:** [30 March 2005 23:41 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/12 "2005-03-30T23:41:22Z")

</div>

Hi --

> **···**
>
> On Thu, 31 Mar 2005, Navindra Umanee wrote:
> 
> > Florian Gross \<flgr@ccan.de\> wrote:
> > 
> > > I thought the original post would answer that well enough already, but
> > 
> > Well, someone clarified what the problem was... he was assigning to  
> > the class variables for Module instead of for the class itself. I  
> > guess "self" is what is confusing.
> > 
> > > in my own Ruby experience I have so far being surprised when using them.  
> > > Why should they be used when you can use far simpler constructs with  
> > > clearer rules?
> > 
> > Well, the idea is to have shared storage for all objects of a  
> > particular class, right? Class variables fit exactly that  
> > requirement.
> 
> As long as you add "... and possibly objects of another class" 🙂  
> It's that inheritance thing, at least in large part, that causes  
> people to find class variables awkward.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [31 March 2005 00:14 UTC](https://rubytalk.org/t/defining-a-class-accessor/17277/13 "2005-03-31T00:14:44Z")

</div>

Navindra Umanee wrote:

> Well, the idea is to have shared storage for all objects of a  
> particular class, right?

Which can be done like this, anyway:

class Foo  
&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :space  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def method()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use self.class.space  
&nbsp;&nbsp;&nbsp;end  
end

What I dislike about @@vars is that they behave differently depending on what context you are in -- method vs. class/module definition scope.
