如何将对象保存到文件? [英] How can I save an object to a file?

查看:130
本文介绍了如何将对象保存到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个对象保存到文件中,然后轻松地从文件中读取它.作为一个简单的示例,可以说我有以下3d数组:

I would like to save an object to a file, and then read it from the file easily. As a simple example, lets say I have the following 3d array:

m = [[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]],
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]]

是否有一个简单的Ruby API,无需编程解析器即可解释文件中的数据,我就可以使用它来实现此目的?在示例中,我给出了一个简单的例子,但是随着对象变得越来越复杂,使对象持久化变得很烦人.

Is there an easy Ruby API that I can use to achieve this without programming a parser to interpret the data from the file? In the example I give it is easy, but as the objects become more complicated, it gets annoying to make objects persistent.

推荐答案

请参阅元帅: http ://ruby-doc.org/core/classes/Marshal.html

-或-

YAML: http://www.ruby-doc.org/core/classes/YAML.html

这篇关于如何将对象保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