停止和继续嵌入的 ruby 代码
我通过嵌入命令(rb_eval 等)从 C++ 代码中调用 Ruby 函数。有什么方法可以中途停止代码的执行,保存局部变量,然后从同一位置重新启动它吗?
I call Ruby functions from my C++ code through the embedding commands (rb_eval and the like). Is there any way to stop the execution of the code partway, save the local variables, and restart it from the same spot later?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想存储 Ruby 变量供以后使用,您需要使用名为 编组。创建一个类,您可以在其中存储要保存的所有变量,并使用
Marshal::dump
将该类存储到文件中。稍后可以使用Marshal::load
将数据重新构造为 Ruby 变量。从特定点重新启动代码可能并不那么容易。您可以封送类和数据,但不一定是整个 Ruby 解释器本身的状态。一种可能性是在编组数据中存储足够的状态信息,以便您重新加载数据并确定需要从何处获取。
If you want to store Ruby variables for use later, you want to use a feature called Marshaling. Create a class in which you can store all variables you wish to save, and use
Marshal::dump
to store the class into a file. The data can be reconstituted into a Ruby variable again later by usingMarshal::load
.Restarting your code from a particular point might not be as easy. You can marshal classes and data but not necessarily the state of the entire Ruby interpreter itself. One possibility is to store enough state information in your marshaled data to let you re-load the data and figure out where you need to pick up.