aboutsummaryrefslogtreecommitdiffstats
path: root/variable.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-14 08:36:49 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-14 08:36:49 +0000
commitb6a69b9e762e1bb2e81c2b978f2635166fee49c3 (patch)
tree01475f3c7b7fe9a38d1d563e8358f25a19e41cb3 /variable.c
parent8801161f73398580d54760239553065caf28c9bb (diff)
downloadruby-b6a69b9e762e1bb2e81c2b978f2635166fee49c3.tar.gz
variable.c: matched backrefs only
* variable.c (rb_f_global_variables): add matched back references only, as well as defiend? operator. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53534 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'variable.c')
-rw-r--r--variable.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/variable.c b/variable.c
index 26d9a4b944..b53d0c1d1d 100644
--- a/variable.c
+++ b/variable.c
@@ -880,15 +880,25 @@ VALUE
rb_f_global_variables(void)
{
VALUE ary = rb_ary_new();
+ VALUE sym, backref = rb_backref_get();
rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary);
- if (!NIL_P(rb_backref_get())) {
+ if (!NIL_P(backref)) {
char buf[2];
- int i;
+ int i, nmatch = rb_match_count(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 <= nmatch; ++i) {
+ if (!rb_match_nth_defined(i, backref)) continue;
+ if (i < 10) {
+ /* probably reused, make static ID */
+ buf[1] = (char)(i + '0');
+ sym = ID2SYM(rb_intern2(buf, 2));
+ }
+ else {
+ /* dynamic symbol */
+ sym = rb_str_intern(rb_sprintf("$%d", i));
+ }
+ rb_ary_push(ary, sym);
}
}
return ary;