Reputation: 1267
What is the difference between constant variables and global variables?
CONSTANT = 100
$global = 100
I read this question but I can't understand.
Upvotes: 0
Views: 1299
Reputation: 10997
Global variables are global, meaning that even if you put them in a class which is very specifically scoped, they're still available everywhere. They are also explicitly variables (meaning one should not be surprised if their value changes).
For example:
module TopLevel
module MiddleLevel
module LowLevel
class SpecificSomething
$my_global = "duff man says a lot of things"
end
end
end
end
module TopLevel
def self.global
p $my_global
end
end
TopLevel.global
#=> "duff man says a lot of things"
Constants are accessible where they are defined - that is, they are NOT global. They are also constants (as the link you provided points out), so one would NOT expect them to change (although ruby does allow them to be changed).
Upvotes: 3