[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Determining local variables

svy

3/6/2008 6:09:00 AM

Is there any way to determine what local variables are currently
defined? (possibly using binding? thread?)
Is there any way similar to how an array of current instance variables
can be found?
Thanks
1 Answer

Joel VanderWerf

3/6/2008 6:57:00 AM

0

svy wrote:
> Is there any way to determine what local variables are currently
> defined? (possibly using binding? thread?)
> Is there any way similar to how an array of current instance variables
> can be found?
> Thanks

Use the #local_variables and the #instance_variables methods.

class C
def foo
x = 1
y = 2
p local_variables
z = 3 # note z is included

@x = 10
@y = 20
p instance_variables
@z = 3 # note @z is _not_ included
end
end

C.new.foo

__END__

Output:

["x", "y", "z"]
["@y", "@x"]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407