简体   繁体   English

js-yaml -> 解析 ruby​​ 类型

[英]js-yaml -> Parse ruby types

I am making a Ruby console in JavaScript and thus need to accurately send Ruby object information to the renderer process.我正在用 JavaScript 制作一个 Ruby 控制台,因此需要准确地将 Ruby 对象信息发送到渲染器进程。 I decided to use YAML given that it is super easy to use YAML on the Ruby side and contains all information required.我决定使用 YAML,因为在 Ruby 端使用 YAML 非常容易,并且包含所需的所有信息。 But I keep getting errors.但我不断收到错误。

Take the following ruby:拿下面的红宝石:

[Test,Test.new].to_yaml

This converts to the following YAML:这将转换为以下 YAML:

---
- !ruby/class 'Test'
- !ruby/object:Test {}

When I try to parse this with JS-YAML we get the following error:当我尝试使用JS-YAML解析它时,我们收到以下错误:

unknown tag !<!ruby/class> at line 2, column 21:
    - !ruby/class 'Test'
                        ^

So I expect this is happenning because YAML in JavaScript doesn't have Ruby types!所以我希望这会发生,因为 JavaScript 中的 YAML 没有 Ruby 类型! I've seen that one solution is to create new YAML types to handle this data:我已经看到一种解决方案是创建新的 YAML 类型来处理这些数据:

var RubyClassType = new jsyaml.Type('!ruby/class', {
  kind: 'class'
});

However, in an ideal world I wouldn't have to define each individual type.但是,在理想的世界中,我不必定义每个单独的类型。 In an ideal world either, all unknown types will be treated the same (eg as yaml sequences) or ruby wouldn't generate the odd ruby types in the first place.在理想的世界中,所有未知类型都将被视为相同(例如作为 yaml 序列),否则 ruby​​ 不会首先生成奇怪的 ruby​​ 类型。 Can I get around this issue without having to define every Ruby type in JavaScript?我可以在不必在 JavaScript 中定义每个 Ruby 类型的情况下解决这个问题吗?

Looks like this can be handled in JS-YAML as follows: handle_unknown_types.js看起来这可以在 JS-YAML 中处理如下: handle_unknown_types.js

Won't accept this as an answer though as a ruby-first solution would be better.不会接受这个作为答案,因为 ruby​​-first 解决方案会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM