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
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.