Why can't I use $3somevar for global variable in ruby 1.8.0?

Hi, I’m new to Ruby programming and I just upgraded from 1.6.8 to 1.8.0
preview3. I noticed that I can no longer define global variables like
$3somevar (an example of the error I get is attached below). Ruby 1.8
seems to complain about the 3 following the $ where it was perfectly
fine with it in 1.6.8. Is there a reason for this? Thanks.

Regards,
Donglai

···

%ruby example.rb
example.rb:1: warning: useless use of a variable in void context
example.rb:1: syntax error
$3somevar = “whatever”
^

Huh?

dcarrera ~ $ ruby -v
ruby 1.8.0 (2002-12-24) [sparc-solaris2.8]
dcarrera ~ $ irb

$3somevar = “whatever”
=> “whatever”

What’s the problem?

Try the above and see if it works for you. The problem might be
elsewhere.

···

On Fri, Jun 27, 2003 at 12:39:10PM +0900, Donglai Gong wrote:

Hi, I’m new to Ruby programming and I just upgraded from 1.6.8 to 1.8.0
preview3. I noticed that I can no longer define global variables like
$3somevar (an example of the error I get is attached below). Ruby 1.8
seems to complain about the 3 following the $ where it was perfectly
fine with it in 1.6.8. Is there a reason for this? Thanks.

Regards,
Donglai


%ruby example.rb
example.rb:1: warning: useless use of a variable in void context
example.rb:1: syntax error
$3somevar = “whatever”
^


Daniel Carrera | OpenPGP fingerprint:
Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88
UMD (301) 405-5137 | http://www.math.umd.edu/~dcarrera/pgp.html

Hi,

Daniel Carrera dcarrera@math.umd.edu writes:

dcarrera ~ $ ruby -v
ruby 1.8.0 (2002-12-24) [sparc-solaris2.8]
dcarrera ~ $ irb

$3somevar = “whatever”
=> “whatever”

What’s the problem?

Your ruby is too old.

ChangeLog says:

···

Tue Apr 8 11:49:31 2003 Yukihiro Matsumoto matz@ruby-lang.org

(snip)

    * parse.y (yylex): disallow global variables like "$1ve".
      [ruby-core:00945]


eban

There must be a reason why $1ve is disallowed in 1.8.0 but I can’t
imagine why. Anyone knows the reason behind this change?

Donglai

···

On Friday, Jun 27, 2003, at 00:23 US/Eastern, WATANABE Hirofumi wrote:

Hi,

Daniel Carrera dcarrera@math.umd.edu writes:

dcarrera ~ $ ruby -v
ruby 1.8.0 (2002-12-24) [sparc-solaris2.8]
dcarrera ~ $ irb

$3somevar = “whatever”
=> “whatever”

What’s the problem?

Your ruby is too old.

ChangeLog says:

Tue Apr 8 11:49:31 2003 Yukihiro Matsumoto matz@ruby-lang.org

(snip)

    * parse.y (yylex): disallow global variables like "$1ve".
      [ruby-core:00945]


eban

Hi,

Donglai Gong donglai@MIT.EDU writes:

There must be a reason why $1ve is disallowed in 1.8.0 but I can’t
imagine why. Anyone knows the reason behind this change?

[ruby-core:00945]
[ruby-core:00949]

···


eban

“WATANABE Hirofumi” eban@os.rim.or.jp schrieb im Newsbeitrag
news:4518-Fri27Jun2003134257+0900-eban@os.rim.or.jp…

Hi,

Donglai Gong donglai@MIT.EDU writes:

There must be a reason why $1ve is disallowed in 1.8.0 but I can’t
imagine why. Anyone knows the reason behind this change?

In most programming languages identifiers may not start with a digit.
Same for Ruby. You can’t do class 3Foo; end either. Simply get used to
not start identifiers with numbers. The change just makes identifiers for
global variables consistent with other identifiers.

In Ruby there’s the additional point that $1, $2 etc. are used for holding
the contents of regexp sub pattern matches.

robert

In most programming languages identifiers may not start with a digit.
Same for Ruby.

I’d agree with this point.

In Ruby there’s the additional point that $1, $2 etc. are used for holding
the contents of regexp sub pattern matches.

But not this. The existence of the magic variable $_ doesn’t preclude a user
from using $_var, why should $1’s existence preclude $1var? (Or is my
hypothesis incorrect; $_var works on my version of ruby.)

“WATANABE Hirofumi” eban@os.rim.or.jp schrieb im Newsbeitrag
news:4518-Fri27Jun2003134257+0900-eban@os.rim.or.jp…

Hi,

Donglai Gong donglai@MIT.EDU writes:

There must be a reason why $1ve is disallowed in 1.8.0 but I can’t
imagine why. Anyone knows the reason behind this change?

In most programming languages identifiers may not start with a digit.
Same for Ruby. You can’t do class 3Foo; end either. Simply get used to
not start identifiers with numbers. The change just makes identifiers for
global variables consistent with other identifiers.

But $3var doesn’t start with a 3; it starts with a $.

This is historical Ruby behavior, but it seems to be
changing; I now see that @3var is also disallowed. (I
don’t think it was in the past.)

In Ruby there’s the additional point that $1, $2 etc. are used for holding
the contents of regexp sub pattern matches.

This is probably related to the change. I don’t read
ruby-core, so I can’t say for sure.

Hal

···

----- Original Message -----
From: “Robert Klemme” bob.news@gmx.net
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, June 27, 2003 3:03 AM
Subject: Re: why can’t I use $3somevar for global variable in ruby 1.8.0?


Hal Fulton
hal9000@hypermetrics.com

“Hal E. Fulton” hal9000@hypermetrics.com schrieb im Newsbeitrag
news:014d01c33cd2$c69bbdc0$0300a8c0@austin.rr.com

There must be a reason why $1ve is disallowed in 1.8.0 but I can’t
imagine why. Anyone knows the reason behind this change?

In most programming languages identifiers may not start with a digit.
Same for Ruby. You can’t do class 3Foo; end either. Simply get used
to
not start identifiers with numbers. The change just makes identifiers
for
global variables consistent with other identifiers.

But $3var doesn’t start with a 3; it starts with a $.

Hmm, you can call me picky, but I’d say it does start with a digit.
Here’s why: I regards the “$” not the first character of the identifier
but a namespace tag for the global variables namespace. Similarly there
is “@” as tag for the namespace “variables of the current instance”. You
can’t leave these things out without changing the scope of the variable,
so IMHO this is not part of the identifier.

Well, of course you can view this differently, but I suggest you
don’t…

/me scuffles away in search for a serious threat…
:slight_smile:

This is probably related to the change. I don’t read
ruby-core, so I can’t say for sure.

Me, too. Speculation is a nice thing unless carried too far. :-))

CU

robert

The character definitely is part of the name. For example, try doing
instance_variable_get on :foo instead of :@foo

Regards,

Brian.

···

On Mon, Jun 30, 2003 at 06:35:58PM +0900, Robert Klemme wrote:

But $3var doesn’t start with a 3; it starts with a $.

Hmm, you can call me picky, but I’d say it does start with a digit.
Here’s why: I regards the “$” not the first character of the identifier
but a namespace tag for the global variables namespace. Similarly there
is “@” as tag for the namespace “variables of the current instance”.