Question

Possible Duplicate:
Difference between class variables and class instance variables?

While reading a Ruby book, I seem to have missed the variables chapter. Now I can't seem to understand the following things:

  1. What is an instance variable?
  2. What is a class instance variable?
  3. What is the difference between a variable, @instance_var and @class_instance_var?

I tried to read some posts in different blogs, but I still do not understand. Any help would be appreciated.

Was it helpful?

Solution

What is an instance variable?

It's a variable that has an idependant value that pertains to this instance of a class. For example, a Person class could have @name and @age as instance variables. All instance of Person have a name and age, but each instance will have a different value for those things.


What is a class instance variable?

This is a little wierd, but you have to realize that the Person class is itself an instance of Class. So it too can have instance variables. This is often used to configure a class. Like perhaps to add an API key to a class so that all instance can be created with that data.

class PersonFetcher

  # class method can set class instance variables
  def self.set_api_key(key)
    @api_key = key
  end

  # instance method sets instance variables
  def set_name(name)
    @name = name
  end

end

What is the difference between a variable, @instance_var and @class_instance_var?

How it persists.

variable is local. It's simply a reference to an object. Once no code or object has a reference to this value it is destroyed via garbage collection. It only persists if you keep using it.

@instance_var persists on an instance. So long as the instance persists, the instance variables it has will as well. So long as the Person instance exists, that instance will have a @name.

@class_instance_var persists on the class object (which remember is an instance of Class). So it will exist in the class object forever, since you can never really get rid of declared classes.

OTHER TIPS

Variable Type Affects Scope

There are many canonical explanations on the web, and some in-depth explanations elsewhere on SO, but I'll share with you another way of looking at it: variable scope.

Local Variables

A local variable is typically used in a method, and has only local scope. For example:

def foo
  # bar is only in scope within #foo.
  bar = true
end

The bar variable goes out of scope whenever the method returns.

Instance Variables

Instance variables are available to any method bound to an object instance. As a practical matter, this generally gives it scope within some instantiated class object. For example:

class Foo
  def bar
    @quux = true
  end

  def baz
    @quux == true
  end
end

foo = Foo.new

While @quux isn't truly global, it is visible to all methods bound to our instantiated foo object without the need to pass it around explicitly as a parameter.

Class Variables

Class variables are shared among all instances of a class. That means that if you change @@bar from instance foo, then the value is also changed for instance bar.

class Foo
  @@bar = true

  def show_class_variable
    @@bar
  end

  def bar= value
    @@bar = value
  end
end

foo = Foo.new
bar = Foo.new

foo.show_class_variable # => true
bar.show_class_variable # => true

foo.bar = false
foo.show_class_variable # => false
bar.show_class_variable # => false

Note how changing @@bar affected both instances simultaneously. That's the beauty (and the horror) of class variables.

A varaible is something local to a method and thus should be defined in this method, an instance variable is a variable that is defined for (instance of) an object of the given class and has a value for each object. A class instance variable is denoted with @@(as opposed to the single @ you mention) and is shared between all instances of a class and if one of them changes its value then all the instances will see the new value. A class instance variable is equivelent to c++ and java's static member variables and the instance variables are equivelent to the non-static ones.

Instance variables are defined at the moment when a class has been instaniated. Suppose you have a class Point, which define some local variables and methods.

class Point
  def initialize(x,y)
     @x, @y = x, y
  end

  def add(other)
     Point.new(@x + other, @y + other)
  end
end

When you instantiate the defined class and assign it to a variable you are instantiating the class:

point = Point.new(2,2)

..this is the instance variable.

Classes in Ruby are Objects and can have instance variables as other objects can. An instance variable defined inside a class definition, but outside an instance method definition is called class instance variable.

Example:

class Point 
  @n = 0
  @total_x = 0
  @total_y = 0

  def initialize(x,y)
     @x,@y = x,y
  end
end

There is a third one, namely the class variable. Class variables are visible to, and shared by the class methods and the instance methods of the class. Like instance variables, class variables can be used by the implementation of a class, but they are not visible to the users of the class. Class variables begin with @@. Instance variables are always evaluated in reference to self. This is a significant difference from class variables, which are always evaluated in reference to the class object created by the enclosing class definition statement. Using the same example we can rewrite the code as follows:

class Point
   @@n = 0
   @@total_x = 0
   @@total_y = 0

   def initialize(x,y)
      @x,@y = x, y
      @@n += 1
   end

   def self.report
      puts "Number of points: #{@@n.to_s}"
   end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top