# Instance Var Scope in Modules?

**URL:** <https://www.ruby-forum.com/t/instance-var-scope-in-modules/138052>\
**Category:** Rails\
**Created:** [May 19, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/instance-var-scope-in-modules/138052 "2008-05-19T21:31:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [May 19, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/instance-var-scope-in-modules/138052/1 "2008-05-19T21:31:19Z")

</div>

I’ve created a module to include in a Controller. Basically it works,  
but I am expecting methods in the module to have access to instance  
variables from the controller, and that appears to not be the case.  
Yet, a simple Ruby example says it should be. I’m not sure what’s  
different.

Simple Example:

```
module ShapeStuff
  def set_type
    @type = 'circle'
  end
end

class Shape
  include ShapeStuff
  attr_accessor :type
  def initialize
    @type = 'square'
  end
end

x = Shape.new
puts x.type # 'square'

x.set_type
puts x.type # 'circle'

```

My distilled Rails example would be something like this. I require  
the file in environment.rb.

```
module EventControls
  def change_test_var
    @test_var = 'y'
  end
end

class UsersAdminController < ApplicationController

  include EventControls
  layout '1col_admin'
  attr_accessor :test_var

  def some_action
    @test_var = 'x'
    change_test_var
  end
end

```

Using debug(@test\_var) the value doesn’t ever appear to change from ‘x’

Additionally, I expected session to be available to the module  
methods too, but it isn’t.

Confused…

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [May 19, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/instance-var-scope-in-modules/138052/2 "2008-05-19T22:16:53Z")

</div>

On 19 May 2008, at 20:30, Greg W. wrote:

> class UsersAdminController \< ApplicationController

I would expect this to work, and in a quick experiment it did. Is  
there any more to your example ?

Fred

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [May 19, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/instance-var-scope-in-modules/138052/3 "2008-05-19T23:32:47Z")

</div>

On May 19, 2008, at 1:16 PM, Frederick C. wrote:

> > end
> 
> I would expect this to work, and in a quick experiment it did. Is  
> there any more to your example ?

Hmmm, well there must be. I haven’t been able to see what more my  
actual code does, but I’ll go back and have a closer look. I just  
needed a sanity check that this should work before I start tearing it  
down section by section (at this point it’s quite a bit of code). Thx.

– gw
