views:

322

answers:

3

Is there a way to access everything in the symbol table in Ruby? I want to be able to serialize or otherwise save the current state of a run of a program. To do this, it seems I need to be able to iterate over all the variables in scope.

A: 

I don't believe there is, but you could always use marshall dump/load.

Allyn
+1  A: 

If I have understood your question properly - that you would like to see all the symbols in your program then the following should do the trick:

puts Symbol.all_symbols.inspect

The “all_symbols” class method will return an Array of every Symbol currently in the program.

andHapp
+4  A: 
Geo
Be careful: you have to initialize my_array outside the loop, and as a result it will be included when you iterate over local_variables. You should add "unless var == 'my_array'" just before the closing curly brace to ignore it.
Bkkbrad
Of course. I was only showing snippets .
Geo