# Cookies and Foreign Classes

**URL:** <https://www.ruby-forum.com/t/cookies-and-foreign-classes/124642>\
**Category:** Rails\
**Created:** [December 31, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/cookies-and-foreign-classes/124642 "2007-12-31T13:26:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7feea3/32.png) [@ngm](https://www.ruby-forum.com/u/ngm)\
**Post date:** [December 31, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/cookies-and-foreign-classes/124642/1 "2007-12-31T13:26:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ngm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7feea3/32.png) [@ngm](https://www.ruby-forum.com/u/ngm)\
**Post date:** [December 31, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/cookies-and-foreign-classes/124642/2 "2007-12-31T13:40:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ngm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7feea3/32.png) [@ngm](https://www.ruby-forum.com/u/ngm)\
**Post date:** [December 31, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/cookies-and-foreign-classes/124642/3 "2007-12-31T13:42:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ngm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7feea3/32.png) [@ngm](https://www.ruby-forum.com/u/ngm)\
**Post date:** [December 31, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/cookies-and-foreign-classes/124642/4 "2007-12-31T16:36:26Z")

</div>

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
