# Symbols and class veriable

**URL:** <https://www.ruby-forum.com/t/symbols-and-class-veriable/183071>\
**Category:** Ruby\
**Created:** [February 7, 2010, 3:01pm UTC](https://www.ruby-forum.com/t/symbols-and-class-veriable/183071 "2010-02-07T15:01:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [February 7, 2010, 3:01pm UTC](https://www.ruby-forum.com/t/symbols-and-class-veriable/183071/1 "2010-02-07T15:01:34Z")

</div>

Hello, Is the any connection between a symbol and a class variable. For  
example, if I declare the variable @red\_car\_speed is this aliased or  
connected by :red\_car\_speed. The section of code that is confuring me  
is:

* * *

require ‘ruby-processing’

class TwoCarObjects \< Processing::App  
load\_library :control\_panel

def setup  
control\_panel do |c|  
c.slider :red\_car\_speed, -5…15  
c.slider :blue\_car\_speed, -5…15  
end

```
# Initialize car objects
@red_car = Car.new(self, color(255,0,0), 0, 100)
@blue_car = Car.new(self, color(0,0,255), 0, 10)
@red_car_speed = 1
@blue_car_speed = 2
rect_mode CENTER

```

end

def draw  
background 255

# Operate the car object in draw

# by calling object methods using the dots syntax.

@red\_car.move(@red\_car\_speed)  
@red\_car.display\_car  
@blue\_car.move(@blue\_car\_speed)  
@blue\_car.display\_car  
end

## end

It seems that the symbols and class variable for, for example  
red\_car\_speed are linked

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [February 7, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/symbols-and-class-veriable/183071/2 "2010-02-07T17:55:19Z")

</div>

Jerome David S. wrote:

> Hello, Is the any connection between a symbol and a class variable. For  
> example, if I declare the variable @red\_car\_speed is this aliased or  
> connected by :red\_car\_speed. The section of code that is confuring me  
> is:
> 
> * * *
> 
> require ‘ruby-processing’
> 
> class TwoCarObjects \< Processing::App  
> load\_library :control\_panel
> 
> def setup  
> control\_panel do |c|  
> c.slider :red\_car\_speed, -5…15  
> c.slider :blue\_car\_speed, -5…15  
> end
> 
> ```
> # Initialize car objects
> @red_car = Car.new(self, color(255,0,0), 0, 100)
> @blue_car = Car.new(self, color(0,0,255), 0, 10)
> @red_car_speed = 1
> @blue_car_speed = 2
> rect_mode CENTER
> 
> ```
> 
> end
> 
> def draw  
> background 255
> 
> # Operate the car object in draw
> 
> # by calling object methods using the dots syntax.
> 
> @red\_car.move(@red\_car\_speed)  
> @red\_car.display\_car  
> @blue\_car.move(@blue\_car\_speed)  
> @blue\_car.display\_car  
> end
> 
> ## end
> 
> It seems that the symbols and class variable for, for example  
> red\_car\_speed are linked

@variable\_name is an _instance_ variable, not class variable. They are  
scoped to the individual instances of a class, whereas class variables  
can be thought of as “globals” accessible to all instances of a  
particular class (the behavior of class variables in Ruby is strange and  
inconsistent between versions, so avoid using them).

Symbols are actually fairly similar to strings, except with stricter  
rules about their contents, are immutable, act like singletons (each  
symbol representation only is created once, so if a = :test; b = :test,  
then a.object\_id == b.object\_id), and are never garbage collected. For  
the last the reasons symbols are often treated as identifiers in Ruby;  
many methods use symbols to look up or return the the names of variables  
or methods. They’re also commonly used as hash keys, or really just  
about any situation that requires a accessing something by name, the  
name probably lasting the life of the Ruby process. However, symbols  
really have no intrinsic relation to any sort of variable; this illusion  
rises strictly from convention.

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [February 7, 2010, 6:48pm UTC](https://www.ruby-forum.com/t/symbols-and-class-veriable/183071/3 "2010-02-07T18:48:54Z")

</div>

Jerome David S. wrote:

> Hello, Is the any connection between a symbol and a class variable. For  
> example, if I declare the variable @red\_car\_speed is this aliased or  
> connected by :red\_car\_speed.

No.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [February 7, 2010, 11:28pm UTC](https://www.ruby-forum.com/t/symbols-and-class-veriable/183071/4 "2010-02-07T23:28:11Z")

</div>

Jerome David S. wrote:

> Hello, Is the any connection between a symbol and a class variable. For  
> example, if I declare the variable @red\_car\_speed is this aliased or  
> connected by :red\_car\_speed. The section of code that is confuring me  
> is:

[…]

> def draw  
> background 255

And note that you’ve sinned here. You didn’t keep correct indentation.  
You cause some readers to misread your code and assume you’re confusing  
instance variables with class instance variables (it’s what I initially  
thought before I noticed there’s a “def” there).

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [February 7, 2010, 11:23pm UTC](https://www.ruby-forum.com/t/symbols-and-class-veriable/183071/5 "2010-02-07T23:23:55Z")

</div>

Jerome David S. wrote:

> Hello, Is the any connection between a symbol and a class variable. For  
> example, if I declare the variable @red\_car\_speed is this aliased or  
> connected by :red\_car\_speed. The section of code that is confuring me  
> is:
> 
> ```
> control_panel do |c|
> c.slider :red_car_speed, -5..15
> c.slider :blue_car_speed, -5..15
> end
> 
> ```

[…]

> ```
> @red_car_speed = 1
> @blue_car_speed = 2
> 
> ```

@something and :something aren’t linked.

It happens that the slider() method gets the name of a variable whose  
value it will change as the user interacts with the slider. This  
technique, of seemingly extending Ruby’s syntax, is known as  
“metaprogramming”.
