Cookies and Foreign Classes

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.

Hello,

Am Montag, 31. Dezember 2007 schrieb Nuno Machado:

f = Foreign.new
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

Hello,

Am Montag, 31. Dezember 2007 schrieb Jan L.:

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 => cookies

Sorry,

Keep smiling
yanosz

On Mon, Dec 31, 2007 at 01:26:38PM +0100, Nuno Machado wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs