i''ve seen both of these syntaxes before and have also used both in my own models: class Foo def get_bar @bar end end and class Foo def get_bar self.bar end end seemingly with no difference... i''m a nuby though so could anyone enlighten me to the differences if there are any? thanks, jeff --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
@bar is an instance variable... self.bar would be a method. Try doing this code in irb and you''ll see that calling get_bar in the first version returns nil, while calling it on the second version throws a NoMethodError. b jemminger wrote:> i''ve seen both of these syntaxes before and have also used both in my > own models: > > class Foo > def get_bar > @bar > end > end > > and > > class Foo > def get_bar > self.bar > end > end > > seemingly with no difference... i''m a nuby though so could anyone > enlighten me to the differences if there are any? > > thanks, > jeff > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
aha! i guess then the reason it has worked in RoR models is due to AR creating attr_accessors for the table columns, so calling model.some_col was returning @some_col... thanks for clearing that up On Apr 5, 11:04 pm, Ben Munat <bmu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @bar is an instance variable... self.bar would be a method. Try doing > this code in irb and you''ll see that calling get_bar in the first > version returns nil, while calling it on the second version throws a > NoMethodError. > > b > > jemminger wrote: > > i''ve seen both of these syntaxes before and have also used both in my > > own models: > > > class Foo > > def get_bar > > @bar > > end > > end > > > and > > > class Foo > > def get_bar > > self.bar > > end > > end > > > seemingly with no difference... i''m a nuby though so could anyone > > enlighten me to the differences if there are any? > > > thanks, > > jeff--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeff Emminger wrote:> i''ve seen both of these syntaxes before and have also used both in my > own models: > > class Foo > def get_bar > @bar > end > end > > and > > class Foo > def get_bar > self.bar > end > end > > seemingly with no difference... i''m a nuby though so could anyone > enlighten me to the differences if there are any? > > thanks, > jeffThis can be a real Gotcha in ActiveRecord. AR uses the *_missing methods to give the illusion that it knows what columns you have in your table. I believe that using the instance variable (@bar) now spews lots of depreciation noeses onto stdout. Good programming practice it to used the method call because you can then override that method call in your class and massage the data before it goes out (or comes in). This can eliminate the need for the get_* functions. def bar retval = self[:bar] #do interesting thing with retval here retval end John Miller -- 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 -~----------~----~----~----~------~----~------~--~---