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

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

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.

arrr - thanks heaps Alex

Thanks, Alex W.!

Regards,
Anil W.

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.

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

Regards,
Jatinder

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.