Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Shouldn't this be called a global constant?

Reply
Thread Tools

Shouldn't this be called a global constant?

 
 
Abder-Rahman Ali
Guest
Posts: n/a
 
      07-13-2010
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/.

 
Reply With Quote
 
 
 
 
Xavier Noria
Guest
Posts: n/a
 
      07-13-2010
On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali
<(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Abder-Rahman Ali
Guest
Posts: n/a
 
      07-13-2010
Xavier Noria wrote:
> On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali
> <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Xavier Noria
Guest
Posts: n/a
 
      07-13-2010
On Tue, Jul 13, 2010 at 3:08 PM, Abder-Rahman Ali
<(E-Mail Removed)> 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.

 
Reply With Quote
 
Abder-Rahman Ali
Guest
Posts: n/a
 
      07-13-2010
Got your point. Thanks.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Colin Bartlett
Guest
Posts: n/a
 
      07-14-2010
[Note: parts of this message were removed to make it a legal post.]

On Tue, Jul 13, 2010 at 1:59 PM, Xavier Noria <(E-Mail Removed)> wrote:

> On Tue, Jul 13, 2010 at 2:42 PM, Abder-Rahman Ali
> <(E-Mail Removed)> 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 <(E-Mail Removed)> wrote:

> On Tue, Jul 13, 2010 at 3:08 PM, Abder-Rahman Ali
> <(E-Mail Removed)> 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"

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      07-14-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is a button Click event also called when a textbox TextChanged event is called??? S_K ASP .Net 6 11-08-2007 07:44 PM
What is called carry chain structure in FPGA is called in IC? Weng Tianxiang VHDL 6 09-12-2007 07:19 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments