Can a global be a constant?

Hi

I just noticed this (after 2 yrs). It appears that a
capitalized global is not considered a constant.

$D = true # ok
$D = false # ok
F = true # ok
F = false # -:4: warning: already initialized constant F

Why is this?

···


Jim Freeze

If you just try long enough and hard enough, you can always manage to
boot yourself in the posterior.
– A. J. Liebling

Hi –

···

On Fri, 9 May 2003, Jim Freeze wrote:

Hi

I just noticed this (after 2 yrs). It appears that a
capitalized global is not considered a constant.

$D = true # ok
$D = false # ok
F = true # ok
F = false # -:4: warning: already initialized constant F

Why is this?

Because a capitalized global variable is a variable :slight_smile:

Same thing with instance variables:

$ ruby -e “A=1; A=2; @B=1; @B=2”
-e:1: warning: already initialized constant A

Once something doesn’t begin with [A-Z], it can’t be a constant.

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

The first character is less like a prefix
than like a part of the name. If it starts
with a $, it doesn’t start with a capital
letter.

OTOH, constants have a big scope anyway.
Did you want to keep the scope across
files or something?

Hal

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 4:45 PM
Subject: Can a global be a constant?

I just noticed this (after 2 yrs). It appears that a
capitalized global is not considered a constant.

$D = true # ok
$D = false # ok
F = true # ok
F = false # -:4: warning: already initialized constant F

Why is this?

Ok. I didn’t realize that $, @, and @@ were considered part
of the name. Makes sense now.

···

On Friday, 9 May 2003 at 7:01:04 +0900, dblack@superlink.net wrote:

Hi –

On Fri, 9 May 2003, Jim Freeze wrote:

Hi

Because a capitalized global variable is a variable :slight_smile:

Same thing with instance variables:

$ ruby -e “A=1; A=2; @B=1; @B=2”
-e:1: warning: already initialized constant A

Once something doesn’t begin with [A-Z], it can’t be a constant.


Jim Freeze

All of the true things I am about to tell you are shameless lies.
– The Book of Bokonon / Kurt Vonnegut Jr.

Actually, what I am thinking about is how
to declare application state.
For example, if an app is run with a --verbose
or a --debug option, I want every object that
I instantiate to respond appropriately, including
threads and forks.

In addition, I think it is in bad taste to use
global variables. So I have been playing around
with constants and such. (I was comparing scope
between globals and constants when I discovered
this.)

So, I either have to relinquish my hesitation to use
global variables, or find a suitable alternative.

Do you have any suggestions?

···

On Friday, 9 May 2003 at 7:07:36 +0900, Hal E. Fulton wrote:

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 4:45 PM
Subject: Can a global be a constant?

The first character is less like a prefix
than like a part of the name. If it starts
with a $, it doesn’t start with a capital
letter.

OTOH, constants have a big scope anyway.
Did you want to keep the scope across
files or something?


Jim Freeze

Left to themselves, things tend to go from bad to worse.

Well, I’m not sure of all your needs.

I sometimes use instance variables as globals at the
top level… they act global since they go into Object’s
class, and if/when I wrap stuff in a class, I don’t
have to change them all.

I’ve also noticed that we don’t mind that a class is
global, if you know what I mean. You could always
encapsulate all your global data in a class called
State (with class variables and accessors for them).

Then you could pretend to be doing something clever
when actually you’re just using one big fancy global
variable. :slight_smile:

Hal

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 5:31 PM
Subject: Re: Can a global be a constant?

Actually, what I am thinking about is how
to declare application state.
For example, if an app is run with a --verbose
or a --debug option, I want every object that
I instantiate to respond appropriately, including
threads and forks.

In addition, I think it is in bad taste to use
global variables. So I have been playing around
with constants and such. (I was comparing scope
between globals and constants when I discovered
this.)

So, I either have to relinquish my hesitation to use
global variables, or find a suitable alternative.

Do you have any suggestions?

And you’ll see this if you try to access one of these things using the
symbol:

class Foo; end
a = Foo.new
a.instance_variable_set(:@bar,1) #>> OK
a.instance_variable_set(:bar,1) #>> NameError: ‘bar’ is not an
instance variable name

Cheers,

Brian.

···

On Fri, May 09, 2003 at 07:04:40AM +0900, Jim Freeze wrote:

Ok. I didn’t realize that $, @, and @@ were considered part
of the name. Makes sense now.

It sounds like a good fit for a singleton class. You can freeze it after
state setup is done.

Gennady.

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 3:31 PM
Subject: Re: Can a global be a constant?

On Friday, 9 May 2003 at 7:07:36 +0900, Hal E. Fulton wrote:

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 4:45 PM
Subject: Can a global be a constant?

The first character is less like a prefix
than like a part of the name. If it starts
with a $, it doesn’t start with a capital
letter.

OTOH, constants have a big scope anyway.
Did you want to keep the scope across
files or something?

Actually, what I am thinking about is how
to declare application state.
For example, if an app is run with a --verbose
or a --debug option, I want every object that
I instantiate to respond appropriately, including
threads and forks.

