# Global variable in helper function

**URL:** <https://www.ruby-forum.com/t/global-variable-in-helper-function/139874>\
**Category:** Rails\
**Created:** [June 8, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/global-variable-in-helper-function/139874 "2008-06-08T16:22:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 8, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/global-variable-in-helper-function/139874/1 "2008-06-08T16:22:43Z")

</div>

I have a helper function that generates a tree and i’m trying to get the  
sum of time for each element in the whole tree.

def tree(time)  
print element  
time = element.time  
tree(time)  
end function

the script works but i’m having trouble getting the total time. as i  
currently have it set up the total\_time is reset each time it starts a  
new branch.

is there some way i can set up a global variable or something similar in  
my helper function so when the tree goes back up a branch i don’t lose  
the time calculated for that branch?

you can see what i mean here:  
[http://wl-dev.kuliksco.com/manufactures/247](http://wl-dev.kuliksco.com/manufactures/247)

i am only getting the total time for the last branch in the tree.

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [June 8, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/global-variable-in-helper-function/139874/2 "2008-06-08T19:57:38Z")

</div>

nevermind i got. i didn’t realize i could use an instance variable in  
the helper
