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?