summaryrefslogtreecommitdiff
path: root/variable.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-14 08:36:49 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-01-14 08:36:49 (GMT)
commit92e803c9c8e7cebe937c5ba05312417dbf99d688 (patch)
tree01475f3c7b7fe9a38d1d563e8358f25a19e41cb3 /variable.c
parent1301d1f5bccc10daf93c816ef4e177776d0668de (diff)
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 26d9a4b..b53d0c1 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;