-1

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?

Community
  • 1
  • 1
pankajdoharey
  • 1,562
  • 19
  • 30

1 Answers1

2

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:

Community
  • 1
  • 1
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364