【发布时间】:2014-02-26 07:10:14
【问题描述】:
假设我有 Line 类中的对象 line:
class Line
def initialize point1, point2
@p1 = point1
@p2 = point2
end
end
line = Line.new...
如何二进制序列化 line 对象?我试过了:
data = Marshal::dump(line, "path/to/still/unexisting/file")
但是它创建了文件并且没有添加任何东西。我阅读了 Class: IO 文档,但我无法真正理解。
【问题讨论】:
-
Marshal 不是持久存储的好选择,二进制格式取决于您使用的特定 Ruby 版本。最好使用 JSON、YAML、XML 等通用格式...
-
@muistooshort 如果您愿意在 ruby 版本更改时重新创建它,并且您有大量需要保存的数据/变量,那么这是一个不错的选择。它也很快。
标签: ruby serialization marshalling binary-serialization