diff options
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 20 |
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; |