Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to global variables

Tags:

ruby

I have a program that will grab several global settings from an API when first logged in. These values are then used extensively throughout the program. Currently I am storing them in global variables, but it does not seem very OOP.

What are the alternatives to global variables for storing extensively used settings? Use constants? Class variables? Where would I initialize the values through the API call, since this would only need to happen once? I have seen some examples that instantiate a class to get to the variables but that does not make much sense to me.

I would like to set the values on login and after this call the variables everywhere else with a simple expression like Global.myvalue or GLOBAL_MYVALUE

like image 812
Augusto Samamé Barrientos Avatar asked Dec 02 '25 10:12

Augusto Samamé Barrientos


1 Answers

The Singleton Pattern might be handy for this:

https://ruby-doc.org/stdlib-2.1.0/libdoc/singleton/rdoc/Singleton.html

like image 193
seph Avatar answered Dec 05 '25 00:12

seph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!