Forum: Ruby on Rails Instance variables versus local variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C4a0d49327c78017de7194d4e543d2dd?d=identicon&s=25 Joseph Kowalski (Guest)
on 2006-04-22 05:12
(Received via mailing list)
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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-22 05:37
Joseph Kowalski wrote:
> 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.
C4a0d49327c78017de7194d4e543d2dd?d=identicon&s=25 Joseph Kowalski (Guest)
on 2006-04-22 09:23
(Received via mailing list)
On 4/21/06, Alex Wayne <rubyonrails@beautifulpixel.com> wrote:
>
>   end
> foo = Foo.new
> controller for almost all other needs.
Okay..got now. Thank you!
9f173c578c2ff435a41d448e25d62870?d=identicon&s=25 SB (Guest)
on 2006-04-25 16:32
(Received via mailing list)
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.

Sam
2d1a6a800524db3dd5a6bd7c79ea0e7b?d=identicon&s=25 Peter (Guest)
on 2006-04-25 17:31
>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.
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-25 19:02
Peter wrote:
>>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.
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-25 19:28
Alex Wayne wrote:
> Peter wrote:
>>>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.

--
Josh Susser
http://blog.hasmanythrough.com
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-25 19:53
(Received via mailing list)
Hi --

On Tue, 25 Apr 2006, Alex Wayne wrote:

>> But maybe I am wrong.
>
> I say again, regarding controllers:
>
> Instance Variables: Passed to views.

Or used obliquely (which may be a subset of "passed", but anyway just
to mention it), like:

   class SomeController
     def edit
       @entry = Entry.find...
       ...

and then in the view:

   <%= text_field "entry", "title" %>

where you'll get @entry.title as the default value of the field.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
This topic is locked and can not be replied to.