This novice coder roughly understands the difference between instance
and local variables thanks to David Black’s excellent book, but I’m
still unclear as to when and why a either is more desirable to use.
In general, I use instance variables in my controller and local in my
views, but I’m not sure as to why or if this is correct.
This novice coder roughly understands the difference between instance
and local variables thanks to David Black’s excellent book, but I’m
still unclear as to when and why a either is more desirable to use.
In general, I use instance variables in my controller and local in my
views, but I’m not sure as to why or if this is correct.
Thanks
Joe Kowalski
Instance variables are unique to the instance of an object and can be
used in any of its method. Local variable are only used inside one
method.
try the following in irb
Class Foo
def initialize
var = 123 @var = 123
end
def instance_var @var
end
def local_var
var
end
end
foo = Foo.new
foo.instance_var #=> 123
foo.local_var #=> NameError var is not defined
When we set var = 123 in initialize that variable only exists in the
“new” method. But when we use an instance variable like @var then @var
is accessible in any method of the class, and retains its value.
In Rails specifically, controller instance variables are passed along to
the views. So the only reason to use an instance variable is if you
want the view aware of that variable. Use local variables in your
controller for almost all other needs.
Since this came up in the Ruby on Rails Weblog, can anybody care to
elaborate a little more on some typical scenarios where an instance
variable is preferable to a local variable and vice versa?
I see a lot of instance variables in the sample code of the rails book
so I’m a little lost on this.
So the only reason to use an instance variable is if you
want the view aware of that variable.
So like he said above.
Of course you want your instance @row in the view if its a database row.
Otherwise there is nothing to view.
But vars for counters in loops or whatever don’t have to be instances of
course.
So the only reason to use an instance variable is if you
want the view aware of that variable.
So like he said above.
Of course you want your instance @row in the view if its a database row.
Otherwise there is nothing to view.
But vars for counters in loops or whatever don’t have to be instances of
course.
But maybe I am wrong.
I say again, regarding controllers:
Instance Variables: Passed to views.
Local Variables: Not passed to views.
class SomeController < …
def assign_user
post = Post.find(params[:id])
post.user = User.find(params[:user_id])
post.save
redirect_to :action => 'index'
end
end
So in the above example there are no instance variable because there is
no view, therefore since nothing needs to be passed along, all variables
should be local.
So the only reason to use an instance variable is if you
want the view aware of that variable.
So like he said above.
Of course you want your instance @row in the view if its a database row.
Otherwise there is nothing to view.
But vars for counters in loops or whatever don’t have to be instances of
course.
But maybe I am wrong.
I say again, regarding controllers:
Instance Variables: Passed to views.
Local Variables: Not passed to views.
class SomeController < …
def assign_user
post = Post.find(params[:id])
post.user = User.find(params[:user_id])
post.save
redirect_to :action => 'index'
end
end
So in the above example there are no instance variable because there is
no view, therefore since nothing needs to be passed along, all variables
should be local.
That’s mostly right. You might want an instvar in your controller to
help refactor your methods or simplify control flow. A typical example
is using a before filter to set up state that is needed by all actions
and stash that in an instvar for the actions to use. User authentication
systems commonly use that pattern.
And of course model objects can have their own instvars too. They can be
handy for holding values that are never stored in the database. For
example, you can create a “password” instvar to hold the plaintext
password that is entered on a form, then convert it to a MD5 hash value
to store it in the “hashed_password” field in the database.
My rule of thumb is that if the value is only needed within a method, it
should be a local. If other methods in the object (or a controller’s
view) need the value, make it an instance variable.