# Class variable weirdness

**URL:** <https://www.ruby-forum.com/t/class-variable-weirdness/97457>\
**Category:** Ruby\
**Created:** [January 26, 2006, 4:07am UTC](https://www.ruby-forum.com/t/class-variable-weirdness/97457 "2006-01-26T04:07:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [January 26, 2006, 4:07am UTC](https://www.ruby-forum.com/t/class-variable-weirdness/97457/1 "2006-01-26T04:07:42Z")

</div>

I was fooling around with class variables tonight, and I know this is  
pretty silly, but forsome reason this code will work when in  
its own class, as you can see in class A and class B, but when I  
put the code together into class C I get an error

class\_vars.rb:26: undefined method `up=’ for C:Class (NoMethodError)

Can anyone explain why this is happening?

class A  
@@foo=1  
def self.foo=(n)  
@@foo = n  
end  
end  
#prints “10”  
puts “%s” % A.foo=10

class B  
@@foo=1  
def self.up  
@@foo+=1  
end  
end  
#prints “2”  
puts “%s” % B.up

class C  
@@foo=1  
def self.foo=(n)  
@@foo = n  
end  
def self.up  
@@foo+=1  
end  
end  
#should print “2, 10” but it doesn’t work for me.  
puts “%s, %s” % (C.up,C.foo=10)

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [January 26, 2006, 4:19am UTC](https://www.ruby-forum.com/t/class-variable-weirdness/97457/2 "2006-01-26T04:19:44Z")

</div>

Hi –

On Thu, 26 Jan 2006, Alex C. wrote:

> I was fooling around with class variables tonight, and I know this is  
> pretty silly, but forsome reason this code will work when in  
> its own class, as you can see in class A and class B, but when I  
> put the code together into class C I get an error
> 
> class\_vars.rb:26: undefined method `up=’ for C:Class (NoMethodError)
> 
> Can anyone explain why this is happening?  
> […]  
> puts “%s, %s” % (C.up,C.foo=10)

You need an array there: [C.up, C.foo=10]. The way you’ve got it,  
what’s happening is that it’s being parsed as two assignments:

C.up = 10  
C.foo = nil

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming May 1, 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [January 26, 2006, 5:22am UTC](https://www.ruby-forum.com/t/class-variable-weirdness/97457/3 "2006-01-26T05:22:57Z")

</div>

hi David

On 1/25/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Can anyone explain why this is happening?  
> > […]  
> > puts “%s, %s” % (C.up,C.foo=10)
> 
> You need an array there: [C.up, C.foo=10]. The way you’ve got it,  
> what’s happening is that it’s being parsed as two assignments:
> 
> C.up = 10  
> C.foo = nil

Thanks I appreciate the quick reply, I think the () was a bit of  
python showing its roots.  
What I would like to do now is get instances of those classes and use  
the  
methods that I’ve defined but I cant figure out how, without changing  
the class.

class A  
@@foo=1  
def self.foo=(n)  
@@foo = n  
end  
end  
puts “%s” % A.foo=10  
a = A.new  
#this does not work for an instance  
#puts “%s” % a.foo=10

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [January 26, 2006, 5:35am UTC](https://www.ruby-forum.com/t/class-variable-weirdness/97457/4 "2006-01-26T05:35:06Z")

</div>

Hi–

On Thu, 26 Jan 2006, Alex C. wrote:

> > C.up = 10  
> > def self.foo=(n)  
> > @@foo = n  
> > end  
> > end  
> > puts “%s” % A.foo=10  
> > a = A.new  
> > #this does not work for an instance  
> > #puts “%s” % a.foo=10

Instances of A don’t have a foo method, so you would indeed have to  
change the class to give them one. Is there a reason you don’t want  
to in this particular case?

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming May 1, 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [January 26, 2006, 6:08am UTC](https://www.ruby-forum.com/t/class-variable-weirdness/97457/5 "2006-01-26T06:08:20Z")

</div>

> Instances of A don’t have a foo method, so you would indeed have to  
> change the class to give them one. Is there a reason you don’t want  
> to in this particular case?

Yes I see I can not have it both ways now, either a class.method or a  
instance.method  
but not both with the same declairation.  
Thanks David
