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:
f = Foreign.new
@out = f.foreign_method
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
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
Thanks a lot.