27

I'm new to rails and ruby. I was studying the concept of class and instance variables. I understood the difference but when I tried it out using the controller in rails it got me confused. What I did is I declared a class and instance variables outside the class methods:

class BooksController < ApplicationController
  # GET /books
  # GET /books.json

  @@world = "Hello World"
  @insworld = "my hobby"

  def index
    @books = Book.all
    binding.pry

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @books }
    end
  end

end

I was under the impression that @insworld has the value of "my hobby", but when I tried to check the value of @insworld when I was inside the index method, @insworld was returning a nil value. @@world has the value of "Hello World". So what happened here? Aren't they defined in the same class?

Finks
  • 1,661
  • 2
  • 16
  • 25
  • You might want to check the accepted answer for this SO question: http://stackoverflow.com/questions/3802540/difference-between-class-variables-and-class-instance-variables – lucke84 Jan 23 '14 at 15:07

5 Answers5

58

Classes are also objects in Ruby, so they can have their own instance variables which are called class instance variables.

  • @@world is a class variable
  • @insworld is a class instance variable
  • #index is an instance method

When you try to access @insworld in #index, Ruby searches for the instance variable in the A object (meaning A.new) because #index is an instance method.

But you defined @insworld as a class instance variable which means it is defined in the class object itself (meaning A).

The following code demonstrates:

class Hi
  @@a = 1 # class variable
  @b  = 2 # class instance variable

  def initialize
    @c = 3 # instance variable
  end

  def test # instance method, works on objects of class Hi
    puts @@a # => 1
    puts @b  # => nil, there is no instance variable @b
    puts @c  # => 3 # we defined this instance variable in the initializer
  end
end

Hi.class_variables        # => @@a
Hi.instance_variables     # => @b
Hi.new.instance_variables # => @c
# Hi is an object of class Class
# Hi.new is an object of class Hi

Keep in mind that all instance variables return nil if they don't exist.

Agis
  • 32,639
  • 3
  • 73
  • 81
7

When you declare @instworld you are inside BooksController class (i.e. self will return BooksController. Weird thing in ruby is that classes are also objects (the are instances of class Class) hence you in fact declares instance variable @instworld for this particular instance of class Classm not for instance of BooksController.

You can check it really easily by declaring class method:

class A
  # self here returns class A
  @variable = 'class instance variable'
  @@variable = 'class variable'

  def initalize
    # self here returns the instance
    @variable = 'instance variable'
  end

  def self.test_me
    # self here returns class A
    @variable
  end

  def test_me
    # self returns the instance
    @variable
  end

  #class variable is accessible by both class and instance 
  def test_me2
    @@variable
  end

  def self.test_me2
    @@variable
  end
end

A.test_me       #=> 'class instance variable'
A.new.test_me   #=> 'instance variable'
A.test_me2      #=> 'class variable'
A.new.test_me2  #=> 'class variable'
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
3

When you declare instance variable outside of method, you might not get the result you want. It is an instance variable, yes, but it belongs to the class itself (which is an instance of class Class).

class Foo
  @myvar = "class-level"

  def initialize
    @myvar = 'instance-level'
  end
end

f = Foo.new

f.class.instance_variable_get(:@myvar) # => "class-level"
f.instance_variable_get(:@myvar) # => "instance-level"
f.instance_variable_get(:@myvar2) # => nil 

If you try to get value of uninitialized ivar, it will evaluate to nil. That's why you got nil in your experiments: the variable doesn't exist at that scope.

To get instance-level variables, set them in methods/actions.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
2

The scope of Instance variables are from action to view they initiate when any action occurs and destroy when action ends.

Ahmad Hussain
  • 2,443
  • 20
  • 27
  • 1
    so how do you have a true class instance variable, one that persists among all actions? @ahmad Hussain – conterio Mar 01 '17 at 22:10
2

Declaring the @insworld within the class but not in the constructor or any of the instance methods sets @insworld scoped to the instance of the class itself.

BooksController.instance_variable_get(:'@insworld')

If you need the access to variable inside of your index method, consider defining it within the method instead.

random-forest-cat
  • 33,652
  • 11
  • 120
  • 99