相关疑难解决方法(0)

使用 YAML.load_file 时升级到 Ruby 3.1 会导致 Psych::DisallowedClass 异常

升级到 ruby​​ 3.1 时,我在使用时看到以下排序错误消息YAML.load_file some_file_name

 Psych::DisallowedClass:
   Tried to load unspecified class: Matrix
Run Code Online (Sandbox Code Playgroud)

其他加载语句会导致类似的错误,但引用不同的未指定类,例如 OpenStruct。看来最新版本的 YAML 只加载允许的白名单中的类,因此需要使用 allowed_class 关键字来允许其他类。我努力了

hsh = YAML.load_file some_file_name, permitted_classes: [Matrix, OpenStruct]
Run Code Online (Sandbox Code Playgroud)

但这给出了错误

 Psych::DisallowedClass:
   Tried to load unspecified class: Symbol
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

ruby yaml activesupport

54
推荐指数
3
解决办法
2万
查看次数

标签 统计

activesupport ×1

ruby ×1

yaml ×1