Dharam Gollapudi
Dharam Gollapudi

Reputation: 6438

Ruby Variables Clarification

I see that Ruby has the following variables: - global variables (represented by $variable_name) - class variables (represented by @@variable_name) - instance variables (represented by @variable_name) and - local variables (represented by variable_name or _variable_name)

Occasionally I see the following in the rails source code:

class SomeClass @var end

Here what exactly @var represent and what do you call it, metaclass variable? Also whats the advantage of using this kind of variables?

Upvotes: 0

Views: 143

Answers (1)

BaroqueBobcat
BaroqueBobcat

Reputation: 10150

It is one of the classes instance variables. In Ruby, everything is an object, even classes, so it isn't surprising that classes can have instance variables.

class A
  @@class_var = 1
  @instance_var = 1
end
A.class_variables
#=> ["@@class_var"]
A.instance_variables
#=>["@instance_var"]

More Info

Upvotes: 1

Related Questions