# Accessing Global Variable

**URL:** <https://www.ruby-forum.com/t/accessing-global-variable/124441>\
**Category:** Rails\
**Created:** [December 28, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/accessing-global-variable/124441 "2007-12-28T17:45: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 28, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/accessing-global-variable/124441/1 "2007-12-28T17:45:38Z")

</div>

Sorry guys,

It’s a simple question but I’m a bit lost:

I’ve the following controller:

class CalendarController \< ApplicationController  
$srcArray = Array.new

def index  
@calendars = Calendar.find(:all)  
@calendars.each do |c|  
$srcArray = $srcArray \<\< c.name+’&’  
end  
@iframe = build\_iFrame  
end

def update\_calendar  
$srcArray.delete\_at(1)  
@iframe2 = build\_iFrame  
end

private  
def build\_iFrame  
src = $srcArray.to\_s  
src = src.chop  
@iframe = ‘ **blablabla;src=’+src+’;blablabla** ’  
return @iframe  
end  
end

When it’s called it builds a global array based on a DB.  
Then it returns a bold string to the View. FINE.

I’ve an ajaxed link\_to\_remote that calls the function update\_calendar.  
It’d be supposed to remove the first position of the global array and  
return the same bold string without the 1st position. But it returns  
nil.

I don’t know what’s the problem since the global array was previously  
built.

Thanks for your help.

---

<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 28, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/accessing-global-variable/124441/2 "2007-12-28T18:36:47Z")

</div>

On Dec 28, 2007, at 10:45 AM, Nuno Machado wrote:

> def index  
> end  
> When it’s called it builds a global array based on a DB.  
> Then it returns a bold string to the View. FINE.
> 
> I’ve an ajaxed link\_to\_remote that calls the function update\_calendar.  
> It’d be supposed to remove the first position of the global array and  
> return the same bold string without the 1st position. But it returns  
> nil.
> 
> I don’t know what’s the problem since the global array was previously  
> built.

I’ve not used globals very much, but if each call into a controller  
is stateless, your global is not surviving. You’d need to write it  
into session in order to do what you are suggesting.

Peace,  
Phillip

---

<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 28, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/accessing-global-variable/124441/3 "2007-12-28T20:14:27Z")

</div>

> I’ve not used globals very much, but if each call into a controller  
> is stateless, your global is not surviving. You’d need to write it  
> into session in order to do what you are suggesting.
> 
> Peace,  
> Phillip

Hi Phillip,

Yes, you’re absolutely right.  
Due to the stateless nature of the HTTP protocol, I had to write the  
array into a session variable.

Many, many thanks.

---

<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 28, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/accessing-global-variable/124441/4 "2007-12-28T21:25:13Z")

</div>

On 28 Dec 2007, at 19:14, Nuno Machado wrote:

> Hi Phillip,
> 
> Yes, you’re absolutely right.  
> Due to the stateless nature of the HTTP protocol, I had to write the  
> array into a session variable.

It’s not actually the statelessness of the http protocol: you code  
exists in a long running ruby process. However in development mode you  
code is reloaded on each request and so your global is reset to  
Array.new each time.  
Regardless a global is a bad thing to use because you’ve only got one  
global but possibly many simultaneous users of your app, so the users  
would interfere with each other.

Fred
