Shouldn't this be called a global constant?

Discussion in 'Ruby' started by Abder-Rahman Ali, Jul 13, 2010.

  1. 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/.
     
    Abder-Rahman Ali, Jul 13, 2010
    #1
    1. Advertising

  2. Abder-Rahman Ali

    Xavier Noria Guest

    On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali
    <> 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.
     
    Xavier Noria, Jul 13, 2010
    #2
    1. Advertising

  3. Xavier Noria wrote:
    > On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali
    > <> 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/.
     
    Abder-Rahman Ali, Jul 13, 2010
    #3
  4. Abder-Rahman Ali

    Xavier Noria Guest

    On Tue, Jul 13, 2010 at 3:08 PM, Abder-Rahman Ali
    <> 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.
     
    Xavier Noria, Jul 13, 2010
    #4
  5. Abder-Rahman Ali, Jul 13, 2010
    #5
  6. [Note: parts of this message were removed to make it a legal post.]

    On Tue, Jul 13, 2010 at 1:59 PM, Xavier Noria <> wrote:

    > On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali
    > <> 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.
    >


    On Tue, Jul 13, 2010 at 2:18 PM, Xavier Noria <> wrote:

    > On Tue, Jul 13, 2010 at 3:08 PM, Abder-Rahman Ali
    > <> 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 Ruby'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.
    >


    Well, that's a good question that hadn't occurred to me in over 8 years, and
    two good succinct replies.

    The following is intended to test my understanding, and any corrections are
    welcome.

    1. A constant has a "constant" reference to an object. You can change the
    "internal" state of that object, and Ruby won't object or warn you.

    2. Ruby lets you change the object that a constant "refers to", albeit with
    a warning.

    irb
    CK = "actor" #=> "actor"
    CK << " - Salome" #=> "actor - Salome"
    CK = "actress" #=> warning: already initialized constant CK
    #=> "actress"
     
    Colin Bartlett, Jul 14, 2010
    #6
  7. Colin Bartlett wrote:
    > The following is intended to test my understanding, and any corrections
    > are
    > welcome.
    >
    > 1. A constant has a "constant" reference to an object. You can change
    > the
    > "internal" state of that object, and Ruby won't object or warn you.
    >
    > 2. Ruby lets you change the object that a constant "refers to", albeit
    > with
    > a warning.
    >
    > irb
    > CK = "actor" #=> "actor"
    > CK << " - Salome" #=> "actor - Salome"
    > CK = "actress" #=> warning: already initialized constant CK
    > #=> "actress"


    Yes, that's all correct.

    String#replace and File#reopen are useful in these cases.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jul 14, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,136
  2. Martin Magnusson
    Replies:
    2
    Views:
    537
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    594
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    359
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    13,037
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page