# Class variable "scope"

**URL:** <https://www.ruby-forum.com/t/class-variable-scope/188143>\
**Category:** Ruby\
**Created:** [May 6, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/class-variable-scope/188143 "2010-05-06T15:08:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinSSS\_cius\_Baggio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c67d28/32.png) [@VinSSS\_cius\_Baggio\_S](https://www.ruby-forum.com/u/VinSSS_cius_Baggio_S)\
**Post date:** [May 6, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/class-variable-scope/188143/1 "2010-05-06T15:08:52Z")

</div>

Hi all,

I’ve been writing a library for me to play with metaprogramming as an  
exercise, but then I got stuck  
on a non-metaprogramming issue:

## a.rb

class A  
def self.add\_something(\*sth)  
@@my\_things ||= []  
@@my\_things \<\< sth  
end

def self.my\_things  
@@my\_things  
end  
end

## b.rb

require ‘a’

class B \< A  
add\_something :bag, :boots  
end  
require ‘a’

## c.rb

class C \< A  
add\_something :sword, :shield  
end

The result, when requiring both c and b into an irb shell is, as  
expected:

ruby-1.8.7-p249 \> C.my\_things  
=\> [[:bag, :boots], [:sword, :shield]]  
ruby-1.8.7-p249 \> B.my\_things  
=\> [[:bag, :boots], [:sword, :shield]]

I did achieve the following:

C.my\_things  
[[:sword, :shield]]  
B.my\_things  
[[:bag, :boots]]

by moving my\_things method into each child class and using the method on  
the  
add\_something method, but there must be a better way. How one could  
improve  
this?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![VinSSS\_cius\_Baggio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c67d28/32.png) [@VinSSS\_cius\_Baggio\_S](https://www.ruby-forum.com/u/VinSSS_cius_Baggio_S)\
**Post date:** [May 6, 2010, 3:34pm UTC](https://www.ruby-forum.com/t/class-variable-scope/188143/2 "2010-05-06T15:34:38Z")

</div>

2010/5/6 Vinícius Baggio F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> @@my\_things \<\< sth  
> class B \< A
> 
> [[:bag, :boots]]
> 
> by moving my\_things method into each child class and using the method on the  
> add\_something method, but there must be a better way. How one could improve  
> this?

Use a class instance variable rather than a class variable.

Also I suspect you really want to get [:sword, :shield] rather than  
[[:sword, :shield]]

class A  
def self.add\_something(\*sth)  
@my\_things ||= []  
@my\_things.concat sth  
end

def self.my\_things  
@my\_things  
end  
end

class B \< A  
add\_something :bag, :boots  
end

class C \< A  
add\_something :sword, :shield  
end

B.my\_things # =\> [:bag, :boots]  
C.my\_things # =\> [:sword, :shield]

If you really want to get a array of arrays rather than a single level  
array, just change the .concat back to \<\<

## HTH

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![VinSSS\_cius\_Baggio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c67d28/32.png) [@VinSSS\_cius\_Baggio\_S](https://www.ruby-forum.com/u/VinSSS_cius_Baggio_S)\
**Post date:** [May 6, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/class-variable-scope/188143/3 "2010-05-06T15:44:36Z")

</div>

Great, much better, thanks for the reply, Rick!
