# Global Count Variable

**URL:** <https://www.ruby-forum.com/t/global-count-variable/95921>\
**Category:** Rails\
**Created:** [June 19, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/global-count-variable/95921 "2006-06-19T21:02:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [June 19, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/global-count-variable/95921/1 "2006-06-19T21:02:57Z")

</div>

I would like to set up a global variable to count how many times a  
specific method is executed, but I would like the variable to keep  
counting regardless of sessions, restarting the app, etc. Is this  
possible?

Or… I could just do a simple read/write to a text file on the server,  
but I’m not finding any good tutorials or examples about simple text  
i/o… any suggestions?

Thanks!  
-stirman

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [June 19, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/global-count-variable/95921/2 "2006-06-19T21:56:32Z")

</div>

you could create another table with one record that you load and  
increase using the rails Find and Save methods for the Model.

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [June 19, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/global-count-variable/95921/3 "2006-06-19T22:09:14Z")

</div>

Seems like a lot of cpu time and effort increase an int variable, right?

Or would text file i/o be comparable?

Thanks for the suggestion, I appreciate it!

-stirman

> you could create another table with one record that you load and  
> increase using the rails Find and Save methods for the Model.

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [June 19, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/global-count-variable/95921/4 "2006-06-19T22:17:49Z")

</div>

It will take as much time, probably less, to update a field in the  
database than to update and save to a file by hand. Besides, is the site  
going to be that busy that you need to take into account system  
resources?

Jason

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [June 19, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/global-count-variable/95921/5 "2006-06-19T22:52:33Z")

</div>

On 6/19/06, Jason S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Seems like a lot of cpu time and effort increase an int variable, right?
> 
> Or would text file i/o be comparable?

Nice thing about the database method is if you put a bit 'o metadata  
in there you can use the same ActiveRecord model for incrementing  
other stuff. It will likely come down the pipes anyway… 🙂 I use  
a similar mechanism to track “ad” impressions / hits… :: shrug ::  
Performance is fine.

-Curtis

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [June 19, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/global-count-variable/95921/6 "2006-06-19T22:42:47Z")

</div>

I agree using a table/record is the best approach. But if you really  
want to  
use a file, then use the IO.File class in Ruby to open/read/write the  
file.

For details look here: [http://ruby.outertrack.com/class/IO](http://ruby.outertrack.com/class/IO)
