From: Abder-Rahman Ali on 13 Jul 2010 08:42 In "why's poignant guide to Ruby" book, it mentioned that: $LOAD_PATH is a global variable. Isn't this a global "constant"? -- Posted via http://www.ruby-forum.com/.
From: Xavier Noria on 13 Jul 2010 08:59 On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali <abder.rahman.ali(a)gmail.com> wrote: > In "why's poignant guide to Ruby" book, it mentioned that: $LOAD_PATH is > a global variable. Isn't this a global "constant"? Not really. Ruby has no concept of local constants, they are global by definition. Constants can't start with a dollar, they must begin with a capital letter. To say you want a particular variable to be global you use the sigil. Global variables are distinguished by the leading dollar sign, and have no requirement about the case used in the identifier. There's for example $stdout. Constants in addition belong to classes and modules, so they may have qualified names like ActiveRecord::Base. That means: the object stored in the constant Base, that is stored in the object that is stored in the constant ActiveRecord, which happens to be a module. Variables, either local or global, have no namespaces.
From: Abder-Rahman Ali on 13 Jul 2010 09:08 Xavier Noria wrote: > On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali > <abder.rahman.ali(a)gmail.com> wrote: > >> In "why's poignant guide to Ruby" book, it mentioned that: $LOAD_PATH is >> a global variable. Isn't this a global "constant"? > > Not really. > > Ruby has no concept of local constants, they are global by definition. > Constants can't start with a dollar, they must begin with a capital > letter. > > To say you want a particular variable to be global you use the sigil. > Global variables are distinguished by the leading dollar sign, and > have no requirement about the case used in the identifier. There's for > example $stdout. > > Constants in addition belong to classes and modules, so they may have > qualified names like ActiveRecord::Base. That means: the object stored > in the constant Base, that is stored in the object that is stored in > the constant ActiveRecord, which happens to be a module. > > Variables, either local or global, have no namespaces. Thanks for your reply. So, is your point here, that anything starting with a $ is a global variable, regardless uppercase or lowercase letters. I asked my question since I know that of Rub's convention is that constants have to begin with an uppercase letter. -- Posted via http://www.ruby-forum.com/.
From: Xavier Noria on 13 Jul 2010 09:18 On Tue, Jul 13, 2010 at 3:08 PM, Abder-Rahman Ali <abder.rahman.ali(a)gmail.com> wrote: > Thanks for your reply. So, is your point here, that anything starting > with a $ is a global variable, regardless uppercase or lowercase > letters. Correct. > I asked my question since I know that of Rub's convention is that > constants have to begin with an uppercase letter. That's right. Point is sigils ($, @, @@), are considered to be part of variable names. That's why the rule is worded that way. Now that we are on it, another tidbit is that Ruby actually lets you change the value a constant holds. That's a rite of passage, embrace contradictions! :). You get a warning though.
From: Abder-Rahman Ali on 13 Jul 2010 09:28 Got your point. Thanks. -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 Prev: Programming TCP-protocol Server (MineCraft) Next: Ruby garabage collector |