0
what is constant
3 odpowiedzi
+ 2
Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.
Constants may not be defined within methods. Referencing an uninitialized constant produces an error. Making an assignment to a constant that is already initialized produces a warning.
#!/usr/bin/ruby
class Example
VAR1 = 100
VAR2 = 200
def show
puts "Value of first Constant is #{VAR1}"
puts "Value of second Constant is #{VAR2}"
end
end
# Create Objects
object=Example.new()
object.show
Here VAR1 and VAR2 are constant. This will produce the following result:
Value of first Constant is 100
Value of second Constant is 200
0
A little more explanation:
"A constant is a variable whose value cannot be modified once it has been assigned".
- 1
variables beginning with the capital letters