In addition, I think it is in bad taste to use
global variables. So I have been playing around
with constants and such. (I was comparing scope
between globals and constants when I discovered
this.)

So, I either have to relinquish my hesitation to use
global variables, or find a suitable alternative.

Do you have any suggestions?


Jim Freeze

Left to themselves, things tend to go from bad to worse.

Yet this is potentially confusing, because a
line like attr_accessor :foo does create an
accessor for @foo.

I suppose the rationale is that you’re specifying
the symbol corresponding to the method name rather
than the instance variable.

Hal

···

----- Original Message -----
From: “Brian Candler” B.Candler@pobox.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 5:44 PM
Subject: Re: Can a global be a constant?

On Fri, May 09, 2003 at 07:04:40AM +0900, Jim Freeze wrote:

Ok. I didn’t realize that $, @, and @@ were considered part
of the name. Makes sense now.

And you’ll see this if you try to access one of these things using the
symbol:

class Foo; end
a = Foo.new
a.instance_variable_set(:@bar,1) #>> OK
a.instance_variable_set(:bar,1) #>> NameError: ‘bar’ is not an
instance variable name

Ok, so how would you
access the state object in the code below without
making it a global as I did? </stupid question>

class App
State = ::Struct.new(:debug, :verbose)
$state = State.new(true, false)
def main
Work.new
end
end

class Work
def initialize
puts $state.debug
end
end

App.new.main

···

On Friday, 9 May 2003 at 7:36:18 +0900, Hal E. Fulton wrote:

----- Original Message -----

I sometimes use instance variables as globals at the
top level… they act global since they go into Object’s
class, and if/when I wrap stuff in a class, I don’t
have to change them all.

I’ve also noticed that we don’t mind that a class is
global, if you know what I mean. You could always
encapsulate all your global data in a class called
State (with class variables and accessors for them).

Then you could pretend to be doing something clever
when actually you’re just using one big fancy global
variable. :slight_smile:


Jim Freeze

You cannot propel yourself forward by patting yourself on the back.

Hi –

From: “Brian Candler” B.Candler@pobox.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 5:44 PM
Subject: Re: Can a global be a constant?

Ok. I didn’t realize that $, @, and @@ were considered part
of the name. Makes sense now.

And you’ll see this if you try to access one of these things using the
symbol:

class Foo; end
a = Foo.new
a.instance_variable_set(:@bar,1) #>> OK
a.instance_variable_set(:bar,1) #>> NameError: ‘bar’ is not an
instance variable name

Yet this is potentially confusing, because a
line like attr_accessor :foo does create an
accessor for @foo.

I suppose the rationale is that you’re specifying
the symbol corresponding to the method name rather
than the instance variable.

There are lots of fun things you can do with names when you start
setting them manually:

