ruby 序列化 (Marshaling) 是如何工作的? [英] How does ruby serialization (Marshaling) work?

查看:38
本文介绍了ruby 序列化 (Marshaling) 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些关于这个主题的信息(像这个链接),但没有任何说明我知道它实际上是如何工作的.如果你不想阅读下面的文章,这里是真正的问题:

I have found some info on the subject (like this link), but nothing that tells me how it actually works under the covers. If you don't want to read the essay below, here are the real questions:

  1. 我应该如何实现 marshal_dumpmarshal_load 方法?即使是一个简单的例子也可以.

  1. How should I implement the marshal_dump and marshal_load methods? even a simple example will do.

marshal_load 被调用时,它如何知道"要创建哪种类型的对象?如果文件中有多个相同类型的对象,你怎么知道哪个是哪个?我显然很困惑...

when marshal_load is called, how does it 'know' which type of object to create? If there are multiple objects of the same type in the file, how do you tell which is which? I am obviously confused...

如果我有一个代表图像的对象,是否有不同的方式将其写入磁盘?

if I have an object which represents an image, is there a different way to write it out to disk?

我的具体问题是:

有点复杂,因为我没有要序列化的对象的源代码.

It is a bit complicated because I do not have the source code for the object I wish to serialize.

我正在开发游戏引擎的模组(使用 RGSS2 游戏库的 RPG Maker VX).有一个名为 Bitmap 的类,它属于(闭源)API.我想在游戏之间保存这个对象/图像,所以我需要将它序列化到保存文件.我不是 ruby​​ 专家,但我知道我可以定义两种方法(marshal_dumpmarshal_load),当我尝试序列化对象.

I am working on a mod to a game engine (RPG Maker VX using the RGSS2 game library). There is a class called Bitmap which belongs to the (closed source) API. I would like to save this object/image between game plays, so I need to serialize it to the save file. I'm not a ruby pro, but I know that I can define two methods (marshal_dump and marshal_load) which will be called by the "Marshal" module when I attempt to serialize the object.

问题是我不知道如何实现所需的两种方法.我实际上可以将它们保留为空方法,它似乎可以工作,但是对象实际上已被处理并且图像数据消失了.除此之外,我不明白它在内部做什么,显然创建空方法是错误的.

The problem is that I do not know how to implement the two methods needed. I can actually just leave them as empty methods and it seems to work, but the object is actually disposed and the image data is gone. Besides that, I don't understand what it is doing internally and obviously creating empty methods is just wrong.

那么谁能告诉我这些东西在内部是​​如何工作的?我认为这会帮助我解决我的问题.除此之外,是否还有另一种类型的图像格式可以使用,我可以将其保存到文件中并避免自己进行序列化?

So can anyone tell me how this stuff works internally? I think that would help me to solve my problem. Beyond that, is there another type of image format that I can use which I could just save to a file and avoid doing my own serialization?

推荐答案

Ruby 编组方法存储它们正在编码的对象的类型.这两个钩子的工作方式是这样的:

The Ruby marshalling methods store the type of the object they're encoding. The way those two hooks work is like this:

  • marshal_dump 必须返回一些描述对象状态的数据.Ruby 并不关心这些数据的格式——它必须是您的 marshal_load 方法可以用来重建对象状态的东西.

  • marshal_dump has to return some data describing the state of your object. Ruby couldn't care less about the format of this data — it just has to be something that your marshal_load method can use to reconstruct the object's state.

marshal_load 在重新创建后立即在编组对象上调用.它基本上是编组对象的初始化方法.它传递了 marshal_dump 返回的任何对象,并且必须使用该数据来重建其状态.

marshal_load is called on the marshalled object just after it's been recreated. It's basically the initialize method for a marshalled object. It's passed whatever object marshal_dump returned and has to use that data to reconstruct its state.

这是一个例子:

class Messenger

  attr_accessor :name, :message

  def marshal_dump
    {'name' => name, 'message' => message}
  end

  def marshal_load(data)
    self.name = data['name']
    self.message = data['message']
  end

end

这篇关于ruby 序列化 (Marshaling) 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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