Ruby 自定义类与 YAML 之间的转换;

发布于 2024-09-04 15:37:40 字数 1256 浏览 1 评论 0原文

我在反序列化我写入 YAML 的 ruby​​ 类时遇到问题。

我想去的地方

我希望能够将一个对象作为完整的“问题”传递,其中包括问题文本、一些可能的答案(用于多项选择)和正确答案。一个模块(编码器)接受输入,从中构建一个“问题”类并将其附加到问题池中。另一个模块读取问题池并构建“问题”对象的数组。

我目前所在的位置

示例问题池

--- |
 --- !ruby/object:MultiQ 
 a: "no"
 answer: "no"
 b: "no"
 c: "no"
 d: "no"
 text: "yes?"

编码器转储到 YAML 文件。对象是一个充满输入的 MultiQ。 (见下文。)

def dump(file, object)
  File.open(file, 'a') do |out|
    YAML.dump(object.to_yaml, out)
  end
  object = nil
end

MultiQ 类定义

class MultiQ
  attr_accessor :text, :answer, :a, :b, :c, :d

  def initialize(text, answer, a, b, c, d)
    @text = text
    @answer = answer
    @a = a
    @b = b
    @c = c
    @d = d
  end
end

解码器(我一直在尝试不同的事情,所以这里的内容不是我的第一个或最好的猜测。但我很茫然,文档并没有真正足够彻底地解释事情。 )

  File.open( "test_set.yaml" ) do |yf|
    YAML.load_documents( yf ) { |item|
      new = YAML.object_maker( MultiQ, item)
      puts new
    }
  end

您可以回答的问题

  1. 我如何实现我的目标?在解析、加载文件或文档之间,我应该使用什么方法才能成功反序列化 Ruby 类?我已经看过 YAML Rdoc,但我没有吸收太多,所以请不要只是将我链接到它。
  2. 您建议使用哪些其他方法?有没有更好的方法来存储这样的问题?我应该使用文档数据库、关系数据库、xml 吗?还有其他格式吗?

I'm having trouble deserializing a ruby class that I wrote to YAML.

Where I want to be

I want to be able to pass one object around as a full 'question' which includes the question text, some possible answers (For multi. choice) and the correct answer. One module (The encoder) takes input, builds a 'question' class out of it and appends it to the question pool. Another module reads a question pool and builds an array of 'question' objects.

Where I am currently

Sample Question Pool

--- |
 --- !ruby/object:MultiQ 
 a: "no"
 answer: "no"
 b: "no"
 c: "no"
 d: "no"
 text: "yes?"

Encoder dump to YAML file. Object is a MultiQ filled up with input. (See below.)

def dump(file, object)
  File.open(file, 'a') do |out|
    YAML.dump(object.to_yaml, out)
  end
  object = nil
end

MultiQ Class definition

class MultiQ
  attr_accessor :text, :answer, :a, :b, :c, :d

  def initialize(text, answer, a, b, c, d)
    @text = text
    @answer = answer
    @a = a
    @b = b
    @c = c
    @d = d
  end
end

The decoder (I've been trying different things, so what's here wasn't my first or best guess. But I'm at a loss and the documentation doesn't really explain things thoroughly enough.)

  File.open( "test_set.yaml" ) do |yf|
    YAML.load_documents( yf ) { |item|
      new = YAML.object_maker( MultiQ, item)
      puts new
    }
  end

Questions you can answer

  1. How do I achieve my goal? What methods should I use, between parsing, loading files or documents, to successfully deserialize a Ruby class? I've already looked over the YAML Rdoc, and I didn't absorb very much, so please don't just link me to it.
  2. What other methods would you suggest using? Is there a better way to store questions like this? Should I be using document db, relational db, xml? Some other format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2024-09-11 15:37:40

当你写入 yaml 时,你不需要首先调用 to_yaml,只需将对象本身传递给 YAML.dump( object )

这可能会导致你遇到其他问题,因为 to_yaml 的输出是一个字符串......而 YAML.dump实际上将您的对象作为字符串写入文件(这就是为什么您有一个初始的“--|”行。加载该文件的任何代码都会将该数据作为字符串加载。

加载一个像这样的单个对象:

File.open( 'test_set.yaml', 'r') { |fh|  mq_loaded = YAML.load( fh ) }

您的“新”对象使用 new 通常会令人困惑,因为 new 是一个关键字。

When you write to yaml, you don't need to first call to_yaml, just pass the object itself to YAML.dump( object )

This probably led you into other problems because the output of to_yaml was a string.. and the YAML.dump actually wrote your object as a string to to the file (that's why you have an initial "-- |" line. Anything code loading that file would load that data as a string.

Load a single object like this:

File.open( 'test_set.yaml', 'r') { |fh|  mq_loaded = YAML.load( fh ) }

The "new" you're using is generally confusing because new is a keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文