$ cat weird.rb
class << o=Object.new
define_method(“&!#?”) { puts “Weird method name!” }
end

o.send(“&!#?”)

$ ruby weird.rb
Weird method name!

:slight_smile:

David

···

On Fri, 9 May 2003, Hal E. Fulton wrote:

----- Original Message -----

On Fri, May 09, 2003 at 07:04:40AM +0900, Jim Freeze wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

Ok, so how would you
access the state object in the code below without
making it a global as I did? </stupid question>

class App
State = ::Struct.new(:debug, :verbose)
$state = State.new(true, false)
def main
Work.new
end
end

class Work
def initialize
puts $state.debug
end
end

App.new.main

Would this be suitable?

class App
class << self
attr_reader :state
end
State = Struct.new(:debug, :verbose)
@state = State.new(true, false)
def main
Work.new
end
end

class Work
def initialize
puts App.state.debug
end
end

App.new.main

David

···

On Fri, 9 May 2003, Jim Freeze wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Not a stupid question… maybe my assumptions
don’t work for you, but what I was thinking
was something like:

class State
@@debug = nil
@@verbose = nil

def State.debug
@@debug
end

def State.debug=(val)
@@debug = val
end

def State.verbose
@@verbose
end

def State.verbose=(val)
@@verbose = val
end

class App
def main
State.debug = true
State.verbose = false
Work.new
end
end

class Work
def initialize
puts State.debug
end
end

App.new.main

Hal

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 6:02 PM
Subject: Re: Can a global be a constant?

I’ve also noticed that we don’t mind that a class is
global, if you know what I mean. You could always
encapsulate all your global data in a class called
State (with class variables and accessors for them).

Then you could pretend to be doing something clever
when actually you’re just using one big fancy global
variable. :slight_smile:

Ok, so how would you
access the state object in the code below without
making it a global as I did? </stupid question>

class App
State = ::Struct.new(:debug, :verbose)
$state = State.new(true, false)
def main
Work.new
end
end

class Work
def initialize
puts $state.debug
end
end

App.new.main

Close.
Now, realize, I’m just now learning about this
weird class << self stuff. Is that just a fancy
way of doing:
class App
def self.state; @state; end
end

So, I think the problem with this is that
Work does not know the name of App.
So, maybe I am dreaming here, but it would
be nice if I could do the following:

class App
def main
State.new(true, true)
Work.new #=> true, true
State.new(false, false)
Work.new #=> false, false
end
end

I realize that I have not shown the object
that gets tied to the first Work.new and
another object that gets tied to the second
work.new, but you get the idea. This may
not be possible, because, I am implying that I
also want any class that Work creates to use
the same state that Work has.

In leu of this option, I guess I am back to a
single global state, which may best be implemented
in the form of a mix-in.

···

On Friday, 9 May 2003 at 8:16:04 +0900, dblack@superlink.net wrote:

Hi –

Would this be suitable?

class App
class << self
attr_reader :state
end
State = Struct.new(:debug, :verbose)
@state = State.new(true, false)
def main
Work.new
end
end

class Work
def initialize
puts App.state.debug
end
end

App.new.main


Jim Freeze

Show respect for age. Drink good Scotch for a change.

Yes. That is the truly global solution. I was
trying to do that but was getting lost somewhere.

Is there a neater (read more concise) way to define State with
maybe a << class_attr_accessor maybe?

A couple of requirements that would be nice:

  1. Only the creator of the state can change it
  2. Classes at most have to do an include to get
    access to the state.
···

On Friday, 9 May 2003 at 8:23:52 +0900, Hal E. Fulton wrote:

Not a stupid question… maybe my assumptions
don’t work for you, but what I was thinking
was something like:

class State
@@debug = nil
@@verbose = nil

def State.debug
@@debug
end

def State.debug=(val)
@@debug = val
end

def State.verbose
@@verbose
end

def State.verbose=(val)
@@verbose = val
end

class App
def main
State.debug = true
State.verbose = false
Work.new
end
end

class Work
def initialize
puts State.debug
end
end

App.new.main

Hal


Jim Freeze

VMS is like a nightmare about RSX-11M.

It seems like you missed David Black’s post, it may be the neat solution you
are looking for:
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/71000

Gennady.

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 4:37 PM
Subject: Re: Can a global be a constant?

On Friday, 9 May 2003 at 8:23:52 +0900, Hal E. Fulton wrote:

Not a stupid question… maybe my assumptions
don’t work for you, but what I was thinking
was something like:

class State
@@debug = nil
@@verbose = nil

def State.debug
@@debug
end

def State.debug=(val)
@@debug = val
end

def State.verbose
@@verbose
end

def State.verbose=(val)
@@verbose = val
end

class App
def main
State.debug = true
State.verbose = false
Work.new
end
end

class Work
def initialize
puts State.debug
end
end

App.new.main

Hal

Yes. That is the truly global solution. I was
trying to do that but was getting lost somewhere.

Is there a neater (read more concise) way to define State with
maybe a << class_attr_accessor maybe?

A couple of requirements that would be nice:

  1. Only the creator of the state can change it
  2. Classes at most have to do an include to get
    access to the state.


Jim Freeze

VMS is like a nightmare about RSX-11M.

Hi –

Not a stupid question… maybe my assumptions
don’t work for you, but what I was thinking
was something like:

class State
@@debug = nil
@@verbose = nil

def State.debug
@@debug
end

def State.debug=(val)
@@debug = val
end

def State.verbose
@@verbose
end

def State.verbose=(val)
@@verbose = val
end

class App
def main
State.debug = true
State.verbose = false
Work.new
end
end

class Work
def initialize
puts State.debug
end
end

App.new.main

Hal

Yes. That is the truly global solution. I was
trying to do that but was getting lost somewhere.

Is there a neater (read more concise) way to define State with
maybe a << class_attr_accessor maybe?

You can use instance variables (of the object State) instead of class
variables, and then State becomes:

class State
class << self
attr_accessor :debug, :verbose
end
end

(though this does not deal with the requirements you list later…)

David

···

On Fri, 9 May 2003, Jim Freeze wrote:

On Friday, 9 May 2003 at 8:23:52 +0900, Hal E. Fulton wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Fri, 9 May 2003, Jim Freeze wrote:

On Friday, 9 May 2003 at 8:16:04 +0900, dblack@superlink.net wrote:

Hi –

Would this be suitable?

class App
class << self
attr_reader :state
end
State = Struct.new(:debug, :verbose)
@state = State.new(true, false)
def main
Work.new
end
end

class Work
def initialize
puts App.state.debug
end
end

App.new.main

Close.
Now, realize, I’m just now learning about this
weird class << self stuff. Is that just a fancy
way of doing:
class App
def self.state; @state; end
end

No, it’s actually a simpler way :slight_smile: (using attr_reader instead of
writing the method manually)

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Fri, 9 May 2003, Jim Freeze wrote:

A couple of requirements that would be nice:

  1. Only the creator of the state can change it

Hmmm… what would that mean exactly if the state is global?
Who is considered to have created it – the State class, or
the class that does “State.debug = true”? But can’t more
than one class do that?

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

No. I saw the post and responded to it.
I am awaiting his response.

···

On Friday, 9 May 2003 at 8:51:29 +0900, Gennady wrote:

It seems like you missed David Black’s post, it may be the neat solution you
are looking for:
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/71000


Jim Freeze

Practical people would be more practical if they would take a little
more time for dreaming.
– J. P. McEvoy