Take the 2-minute tour ×

Possible Duplicate:
Throw exception when re-assigning a constant in Ruby?

When we use a capital letter word in ruby, it is a constant: CONSTANT = "alive". When I modify this "supposed" constant, it gives an error, but modifies it anyway:

(irb):27: warning already initialized constant CONSTANT => "13".

This seems like an odd behavior. If I am designing a game and need a value to be constant, say: Cheatcode_health = true, and by accident, the value gets assigned as false or 0, it would be an unusual behavior. There could be lot of implications. In short is there a true constant in Ruby?

share|improve this question

marked as duplicate by Simone Carletti, R. Martinho Fernandes, marcog, Jörg W Mittag, Andrew Grimm Mar 15 '11 at 22:04


If it hurts when you do that, don't do that. –  R. Martinho Fernandes Mar 15 '11 at 9:27
Please put punctuations, and do capitalizations correctly. It was hard to see where the sentence ends. –  sawa Mar 15 '11 at 9:52

1 Answer 1

Ruby is a very permissive language. There's no way to raise an error if you re-assign a constant.

The only workaround is to create a custom method to assign values to constants and have this method do the check for you.

Other related questions:

share|improve this answer
This Question is a duplicate ... yes how do i close this question –  pankajdoharey Mar 15 '11 at 9:30
@pankaj: you need 3000 reputation to vote to close questions. Don't worry about it, we'll close it eventually. –  R. Martinho Fernandes Mar 15 '11 at 9:50
