umm as long as a variable does not have special characters or a space in
it, as far as I am aware I thought variables are case sensitive, but
should work. The example above I see a variable needs to be ALL_CAPS or
all_lowercase, you cannot HaVe_bOtH. Is that correct?
Jordan Bedwell is correct. Ruby’s constants are /meant/ to be not
changed, but you can define a constant and change its value later. Ruby
will produce a warning ‘already initialized constant’
Also note that your constant’s internal representation can be changed
without triggering the warning.
The last is misleading. You have created a constant that points to an
Hash object. However, the Hash object itself is not frozen. The values
pointed to by the keys are not constant or frozen. You can add new
keys to the Hash, change values, etc. But if you try to set Const to
something else (any other object) it will again show the warning.
an uppercase letter) are changed after initialization.
Convention doesn’t equal required. The only reliably enforced
“convention” is that classes must begin with a capital letter, it does
not enforce CamalCase, it does not prevent Capital_Snake_Case. Ruby
does not even disallow CamelConstants (even though technically classes
are constants.)
In ruby, any variable whose name starts with a capital letter becomes a
constant.
In my previous post I was speaking more to the naming conventions used
in Ruby. Variable names are lowercase_understored and constants are
UPPER_CASE_UNDERSCORED.
The mechanics of Ruby don’t enforce this convention, outside of
presenting a warning when “constants” (any variable name starting with
an uppercase letter) are changed after initialization.
an uppercase letter) are changed after initialization.
Convention doesn’t equal required. The only reliably enforced
“convention” is that classes must begin with a capital letter, it does
not enforce CamalCase, it does not prevent Capital_Snake_Case. Ruby
does not even disallow CamelConstants (even though technically classes
are constants.)
Is that exactly what I’ve said all along? By convention (not
requirement) and name “should” be not “must” be? Or were you just +1 my
last?
Convention doesn’t equal required. The only reliably enforced
“convention” is that classes must begin with a capital letter, it does
not enforce CamalCase, it does not prevent Capital_Snake_Case. Ruby
does not even disallow CamelConstants (even though technically classes
are constants.)
Sorry, classes should be constants.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.