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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann 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? > > Tksthe @ 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
arrr - thanks heaps Alex --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanks, Alex Wayne! Regards, Anil Wadghule -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Wayne 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thats right. For every request a new instance of the controller is created and hence all instance variables get the defaults! Regards, Jatinder On 9/4/06, Dominique Plante <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 Wayne 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. > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
also for example if you want to do something like this def test if g>h @checked = true <more stuff> end <some more stuff> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---