# When do I use "a" versus "@a" as a variable?

**URL:** <https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320>\
**Category:** Rails\
**Created:** [September 2, 2006, 5:55am UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320 "2006-09-02T05:55:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 2, 2006, 5:55am UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320/1 "2006-09-02T05:55:34Z")

</div>

Hi,

Just realising I don’t quite understand the different between the  
variable name to use (say in a controller method) in terms of either “a”  
or “@a”. Both seem to work find re passing it to a local method in the  
class?

Tks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 2, 2006, 7:10am UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320/2 "2006-09-02T07:10:46Z")

</div>

Greg H. wrote:

> Hi,
> 
> Just realising I don’t quite understand the different between the  
> variable name to use (say in a controller method) in terms of either “a”  
> or “@a”. Both seem to work find re passing it to a local method in the  
> class?
> 
> Tks

the @ symbol denotes an instance variable, without it they are local  
variables. First I’ll show the difference in standard ruby:

class Foo  
def initialize(value)  
@foo = value  
foo = value  
end  
def instance\_var  
@foo  
end  
def local\_var  
foo  
end  
end

var = Foo.new(‘Hello World’)  
var.instance\_var #=\> ‘Hello World’  
var.local\_var #=\> ERROR: no method or local variable ‘foo’

An instance variable retains it’s value for the life of the object and  
is exposed to all of the objects internal methods. Local variables are  
destroyed as soon as the method they are defined in finishes execution.

In rails controllers however, all instance variables are passed to the  
views where the local variables are not:

#controller action  
def show\_var\_scope  
@foo = ‘Hello World’  
foo = ‘Goodbye World’  
end

#app/views/show\_var\_scope.rhtml  
\<%= @foo #yields ‘Hello World’ %\>  
\<%= foo #yields ERROR: undefined method or local variable %\>

So use the instance variables in you controller action when you want the  
view to use the variable. Otherwise just use local variables.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 2, 2006, 10:16am UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320/3 "2006-09-02T10:16:04Z")

</div>

arrr - thanks heaps Alex

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 3, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320/4 "2006-09-03T18:10:34Z")

</div>

Thanks, Alex W.!

Regards,  
Anil W.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 4, 2006, 7:58am UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320/5 "2006-09-04T07:58:17Z")

</div>

Correct me if I’m wrong, but one thing to keep in mind about instance  
variables is that yes, it is true that they are available to the view as  
you showed so beautifully, but only on the current request… That is,  
you must save to DB or session in order for another action to be able to  
get the value of @foo when a different view is displayed.

Is this correct?

Thanks!  
Dominique

Alex W. wrote:

> An instance variable retains it’s value for the life of the object and  
> is exposed to all of the objects internal methods. Local variables are  
> destroyed as soon as the method they are defined in finishes execution.
> 
> In rails controllers however, all instance variables are passed to the  
> views where the local variables are not:
> 
> #controller action  
> def show\_var\_scope  
> @foo = ‘Hello World’  
> foo = ‘Goodbye World’  
> end
> 
> #app/views/show\_var\_scope.rhtml  
> \<%= @foo #yields ‘Hello World’ %\>  
> \<%= foo #yields ERROR: undefined method or local variable %\>
> 
> So use the instance variables in you controller action when you want the  
> view to use the variable. Otherwise just use local variables.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 4, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320/6 "2006-09-04T15:11:21Z")

</div>

Thats right.  
For every request a new instance of the controller is created and hence  
all  
instance variables get the defaults!

Regards,  
Jatinder

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [April 11, 2007, 7:28am UTC](https://www.ruby-forum.com/t/when-do-i-use-a-versus-a-as-a-variable/67320/7 "2007-04-11T07:28:22Z")

</div>

also for example if you want to do something like this

def test  
if g\>h  
@checked = true  
  
end  
  
new\_var = 6 if @checked  
end

if you used just normal “checked” you would have to use defined? checked  
in the if condition since its a local variable.

You never have to worry with “@a” variables because it will default to  
nil and not throw an error.
