我正在制作yaml file
动态Ruby
我有以下哈希
{
"defaults"=>
{"foo"=>"bar", "zip"=>"button"},
"node"=>
{
"<<"=>
{"foo"=>"bar", "zip"=>"button"},
"foo"=>"other"
}
}
当我尝试使用解析它时
tree = Psych.parse your_data
data = ToRubyNoMerge.new.accept tree
像这里一样覆盖解析器函数
require 'psych'
class ToRubyNoMerge < Psych::Visitors::ToRuby
def revive_hash hash, o
if o.anchor
@st[o.anchor] = hash
hash.instance_variable_set "@_yaml_anchor_name", o.anchor
end
o.children.each_slice(2) { |k,v|
key = accept(k)
hash[key] = accept(v)
}
hash
end
end
class MyEmitter < Psych::Visitors::Emitter
def visit_Psych_Nodes_Mapping o
o.anchor = 'defaults' if o.anchor
super
end
def visit_Psych_Nodes_Alias o
o.anchor = 'defaults' if o.anchor
super
end
end
现在当我尝试
tree = Psych.dump yaml_constants
data = ToRubyNoMerge.new.accept tree
File.open(file, 'w') { |f| YAML.dump(data.to_yaml, f) }
它给了我以下错误
psych/visitors/to_ruby.rb:23:in `initialize':参数数量错误(给定 0,预期 2)(ArgumentError)
我们可以看到它是有道理的,因为revive_hash
它接受了两个参数,但同样的事情对这个家伙有效。谁能告诉我我做错了什么
注意:我正在关注这篇文章进行解析并询问@matt 这个问题,但他没有回应