# Adding variables through modules

**URL:** <https://www.ruby-forum.com/t/adding-variables-through-modules/107656>\
**Category:** Rails\
**Created:** [June 24, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/adding-variables-through-modules/107656 "2007-06-24T21:28:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [June 24, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/adding-variables-through-modules/107656/1 "2007-06-24T21:28:29Z")

</div>

Hi!

I’m trying to enhance a set of controllers which can set the level of  
security.

```auto
Class MenuControllder < ApplicationController
  include BasicTest
end

module BasicTest # in lib/basic_test.rb
  @test = nil
  def self.included controller
    controller.before_filter(:basic_filter)
    @test = controller.name
  end

  def basic_filter
     raise @test
  end
end
```

This doesn’t work at all. @test is nil by the time it calls  
basic\_filter.  
If I use @@test then @@test results in the last controller that was  
built by the server.

I need a new instance of BasicTest for each controller, and I need it to  
not be accessed by other controllers that are inheriting BasicTest.

I might not be understanding Modules correctly, but I thought that this  
is the types of things they did since multi inheritance is not  
supported!

Any guidance on where to look to to accomplish this sort of thing would  
be appreciated!

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [June 25, 2007, 9:31am UTC](https://www.ruby-forum.com/t/adding-variables-through-modules/107656/2 "2007-06-25T09:31:42Z")

</div>

> module BasicTest # in lib/basic\_test.rb  
> @test = nil  
> def self.included controller  
> controller.before\_filter(:basic\_filter)  
> @test = controller.name  
> end
> 
> def basic\_filter  
> raise @test  
> end  
> end  
> [/code]
> 
> This doesn’t work at all. @test is nil by the time it calls  
> basic\_filter.

… what happens if you put @test = controller.name, before the  
controller.before\_filter(:basic\_filter) call in the self.included  
controller ?  
the @test = nil assignment in the begining of the module kinda ruins the  
assignment afterwords, no?

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [June 25, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/adding-variables-through-modules/107656/3 "2007-06-25T12:26:20Z")

</div>

> … what happens if you put @test = controller.name, before the  
> controller.before\_filter(:basic\_filter) call in the self.included  
> controller ?  
> the @test = nil assignment in the begining of the module kinda ruins the  
> assignment afterwords, no?

if you move the code around like you suggested, @test is still nil in  
the basic\_filter both with and without the @test = nil at the beginning  
of the module.

But from what you said the @ be available in all the methods of a class  
and the @@ act as a singleton variable? That’s what I always thought  
but have been foiled here.

Thanks for the suggestion any more would be greatly appreciated!

Matt

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [June 25, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/adding-variables-through-modules/107656/4 "2007-06-25T13:36:33Z")

</div>

Hi –

On Sun, 24 Jun 2007, Matt Sir wrote:

> module BasicTest # in lib/basic\_test.rb  
> [/code]
> 
> This doesn’t work at all. @test is nil by the time it calls  
> basic\_filter.

Actually you’ve got two totally unrelated @test variables here: the  
one belonging to the module object BasicTest, and one in an instance  
method which will belong to whatever object calls the method  
(basic\_filter). Instance variables always belong to “self”, which can  
be an object of any class.

> If I use @@test then @@test results in the last controller that was  
> built by the server.
> 
> I need a new instance of BasicTest for each controller, and I need it to  
> not be accessed by other controllers that are inheriting BasicTest.

Modules don’t have instances, and they don’t get inherited. So I’m  
not totally sure what you’re aiming for here. But maybe  
instance\_variable\_set would help? I’m thinking of something like  
(untested):

def self.included(controller)  
controller.before\_filter do |c| # c is an instance of controller  
c.instance\_variable\_set(“@test”, controller.name)  
end  
end

though since you can always get from a controller instance to its  
class’s name easily (self.class.name), you don’t really need to store  
this information.

Can you explain a little more what you want to happen?

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [June 25, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/adding-variables-through-modules/107656/5 "2007-06-25T22:05:29Z")

</div>

> def self.included(controller)  
> controller.before\_filter do |c| # c is an instance of controller  
> c.instance\_variable\_set("@test", controller.name)  
> end  
> end
> 
> though since you can always get from a controller instance to its  
> class’s name easily (self.class.name), you don’t really need to store  
> this information.
> 
> Can you explain a little more what you want to happen?
> 
> David

Hi!

So I gave that a try and didn’t quite work but came closer!

In the meantime… We have a database guy and a programmer, that’s me!  
The database guy wants it to be ‘normalized’, HA right! Well to do that  
we have the same information for controllers in different tables which  
mean different models. So we have MenuWidget and ForumWidget (there are  
several types of widgets)

But they all do the same thing in the before filter so I need the class  
name so i can dynamically call the appropriate tables. The @test was a  
simpler test i was using what I really have is something like.

class BasicTest  
self.included  
@problem\_here = Security.new(controller.name)  
end

def basic\_filter  
if @problem\_here.verify?(params[:action])  
blablbalba  
else  
redirect…  
end  
end

class Security  
@@name = nil  
def initialize controller  
@@name = controller.name  
end  
def verify?(action)  
do\_something\_snazy\_with\_the\_name  
end  
end  
end

so the Security class works dandy, it is getting the instance of the  
security class to stick around. I got it to work fine by storing the  
variable inside the controller itself, but I really need to get it into  
the module eventually.

the problem is I also need to use the variable in the controller as well

class MenuController \< …  
include BasicTest

@problem\_here.set\_level(:index,4)  
def index  
end  
end

Anyway thank you very much I will continue to fool around with  
instance\_variable\_set! Hope the new information helped and did not  
confuse!

Matt
