# Variable Scoping Problem

**URL:** <https://www.ruby-forum.com/t/variable-scoping-problem/53023>\
**Category:** Rails\
**Created:** [January 23, 2006, 4:47am UTC](https://www.ruby-forum.com/t/variable-scoping-problem/53023 "2006-01-23T04:47:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@Ben\_Lewis](https://www.ruby-forum.com/u/Ben_Lewis)\
**Post date:** [January 23, 2006, 4:47am UTC](https://www.ruby-forum.com/t/variable-scoping-problem/53023/1 "2006-01-23T04:47:35Z")

</div>

I am having some problems with variable scoping.

I need to be able to set a variable that is accessable by other methods  
within the class (or instance) (i.e not global).

An example is 2 pages that change a class variable:

class AjtestController \< ApplicationController

```
def initialize
	@@variable = "init"
end

def show
	@display = @@variable
	@@variable = "change 1"
end

def list
	@display2 = @@variable
	@@variable = "change 2"
end

```

end

When I view ‘show’ the page prints “init” (the page just prints  
@display)  
When I view ‘list’ the page prints “init” (the page just prints  
@display2)  
This tells me that the class is being initialized (instantiated?) each  
time I go to the ‘show’ or ‘view’ page.

If i remove the initialize method, i get an  
“uninitialized class variable @@variable in AjtestController” error.

Is there something I am doing wrong here? I should be able to pass  
variables between methods in the same class.

Thanks,  
~Ben

---

<div class="post-metadata">

**Author:** ![Ben\_Lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@Ben\_Lewis](https://www.ruby-forum.com/u/Ben_Lewis)\
**Post date:** [January 23, 2006, 5:22am UTC](https://www.ruby-forum.com/t/variable-scoping-problem/53023/2 "2006-01-23T05:22:04Z")

</div>

Ben Lewis wrote:

> I am having some problems with variable scoping.
> 
> I need to be able to set a variable that is accessable by other methods  
> within the class (or instance) (i.e not global).
> 
> An example is 2 pages that change a class variable:
> 
> class AjtestController \< ApplicationController
> 
> def initialize  
> @@variable = “init”  
> end
> 
> def show  
> @display = @@variable  
> @@variable = “change 1”  
> end
> 
> def list  
> @display2 = @@variable  
> @@variable = “change 2”  
> end  
> end
> 
> When I view ‘show’ the page prints “init” (the page just prints  
> @display)  
> When I view ‘list’ the page prints “init” (the page just prints  
> @display2)  
> This tells me that the class is being initialized (instantiated?) each  
> time I go to the ‘show’ or ‘view’ page.
> 
> If i remove the initialize method, i get an  
> “uninitialized class variable @@variable in AjtestController” error.
> 
> Is there something I am doing wrong here? I should be able to pass  
> variables between methods in the same class.
> 
> Thanks,  
> ~Ben

The problem is that the object does not persist between page requests.  
Everytime you make a request the server starts from scratch, so all your  
objects and classes get set up as if there were no prior state.

Consequently, your class variable gets wiped out. Try putting stuff in  
the session hash, that does persist.

session[:variable] = “init”

\_Kevin

---

<div class="post-metadata">

**Author:** ![Ben\_Lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@Ben\_Lewis](https://www.ruby-forum.com/u/Ben_Lewis)\
**Post date:** [January 23, 2006, 5:38am UTC](https://www.ruby-forum.com/t/variable-scoping-problem/53023/3 "2006-01-23T05:38:22Z")

</div>

On Jan 22, 2006, at 8:22 PM, Kevin O. wrote:

> > ```
> > @display2 = @@variable
> > 
> > ```
> > 
> > time I go to the ‘show’ or ‘view’ page.  
> > The problem is that the object does not persist between page requests.  
> > \_Kevin  
> > And you would be better off putting the variable creation in a  
> > before\_filter rather then using initialize:

> > class AjtestController \< ApplicationController

```
  before_filter :set_var

  def set_var
        @@var = 'init'
  end

```

end

cheers-

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![Ben\_Lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@Ben\_Lewis](https://www.ruby-forum.com/u/Ben_Lewis)\
**Post date:** [January 23, 2006, 5:39am UTC](https://www.ruby-forum.com/t/variable-scoping-problem/53023/4 "2006-01-23T05:39:04Z")

</div>

> session[:variable] = “init”

Thanks for the quick reply Kevin.

I had seen this method previously. While this is an option, I would  
prefer to not use session variables as this makes the variable available  
to multiple pages at once - in the case of multi-tab browsers.

Is there other options for achieving this functionality?

---

<div class="post-metadata">

**Author:** ![Ben\_Lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@Ben\_Lewis](https://www.ruby-forum.com/u/Ben_Lewis)\
**Post date:** [January 23, 2006, 5:49am UTC](https://www.ruby-forum.com/t/variable-scoping-problem/53023/5 "2006-01-23T05:49:52Z")

</div>

Ben Lewis wrote:

> > session[:variable] = “init”
> 
> Thanks for the quick reply Kevin.
> 
> I had seen this method previously. While this is an option, I would  
> prefer to not use session variables as this makes the variable available  
> to multiple pages at once - in the case of multi-tab browsers.
> 
> Is there other options for achieving this functionality?

I suppose you could stuff it into a database table and retrieve it using  
a before\_filter like Ezra mentioned.

I must admit, I don’t fully understand your issues with the session  
hash. If you use a good naming scheme, you shouldn’t have conflicts  
between actions or controllers trying to use data they shouldn’t.

Sometimes I will do stuff like this…

session["#{controller\_name}\_variable"] = “init”

you can also do sub hashes like

session["#{controller\_name}"][“variable”] = “init”

but I don’t like this much because you run into trouble if the  
controller\_name hash hasn’t been created yet.

@variable = session["#{controller\_name}\_variable"] || “default”

will always work

@variable = session["#{controller\_name}"][“variable”] || “default”

will throw a nil exception if the controller\_name hash doesn’t exist.

---

<div class="post-metadata">

**Author:** ![Ben\_Lewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@Ben\_Lewis](https://www.ruby-forum.com/u/Ben_Lewis)\
**Post date:** [January 23, 2006, 6:02am UTC](https://www.ruby-forum.com/t/variable-scoping-problem/53023/6 "2006-01-23T06:02:06Z")

</div>

> I must admit, I don’t fully understand your issues with the session  
> hash. If you use a good naming scheme, you shouldn’t have conflicts  
> between actions or controllers trying to use data they shouldn’t.

The problem is not exactly a naming issue.

The issue would arise when an operator has 2 tabs open (on the same  
controller).

Using session variables, the session data of the second tab may corrupt  
the session data of the first tab (normally not an issue for static  
pages, but I am using AJAX to modify the variables).

To be honest - this is not a huge problem. I would just prefer not to  
have issues like this if i could help it.

~Ben
