Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw exception when re-assigning a constant in Ruby?

I've long been aware that "constants" in Ruby (i.e., variable names that are capitalized) aren't really constant. Like other programming languages, a reference to an object is the only thing stored in the variable/constant. (Sidebar: Ruby does have the facility to "freeze" referenced objects from being modified, which as far as I know, isn't an ability offered in many other languages.)

So here's my question: when you re-assign a value into a constant, you get a warning like so:

>> FOO = 'bar'
=> "bar"
>> FOO = 'baz'
(irb):2: warning: already initialized constant FOO
=> "baz"

Is there a way to force Ruby to throw an exception instead of printing a warning? It's tough to figure out why reassignments happen sometimes.

like image 237
Benjamin Oakes Avatar asked Jun 11 '10 14:06

Benjamin Oakes


2 Answers

Look at Can you ask ruby to treat warnings as errors? to see how it is possible in some cases to treat warnings as errors.

Otherwise I guess you'd have to write a custom method to assign constants and raise the exception if already assigned.

If you know that a reassignment happens to a specific constant, you can also add a sanity check just before the assignment.

like image 111
averell Avatar answered Nov 04 '22 13:11

averell


You can't intercept it directly, no.

If you really need to do this, I can think of a very dirty hack, though. You could redirect the standard error IO to a custom IO object. The write method could then check for what is being written; if it contains "warning: already initialized constant", then you raise, otherwise you forward the call to the standard error's write.

like image 3
Marc-André Lafortune Avatar answered Nov 04 '22 12:11

Marc-André Lafortune