0
Which value will the constants hold if we assign two values to a single constant
Constant are those that begin with capital letter. ww are assigning two values say X=4 and then X=7. which value will it have? we are given a warning message when we try to change the value say X=4 X=7 puts X need result of this Constant X
4 Respostas
+ 1
u cant change the value of a constant why do u even bother trying to change it to 4
0
i am just giving it a try to chek it. when i tried it it gave the value 7 and not 4
0
You've asked a good point, most people usually overlook these small stuffs.
In Ruby you can change the value of a Constant.
-_-
That's why you're getting 7 instead of 4.
X = 7
X = 4 # Will raise a warning, (eval):1: warning: already initialized constant X
But, that's just a warning, it'll still assign X as 4.
This is thanks to some of the weird decisions of Matz.
If you truly want a value to be immutable, freeze the variable,
MY_CONSTANT = "foo".freeze
MY_CONSTANT << "bar" # => RuntimeError: can't modify frozen string
Example taken from, http://blog.honeybadger.io/when-to-use-freeze-and-frozen-in-ruby/
0
thank u for ur clarification