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
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 KowalskiInstance 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. -- Posted via http://www.ruby-forum.com/.
Joseph Kowalski
2006-Apr-22 07:21 UTC
[Rails] Re: Instance variables versus local variables
On 4/21/06, Alex Wayne <rubyonrails@beautifulpixel.com> wrote:> > 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.Okay..got now. Thank you! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060422/c6b9379c/attachment.html
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 On 4/22/06, Alex Wayne <rubyonrails@beautifulpixel.com> wrote:> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
>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. -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Apr-25 17:02 UTC
[Rails] Re: Re: Instance variables versus local variables
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. -- Posted via http://www.ruby-forum.com/.
Josh Susser
2006-Apr-25 17:34 UTC
[Rails] Re: Re: Instance variables versus local variables
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 -- Posted via http://www.ruby-forum.com/.
dblack@wobblini.net
2006-Apr-25 17:53 UTC
[Rails] Re: Re: Instance variables versus local variables
Hi -- On Tue, 25 Apr 2006, 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.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!
Seemingly Similar Threads
- partials and instance variables for file_column
- setup method in functional tests and instance variables
- how would you take the duplication out of this?
- warning: toplevel constant SomeController referenced by Admin::SomeController
- Undefined method "redirect_to" in before_filter