Symbols and class veriable

  • Thread starter Jerome David Sallinger
  • Start date
J

Jerome David Sallinger

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
 
P

Paul Harrington

Jerome said:
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

@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.
 
M

Marnen Laibow-Koser

Jerome said:
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
(e-mail address removed)
 
A

Albert Schlef

Jerome said:
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".
 
A

Albert Schlef

Jerome said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top