tag:blogger.com,1999:blog-6793817269158736634.post2718173794700540627..comments2023-07-11T06:44:05.157-07:00Comments on Code It Like You Stole It: Instance variables and local variables in RailsTylerhttp://www.blogger.com/profile/02372203985854517525noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-6793817269158736634.post-54383191481373285592011-09-22T19:44:21.826-07:002011-09-22T19:44:21.826-07:00The main thing to think about when dealing with th...The main thing to think about when dealing with the different variable types is scope.<br /><br />Local variables have limited scope and exist within the method, block, etc. they're defined within. Starting out, just think of them as variables that aren't associated with an object's state. They're the ones you'd use within methods to temporarily hold values while performing calculations or procedures.<br /><br />Instance variables are useful for maintaining object state. If you're holding a value within an instance for use by multiple methods or across multiple accesses, use one of these.<br /><br />You're probably dealing with these primarily in the context of controllers and views. When you make a request, it's routed to the appropriate controller. An instance of this controller is initialized and the appropriate action called. Within this action, you set instance variables which, through some Rails magic, are accessible within the view. Try not to let this Rails behavior confuse you as to the basic nature of the variable types.<br /><br />Straightforward documentation:<br />http://www.rubyist.net/~slagell/ruby/localvars.html<br />http://www.rubyist.net/~slagell/ruby/instancevars.htmlMatthewhttps://www.blogger.com/profile/17882155503183882480noreply@blogger.com