public Method

Kernel.local_variables

local_variables     array

Returns the names of the current local variables.

fred = 1
for i in 1..10

end
local_variables   #=> ["fred", "i"]

Source Code

/*
*  call-seq:
*     local_variables    => array
*  
*  Returns the names of the current local variables.
*     
*     fred = 1
*     for i in 1..10
*        # ...
*     end
*     local_variables   #=> ["fred", "i"]
*/

static VALUE
rb_f_local_variables()
{
   ID *tbl;
   int n, i;
   VALUE ary = rb_ary_new();
   struct RVarmap *vars;

   tbl = ruby_scope->local_tbl;
   if (tbl) {
       n = *tbl++;
       for (i=2; i<n; i++) {  /* skip first 2 ($_ and $~) */
           if (!rb_is_local_id(tbl[i])) continue; /* skip flip states */
           rb_ary_push(ary, rb_str_new2(rb_id2name(tbl[i])));
       }
   }

   vars = ruby_dyna_vars;
   while (vars) {
       if (vars->id && rb_is_local_id(vars->id)) { /* skip $_, $~ and flip states */
           rb_ary_push(ary, rb_str_new2(rb_id2name(vars->id)));
       }
       vars = vars->next;
   }

   return ary;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.