Project

General

Profile

Bug #6723

Global variable scoping problem.

Added by Markus Fenske over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
ruby -v:
-
Backport:
2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
[ruby-core:46335]

Description

This issue exists from 1.8 to ruby-head.

See the following example:

def foobar
"Foobar".match(/\*)/)
puts $1.inspect
end

foobar
puts $1.inspect
puts global_variables.inspect

This demonstrates that $1 does not behave like a global variable. However it's listed by Kernel.global_variables as a global variable. And it's named like a global variable.

This leads to some confusion sometimes. See for example http://stackoverflow.com/q/11441782/773690

It may not be a good idea to fix this inconsistent naming, because it may break severaly things. However the fact that $1 and friends are not global variables should be clearly documented. And by the way: Ruby != Perl.

Or maybe make $1 a real global variable?

Associated revisions

Revision 36526
Added by Eric Hodel over 3 years ago

  • re.c (rb_reg_s_last_match): Update $~ to reference Regexp documentation about "special global variables". [Bug #6723]

Revision 36526
Added by Eric Hodel over 3 years ago

  • re.c (rb_reg_s_last_match): Update $~ to reference Regexp documentation about "special global variables". [Bug #6723]

History

#1 [ruby-core:46343] Updated by Koichi Sasada over 3 years ago

  • Assignee set to Yukihiro Matsumoto
  • Target version set to 2.0.0
  • Category set to core

#2 [ruby-core:46344] Updated by Nobuyoshi Nakada over 3 years ago

  • ruby -v changed from ruby 1.9.3p194 to -
  • Assignee changed from Yukihiro Matsumoto to Eric Hodel
  • Category changed from core to doc
  • Status changed from Open to Assigned

#3 Updated by Eric Hodel over 3 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r36526.
Markus, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • re.c (rb_reg_s_last_match): Update $~ to reference Regexp documentation about "special global variables". [Bug #6723]

#4 [ruby-core:46740] Updated by Eric Hodel over 3 years ago

These are described as "Special global variables" and are described as "thread-local and method-local" in the Regexp documentation. I updated a reference to them in re.c, but the existing documentation should be sufficient.

Also available in: Atom PDF