ruby-changes:60314
From: Jeremy <ko1@a...>
Date: Sat, 7 Mar 2020 06:08:07 +0900 (JST)
Subject: [ruby-changes:60314] f991340e07 (master): Document defined? and global_variables handling of regexp global variables [ci skip]
https://git.ruby-lang.org/ruby.git/commit/?id=f991340e07 From f991340e07e76038b044d73ef41bd0cdeb68cf62 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Fri, 6 Mar 2020 13:03:58 -0800 Subject: Document defined? and global_variables handling of regexp global variables [ci skip] Fixes [Bug #11304] diff --git a/doc/syntax/miscellaneous.rdoc b/doc/syntax/miscellaneous.rdoc index 87ec059..d5cfd3e 100644 --- a/doc/syntax/miscellaneous.rdoc +++ b/doc/syntax/miscellaneous.rdoc @@ -83,6 +83,36 @@ Using the specific reflection methods such as instance_variable_defined? for https://github.com/ruby/ruby/blob/trunk/doc/syntax/miscellaneous.rdoc#L83 instance variables or const_defined? for constants is less error prone than using +defined?+. ++defined?+ handles some regexp global variables specially based on whether +there is an active regexp match and how many capture groups there are: + + /b/ =~ 'a' + defined?($~) # => "global-variable" + defined?($&) # => nil + defined?($`) # => nil + defined?($') # => nil + defined?($+) # => nil + defined?($1) # => nil + defined?($2) # => nil + + /./ =~ 'a' + defined?($~) # => "global-variable" + defined?($&) # => "global-variable" + defined?($`) # => "global-variable" + defined?($') # => "global-variable" + defined?($+) # => nil + defined?($1) # => nil + defined?($2) # => nil + + /(.)/ =~ 'a' + defined?($~) # => "global-variable" + defined?($&) # => "global-variable" + defined?($`) # => "global-variable" + defined?($') # => "global-variable" + defined?($+) # => "global-variable" + defined?($1) # => "global-variable" + defined?($2) # => nil + == +BEGIN+ and +END+ +BEGIN+ defines a block that is run before any other code in the current file. diff --git a/eval.c b/eval.c index d5154b7..a68c26a 100644 --- a/eval.c +++ b/eval.c @@ -1996,7 +1996,10 @@ f_current_dirname(VALUE _) https://github.com/ruby/ruby/blob/trunk/eval.c#L1996 * call-seq: * global_variables -> array * - * Returns an array of the names of global variables. + * Returns an array of the names of global variables. This includes + * special regexp global variables such as <tt>$~</tt> and <tt>$+</tt>, + * but does not include the numbered regexp global variables (<tt>$1</tt>, + * <tt>$2</tt>, etc.). * * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr] */ -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/