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.
有没有办法访问Ruby中符号表中的所有内容?我希望能够序列化或以其他方式保存程序运行的当前状态。为此,我似乎需要能够遍历范围内的所有变量。
3 个解决方案
#1
6
I think he comes from a perl background , and that he would like to obtain all the variables defined in a script and serialize them . This way , when he'll load the file , he'll get them back . I'm still searching about how to get a list of the variables , but serialization will be made using Marshal.dump and reading them back will be made with Marshal.load . I'll edit the post once I find out how to get a list of all defined variables .
我认为他来自perl背景,并且他想获得脚本中定义的所有变量并将它们序列化。这样,当他加载文件时,他会让他们回来。我仍在搜索如何获取变量列表,但是将使用Marshal.dump进行序列化,并使用Marshal.load进行读取。一旦找到如何获取所有已定义变量的列表,我将编辑该帖子。
EDIT : found it!
编辑:发现它!
You can get a list of all variables by calling these methods :
您可以通过调用以下方法获取所有变量的列表:
local_variables
global_variables
And if you haven't already got your serialization code , I would suggest something like this:
如果您还没有获得序列化代码,我会建议这样的事情:
- create a class or a Struct instance that holds a variable name and the value of the variable and add them in an array :
创建一个包含变量名称和变量值的类或Struct实例,并将它们添加到数组中:
local_variables.each {|var| my_array << MyVarObject.new(var,eval(var)) } # eval is used to get the value of the variable
and then serialize the array :
然后序列化数组:
data = Marshal.dump(my_array)
File.open("myfile.ser","w") do |file|
file.puts data
end
#2
4
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.
“all_symbols”类方法将返回程序中当前每个符号的数组。
#1
6
I think he comes from a perl background , and that he would like to obtain all the variables defined in a script and serialize them . This way , when he'll load the file , he'll get them back . I'm still searching about how to get a list of the variables , but serialization will be made using Marshal.dump and reading them back will be made with Marshal.load . I'll edit the post once I find out how to get a list of all defined variables .
我认为他来自perl背景,并且他想获得脚本中定义的所有变量并将它们序列化。这样,当他加载文件时,他会让他们回来。我仍在搜索如何获取变量列表,但是将使用Marshal.dump进行序列化,并使用Marshal.load进行读取。一旦找到如何获取所有已定义变量的列表,我将编辑该帖子。
EDIT : found it!
编辑:发现它!
You can get a list of all variables by calling these methods :
您可以通过调用以下方法获取所有变量的列表:
local_variables
global_variables
And if you haven't already got your serialization code , I would suggest something like this:
如果您还没有获得序列化代码,我会建议这样的事情:
- create a class or a Struct instance that holds a variable name and the value of the variable and add them in an array :
创建一个包含变量名称和变量值的类或Struct实例,并将它们添加到数组中:
local_variables.each {|var| my_array << MyVarObject.new(var,eval(var)) } # eval is used to get the value of the variable
and then serialize the array :
然后序列化数组:
data = Marshal.dump(my_array)
File.open("myfile.ser","w") do |file|
file.puts data
end
#2
4
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.
“all_symbols”类方法将返回程序中当前每个符号的数组。