# Unable to work with class variable

**URL:** <https://rubytalk.org/t/unable-to-work-with-class-variable/66417>\
**Category:** ruby-talk\
**Created:** [4 October 2012 13:37 UTC](https://rubytalk.org/t/unable-to-work-with-class-variable/66417 "2012-10-04T13:37:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 October 2012 13:37 UTC](https://rubytalk.org/t/unable-to-work-with-class-variable/66417/1 "2012-10-04T13:37:23Z")

</div>

Here is my code:  
class Test  
&nbsp;&nbsp;@@fields="ok"

&nbsp;&nbsp;attr\_accessor :fields

&nbsp;&nbsp;#initialize attributes.  
&nbsp;&nbsp;def initialize()  
&nbsp;&nbsp;end  
end  
a= Test.fields

It says:  
test.rb:11: undefined method `fields' for Test:Class (NoMethodError)

Why? even I have mentioned attr\_accessor.

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

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 October 2012 13:44 UTC](https://rubytalk.org/t/unable-to-work-with-class-variable/66417/2 "2012-10-04T13:44:03Z")

</div>

Hi,

Calling attr\_accessor defines getters and setter for the \*instances\* of  
the class, not the class itself. I'm also pretty sure that you want in  
an class instance variable and not a class variable. Then you can simply  
call attr\_accessor in the singleton class of Test:

class Test  
&nbsp;&nbsp;@fields = 'ok'  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :fields  
&nbsp;&nbsp;end  
end

p Test.fields

If you actually do want a class variable (and know what you're doing),  
you have to define the getters and setters by hand. There's is no  
ready-made method for this:

class Test  
&nbsp;&nbsp;@@fields = 'ok'  
&nbsp;&nbsp;def self.fields  
&nbsp;&nbsp;&nbsp;&nbsp;@@fields  
&nbsp;&nbsp;end  
end

p Test.fields

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

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 October 2012 06:03 UTC](https://rubytalk.org/t/unable-to-work-with-class-variable/66417/3 "2012-10-05T06:03:56Z")

</div>

Thanks Jan,  
Its great to hear such a complete and to the point answer, thank you  
again. People like you gives me confidence that, one day I can also be  
an expert of ruby!

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [5 October 2012 16:45 UTC](https://rubytalk.org/t/unable-to-work-with-class-variable/66417/4 "2012-10-05T16:45:18Z")

</div>

You still should remove class variables from your repertoire. Their  
scoping semantics is weird and - as this thread shows - they are not  
very well integrated. Better leave them alone.

Example for the weird scoping:

$ ruby /tmp/cv.rb  
B1 -1072888598: [1, 2]  
D1 -1072888598: [1, 2]  
D2 -1072888848: [2]  
B2 -1072888918: [1]  
D2 -1072888918: [1]

$ cat /tmp/cv.rb  
class B1; (@@x ||= ) \<\< 1; end  
class D1 \< B1; (@@x ||= ) \<\< 2; end

class B1; printf "B1 %12d: %p\n", @@x.object\_id, @@x end  
class D1; printf "D1 %12d: %p\n", @@x.object\_id, @@x end

class B2; end  
class D2 \< B2; (@@x ||= ) \<\< 2; end  
class D2; printf "D2 %12d: %p\n", @@x.object\_id, @@x end

class B2; (@@x ||= ) \<\< 1; end

class B2; printf "B2 %12d: %p\n", @@x.object\_id, @@x end  
class D2; printf "D2 %12d: %p\n", @@x.object\_id, @@x end

Kind regards

robert

> **···**
>
> On Fri, Oct 5, 2012 at 8:03 AM, ajay paswan \<lists@ruby-forum.com\> wrote:
> 
> > Thanks Jan,  
> > Its great to hear such a complete and to the point answer, thank you  
> > again. People like you gives me confidence that, one day I can also be  
> > an expert of ruby!
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
