What's the difference between @title
and title
? Since both of them can be variable names. Also, how do I decide which kind of variable I should use? With @
or not?
-
1I would say this tutorial is quite neat to explain all these: https://www.techotopia.com/index.php/Ruby_Variable_Scope#What_is_Variable_Scope.3F – Hearen Mar 27 '19 at 07:54
7 Answers
title
is a local variable. They only exists within its scope (current block)
@title
is an instance variable - and is available to all methods within the class.
You can read more here: http://strugglingwithruby.blogspot.dk/2010/03/variables.html
In Ruby on Rails - declaring your variables in your controller as instance variables (@title
) makes them available to your view.

- 286
- 8
- 26

- 16,474
- 7
- 46
- 63
-
4Thanks for the answer, have follow-up question... Within a class that I'm working on, the member variable is initially constructed like @options = {...}. After this though, methods within the class access and make function calls with that variable as if it were local (i.e. "options.merge()"). Is this just bad form? Is there some other syntactic magic going in? (i.e. like making silent dup of variable, or making it 'const' so that it can't be modified, etc)? I'm coming from a C/C++ & Java perspective, and the really vague and optional syntax is driving me nuts! – Dan Devine Jun 13 '18 at 18:05
-
Does your class have an attr_accessor defined with the same name @DanDevine? attr_accessor is syntactic sugar that generates a getter\setter for you. If you are getting the value without the @ symbol the code receives the variable because it's calling the getter. Note this doesn't work the other way, you need to explicitly use the @ symbol if you are setting the var or else you'll just set a local var. Is this good style? Hard to say. I prefer to use the getter syntax in my methods in case the getter is overriden - I like to respect that encapsulation, even within the class itself. YMMV – Msencenb Dec 14 '19 at 22:25
Use @title
in your controllers when you want your variable to be available in your views.
The explanation is that @title
is an instance variable while title
is a local variable. Rails makes instance variables from controllers available to views because the template code (erb, haml, etc) is executed within the scope of the current controller instance.

- 7,474
- 5
- 39
- 59
-
2Thanks for this! This is the only answer that explains why @variables defined in the controller class can be picked up by code in the views. – AlexC Dec 02 '14 at 13:54
A tutorial about What is Variable Scope? presents some details quite well, just enclose the related here.
+------------------+----------------------+
| Name Begins With | Variable Scope |
+------------------+----------------------+
| $ | A global variable |
| @ | An instance variable |
| [a-z] or _ | A local variable |
| [A-Z] | A constant |
| @@ | A class variable |
+------------------+----------------------+

- 7,420
- 4
- 53
- 63
The difference is in the scope of the variable. The @version is available to all methods of the class instance.
The short answer, if you're in the controller and you need to make the variable available to the view then use @variable
.
For a much longer answer try this: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html

- 3,763
- 3
- 32
- 54
@variable
s are called instance variables in ruby. Which means you can access these variables in ANY METHOD inside the class. [Across all methods in the class]
Variables without the @
symbol are called local variables, which means you can access these local variables within THAT DECLARED METHOD only. Limited to the local scope.
Example of Instance Variables:
class Customer
def initialize(id, name, addr)
@cust_id = id
@cust_name = name
@cust_addr = addr
end
def display_details
puts "Customer id #{@cust_id}"
puts "Customer name #{@cust_name}"
puts "Customer address #{@cust_addr}"
end
end
In the above example @cust_id
, @cust_name
, @cust_addr
are accessed in another method within the class. But the same thing would not be accessible with local variables.

- 9,616
- 6
- 50
- 64

- 6,458
- 2
- 40
- 51
A local variable is only accessible from within the block of it's initialization. Also a local variable begins with a lower case letter (a-z) or underscore (_).
And instance variable is an instance of self
and begins with a @
Also an instance variable belongs to the object itself. Instance variables are the ones that you perform methods on i.e. .send
etc
example:
@user = User.all
The @user
is the instance variable
And Uninitialized instance variables have a value of Nil

- 261
- 1
- 4
- 13
-
So how does this explain the difference between `@title` & `title`? – Richard Peck Dec 09 '13 at 09:37
-
With an instance variable like @title you can perform various methods on it, whereas a local variable you don't – Joe Hilton Dec 09 '13 at 10:02
-
1So how does that actually fix the question? Looks like you're just quoting some tutorial. The OP wants to know whether he should use `@title` or `title` – Richard Peck Dec 09 '13 at 10:04
-
So they should use an instance variable if they want to perform any methods I.e. def new or def create etc – Joe Hilton Dec 09 '13 at 10:11
-
-
3Nothing stops you from doing user = User.all and perform methods on user but this is not the point. – Jaro Sep 05 '14 at 14:39
@ variables are instance variables, without are local variables.
Read more at http://ruby.about.com/od/variables/a/Instance-Variables.htm

- 37,398
- 8
- 88
- 97