Is it possible to determine the uncertainty of a variable in a ruby?

In ruby, is there a way to “undetermine” a variable or constant after it is defined?

In our environments rails we determine that one of the three constants should be true, depending on the environment: TESTING, DEVELOPMENTor PRODUCTION. Then in the controller code we use defined?to find out which environment we are in, i.e. defined? PRODUCTION.

Now I want the unit test to perform some of these environment-specific actions. My initial attempt was to simply set the appropriate constant in my test, and then reset them in stall mode. However, I cannot figure out how to reset DEVELOPMENTand PRODUCTIONthose that defined?return false.

Obviously, the solution will be to simply check whether the corresponding constant is additionally validated if it is defined, but this will lead to the need to touch on a significant amount of existing code.

EDIT: I understand that this is definitely NOT the right way to do something. Alas, changing is not a trivial task, so I'm looking for an easy way to just unit test what's there right now. In addition, I am also just curious to learn about the downstream language about whether it is possible to determine the uncertainty of a variable / constant.

+5
source share
4 answers

I found this answer

Object.send(:remove_const, "TESTING")

, send , remove_const . , , , ; grepsedawk. ​​

+8

, , RubyOnRails, Rails ... RAILS_ENV, - ...

if (RAILS_ENV == "production") ...

, :

RAILS_ENV.include?("development")
+5

/ , . , , .

, , , ENVIRON (DEV, TEST, PROD) - .

Then you can simply compare ENVIRONwith one of three for each action you want to perform.

+3
source

Instead of checking each constant, why not do something like:

environment = :TESTING

if(environment == :TESTING)
+1
source

All Articles