使用 File.open 写入 YAML 文件会换行较长的行

cam*_*man 4 ruby yaml ruby-on-rails

我想知道File.open我是否遗漏了其中的某个方面。我正在尝试用 YAML 编写一些文档,当我附加到文件并保存它时,它会截断我的行并破坏我的格式。

这是发生的事情:

generated_file = YAML::load_file("#{Rails.root}/documentation/auto_generated/liquid_drops/#{class_name}.yml")
Run Code Online (Sandbox Code Playgroud)

我得到了我正在使用的文件,然后我所做的就是写入它:

File.open("#{Rails.root}/documentation/auto_generated/liquid_drops/#{class_name}.yml", 'w+') {|f| f.write generated_file.to_yaml }
Run Code Online (Sandbox Code Playgroud)

它会像这样截断行:

example: "{% for file in object.method.all %} -- this would be the ideal method to iterate over a file's methods"
Run Code Online (Sandbox Code Playgroud)

对此:

example: "{% for file in object.method.all %} -- this would be the ideal method
    to iterate over a file's methods"
Run Code Online (Sandbox Code Playgroud)

下面是 generated_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"
}
Run Code Online (Sandbox Code Playgroud)

cam*_*man 5

事实证明,这实际上已经在“为什么 psych yaml 解释器添加大约 80 个字符的换行符? ”中得到了回答。我正在寻找错误的东西。

做类似的事情

yaml.to_yaml(:options => {:line_width => -1})
Run Code Online (Sandbox Code Playgroud)

防止线条缠绕。