如何将对象保存到文件?

时间:2021-11-27 09:55:26

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:

我想将一个对象保存到一个文件,然后轻松地从文件中读取它。举个简单的例子,假设我有以下3d数组:

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

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.

是否有一个简单的Ruby API,我可以使用它来实现这一点,而无需编写解析器来解释文件中的数据?在示例中,我给它很简单,但随着对象变得更加复杂,使对象持久化变得很烦人。

3 个解决方案

#1


14  

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

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

-or-

-要么-

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

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

#2


45  

You need to serialize the objects before you could save them to a file and deserialize them to retrieve them back. As mentioned by Cory, 2 standard serialization libraries are widely used, Marshal and YAML.

您需要先序列化对象,然后才能将它们保存到文件中并对其进行反序列化以将其检索回来。正如Cory所提到的,2个标准序列化库被广泛使用,Marshal和YAML。

Both Marshal and YAML use the methods dump and load for serializing and deserializing respectively.

Marshal和YAML分别使用dump和load方法进行序列化和反序列化。

Here is how you could use them:

以下是如何使用它们:

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

# Quick way of opening the file, writing it and closing it
File.open('/path/to/yaml.dump', 'w') { |f| f.write(YAML.dump(m)) }
File.open('/path/to/marshal.dump', 'wb') { |f| f.write(Marshal.dump(m)) }

# Now to read from file and de-serialize it:
YAML.load(File.read('/path/to/yaml.dump'))
Marshal.load(File.read('/path/to/marshal.dump'))

You need to be careful about the file size and other quirks associated with File reading / writing.

您需要注意文件大小和与文件读/写相关的其他怪癖。

More info, can of course be found in the API documentation.

更多信息,当然可以在API文档中找到。

#3


3  

YAML and Marshal are the most obvious answers, but depending on what you're planning to do with the data, sqlite3 may be a useful option too.

YAML和Marshal是最明显的答案,但根据您计划对数据做什么,sqlite3也可能是一个有用的选项。

require 'sqlite3'

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

db=SQLite3::Database.new("demo.out")
db.execute("create table data (x,y,z,value)")
inserter=db.prepare("insert into data (x,y,z,value) values (?,?,?,?)")
m.each_with_index do |twod,z|
  twod.each_with_index do |row,y|
    row.each_with_index do |val,x|
      inserter.execute(x,y,z,val)
    end
  end
end

#1


14  

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

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

-or-

-要么-

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

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

#2


45  

You need to serialize the objects before you could save them to a file and deserialize them to retrieve them back. As mentioned by Cory, 2 standard serialization libraries are widely used, Marshal and YAML.

您需要先序列化对象,然后才能将它们保存到文件中并对其进行反序列化以将其检索回来。正如Cory所提到的,2个标准序列化库被广泛使用,Marshal和YAML。

Both Marshal and YAML use the methods dump and load for serializing and deserializing respectively.

Marshal和YAML分别使用dump和load方法进行序列化和反序列化。

Here is how you could use them:

以下是如何使用它们:

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

# Quick way of opening the file, writing it and closing it
File.open('/path/to/yaml.dump', 'w') { |f| f.write(YAML.dump(m)) }
File.open('/path/to/marshal.dump', 'wb') { |f| f.write(Marshal.dump(m)) }

# Now to read from file and de-serialize it:
YAML.load(File.read('/path/to/yaml.dump'))
Marshal.load(File.read('/path/to/marshal.dump'))

You need to be careful about the file size and other quirks associated with File reading / writing.

您需要注意文件大小和与文件读/写相关的其他怪癖。

More info, can of course be found in the API documentation.

更多信息,当然可以在API文档中找到。

#3


3  

YAML and Marshal are the most obvious answers, but depending on what you're planning to do with the data, sqlite3 may be a useful option too.

YAML和Marshal是最明显的答案,但根据您计划对数据做什么,sqlite3也可能是一个有用的选项。

require 'sqlite3'

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

db=SQLite3::Database.new("demo.out")
db.execute("create table data (x,y,z,value)")
inserter=db.prepare("insert into data (x,y,z,value) values (?,?,?,?)")
m.each_with_index do |twod,z|
  twod.each_with_index do |row,y|
    row.each_with_index do |val,x|
      inserter.execute(x,y,z,val)
    end
  end
end