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

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

问题描述

我想将一个对象保存到一个文件中,然后轻松地从文件中读取它.作为一个简单的例子,假设我有以下 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.

推荐答案

参见 Marshal:http://ruby-doc.org/core/classes/Marshal.html

See Marshal: http://ruby-doc.org/core/classes/Marshal.html

-或-

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

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

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