-
-
Save anonymous/4f39fe276dcf168ffa76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/test/ruby/test_variable.rb b/test/ruby/test_variable.rb | |
index f04e358..6e69d56 100644 | |
--- a/test/ruby/test_variable.rb | |
+++ b/test/ruby/test_variable.rb | |
@@ -103,6 +103,15 @@ class TestVariable < Test::Unit::TestCase | |
assert_in_out_err(["-e", "$0='t'*1000;print $0"], "", /\At+\z/, []) | |
end | |
+ def test_global_variables_captures | |
+ '' =~ // | |
+ refute global_variables.include?(:$1) | |
+ 'a' =~ /(.)/ | |
+ assert global_variables.include?(:$1) | |
+ 'ab' =~ /(.)(.)/ | |
+ assert global_variables.include?(:$2) | |
+ end | |
+ | |
def test_global_variable_poped | |
assert_nothing_raised { | |
EnvUtil.suppress_warning { | |
diff --git a/variable.c b/variable.c | |
index bb8b6db..00138cc 100644 | |
--- a/variable.c | |
+++ b/variable.c | |
@@ -12,6 +12,7 @@ | |
**********************************************************************/ | |
#include "internal.h" | |
+#include "ruby/re.h" | |
#include "ruby/st.h" | |
#include "ruby/util.h" | |
#include "constant.h" | |
@@ -901,14 +902,17 @@ VALUE | |
rb_f_global_variables(void) | |
{ | |
VALUE ary = rb_ary_new(); | |
- char buf[2]; | |
- int i; | |
+ VALUE backref = rb_backref_get(); | |
+ char buf[7]; | |
+ int i, len; | |
st_foreach_safe(rb_global_tbl, gvar_i, ary); | |
+ if (RTEST(backref)) { | |
buf[0] = '$'; | |
- for (i = 1; i <= 9; ++i) { | |
- buf[1] = (char)(i + '0'); | |
- rb_ary_push(ary, ID2SYM(rb_intern2(buf, 2))); | |
+ for (i = 1; i < RMATCH_REGS(backref)->num_regs; ++i) { | |
+ len = snprintf(buf + 1, 6, "%d", i); | |
+ rb_ary_push(ary, ID2SYM(rb_intern2(buf, len + 1))); | |
+ } | |
} | |
return ary; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment