OrangeFlash81 OrangeFlash81 - 5 months ago 8
Ruby Question

Is there a difference between `TRUE` and `true`?

There are two instances of

TrueClass
,
FalseClass
and
NilClass
with different names: one in lowercase and one in uppercase. One instance appears to evaluate to the other:

true # => true
TRUE # => true
true == TRUE # => true


Is there a difference between these two constants, and if so, what are the differences? If they are the same, which of these constants should I use in my code? Should I write
some_value = true
or
some_value = TRUE
?

Answer

The difference is that while true is a keyword in Ruby, TRUE is a constant:

true = 1
# => SyntaxError: Can't assign to true

TRUE = false
# => warning: already initialized constant TRUE
# => false

TRUE == true
# => false