【发布时间】:2015-10-07 03:13:01
【问题描述】:
我想知道我是否缺少File.open 的某个方面。我正在尝试用 YAML 编写一些文档,当我附加到文件并完全保存它时,它会截断我的行并破坏我的格式。
这是怎么回事:
generated_file = YAML::load_file("#{Rails.root}/documentation/auto_generated/liquid_drops/#{class_name}.yml")
我得到了我正在使用的文件,然后我所做的就是写入它:
File.open("#{Rails.root}/documentation/auto_generated/liquid_drops/#{class_name}.yml", 'w+') {|f| f.write generated_file.to_yaml }
它会像这样截断行:
example: "{% for file in object.method.all %} -- this would be the ideal method to iterate over a file's methods"
到这里:
example: "{% for file in object.method.all %} -- this would be the ideal method
to iterate over a file's methods"
以下是 generate_file 的示例:
{
"class" => "account",
"methods" => [
{
"method_name" => "this_method",
"description" => "Donec sed odio dui. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio odio ma.",
"return_type" => "Array",
"example" => "{% for method in object.methods.all %}"
}
],
"general_notes" => "Most methods can use this instead of that, because this and that both inherit from the right file"
}
【问题讨论】:
-
我想出了其中的一部分,
generated_method.to_yaml是在那里添加新行的原因。我现在正在调查原因。
标签: ruby-on-rails ruby yaml