Hi guys, I have a method inside a controller that has many lines of code (some of them used to generate/read cookies). In order to keep the code readable and use the object-oriented power of Ruby, I created a foreign class that I call in my method using: def index require(''foreign_class'') f = Foreign.new @out = f.foreign_method end The problem is, when the foreign_method tries to create a cookies with cookies[:c] = ''id'' it says "undefined local variable or method ''cookies'' So... it''s not possible to handle cookies in foreign classes. Or it''s not even supposed to use foreign classes in Ruby? Thanks a lot. -- 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 -~----------~----~----~----~------~----~------~--~---
Hello, Am Montag, 31. Dezember 2007 schrieb Nuno Machado:> Hi guys, > > I have a method inside a controller that has many lines of code (some of > them used to generate/read cookies). > > In order to keep the code readable and use the object-oriented power of > Ruby, I created a foreign class that I call in my method using: > > def index > require(''foreign_class'') > f = Foreign.new > @out = f.foreign_method > end > > The problem is, when the foreign_method tries to create a cookies with > > cookies[:c] = ''id'' > > it says "undefined local variable or method ''cookies'' > > So... it''s not possible to handle cookies in foreign classes. Or it''s > not even supposed to use foreign classes in Ruby?What''s keep you from introducing coookies as a class atttribute in Foreign? I mean: f = Foreign.new :cookies => cookies Keep smiling yanosz --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hello, Am Montag, 31. Dezember 2007 schrieb Jan Luehr:> Hello, > > Am Montag, 31. Dezember 2007 schrieb Nuno Machado: > > Hi guys, > > > > I have a method inside a controller that has many lines of code (some of > > them used to generate/read cookies). > > > > In order to keep the code readable and use the object-oriented power of > > Ruby, I created a foreign class that I call in my method using: > > > > def index > > require(''foreign_class'') > > f = Foreign.new > > @out = f.foreign_method > > end > > > > The problem is, when the foreign_method tries to create a cookies with > > > > cookies[:c] = ''id'' > > > > it says "undefined local variable or method ''cookies'' > > > > So... it''s not possible to handle cookies in foreign classes. Or it''s > > not even supposed to use foreign classes in Ruby? > > What''s keep you from introducing coookies as a class atttribute in Foreign?ups, I meant instance attribute.> I mean: f = Foreign.new :cookies => cookiesSorry, Keep smiling yanosz --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Mon, Dec 31, 2007 at 01:26:38PM +0100, Nuno Machado wrote:> I have a method inside a controller that has many lines of code (some of > them used to generate/read cookies). > > In order to keep the code readable and use the object-oriented power of > Ruby, I created a foreign class that I call in my method using: > > def index > require(''foreign_class'') > f = Foreign.new > @out = f.foreign_method > end > > The problem is, when the foreign_method tries to create a cookies with > > cookies[:c] = ''id'' > > it says "undefined local variable or method ''cookies'' > > So... it''s not possible to handle cookies in foreign classes. Or it''s > not even supposed to use foreign classes in Ruby?I''m trying to figure out how to say this nicely, but I''m not coming up with anything. Bluntly, you don''t seem to understand OO. The reason cookies isn''t available in your "foreign" class is that cookies is a method of the controller (inherited from ActionController::Base). This is to be expected since objects provide separate namespaces for methods and instance variables; that''s a pretty fundamental part of what it means to be object oriented. Also, working in an OO language does not necessarily imply that creating a new object class is the best approach in all cases. For what you seem to be trying to do, I''d recommend using a module (a.k.a. mixin) that you include in your controller class. A module''s methods have access to all the methods and instance variables of the class in which it is included, such as the cookies method.> Thanks a lot.--Greg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---