Title here
Summary here
Constants in Crystal can be defined using the CONSTANT
keyword. Constants are immutable once defined and can be of various data types including string, integer, and float.
Here is how you can translate the example to Crystal:
Explanation:
CONSTANT_S
is defined as a constant string.CONSTANT_N
is defined as a constant integer.CONSTANT_D
is defined as a constant float using an arithmetic expression.puts
is used to print values in Crystal.to_i
method converts a float to an integer.Math.sin
calculates the sine of an integer converted to radians.Save the code in a file named constants.cr
and use the Crystal compiler to run it.
Now that you can define and use constants in Crystal, you can explore more about the language.