4

I'm wondering if there's an aspect of File.open that I'm missing. I'm trying to write some documentation in YAML and when I append to the file and save it at all, it truncates my lines and breaks my formatting.

Here's what's going on:

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

I get the file I'm working with, then all I do is write to it:

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

And it truncates lines like this:

example: "{% for file in object.method.all %} -- this would be the ideal method to iterate over a file's methods"

To this:

example: "{% for file in object.method.all %} -- this would be the ideal method
    to iterate over a file's methods"

Here's an example of what generated_file looks like:

{
  "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"
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
camkidman
  • 185
  • 1
  • 11
  • I figured part of it out, the `generated_method.to_yaml` is what's adding the new lines there. I'm investigating why now. – camkidman Oct 07 '15 at 03:14

3 Answers3

4

Turns out this was actually answered previously in "why does psych yaml interpreter add line breaks around 80 characters?". I was searching for the wrong thing.

Doing something like

yaml.to_yaml(:options => {:line_width => -1})

keeps the lines from wrapping.

camkidman
  • 185
  • 1
  • 11
  • 1
    There's a typo in the code. It should be: `yaml.to_yaml(:options => {:line_width => -1})` or `yaml.to_yaml(options = {:line_width => -1})` but "edits can't change less than 6 chars". :) – Marcin Mateusz Hanc Jul 05 '18 at 10:25
3

I would not call that truncating, but wrapping with indent, a decades old technique for lines that are too long e.g. for a terminal width.

In .to_yaml this width is determined by the BestWidth parameter and defaults to 80 characters. More options can be found on the documentation page under section 24.

YAML emittors for other programming languages use this wrapping as well.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Anthon
  • 69,918
  • 32
  • 186
  • 246
1

It's most likely the to_yaml method that is adding the newline. File.open and the write method should not be adding newlines. Try

f.write generated_file

or

f.write generated_file.to_s

without the to_yaml method.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
emery
  • 8,603
  • 10
  • 44
  • 51
  • I did isolate it down to that method, when I output `generated_file.to_yaml` to the console, it puts breaks in long lines for some strange reason. I might have to approach this a different way. – camkidman Oct 07 '15 at 03:28
  • Yeah, `.to_s` doesn't add the line breaks, but I figured out what it was. The default behavior of the YAML parser is to add linebreaks after 80 characters, but adding `options = {:line_width => -1}` prevents that – camkidman Oct 07 '15 at 15:27