ruby / ruby/psych

Puzzled by \n\n behaviour in a yaml file

Open
#364 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
597
Forks
223
Avg merge
11h 23m
Merged PRs (30d)
3

Description

I could not find documentation on this behaviour so I thought it would be better to share about it and be told I'm wrong than to keep to myself.

In a piece I'm writing, I load, modify then dump a piece of data.

load then dump being called one after the other in a loop would always produce the same values. As a human reading the yaml representation, I can see an extra \n being present in the yaml that is not there in the ruby representation:

require 'yaml'

yaml = <<YAML
en:
  test: >
    multiline a
    multiline b
    multiline c
YAML

data = YAML.load(yaml)
=> {"en"=>{"test"=>"multiline a multiline b multiline c\n"}}

new_yaml = YAML.dump(data)
=> "---\nen:\n  test: 'multiline a multiline b multiline c\n\n'\n"

new_data = YAML.safe_load(new_yaml)
=> {"en"=>{"test"=>"multiline a multiline b multiline c\n"}}

There seems to be a behaviour where only 2 subsequent line returns would be considered as a actual line return:

YAML.load("---\nen:\n  test: 'this\nis\na\n\ntest'")
=> {"en"=>{"test"=>"this is a\ntest"}}

While I understand that \n in the yaml file structure might have a special meaning, I am surprised to also see it behave the same way when within a quoted piece of information.

I'm also wondering why when passing in a file using the > format, that the output is returned as a quoted string instead of re-using the same format. I understand that the code cannot keep track of the "format used" when first loading the data as we only have a Hash, but in case of | this is the format being used for a dump and it seems much more natural for a human to interact with it (main reason why I end up using yaml)

require 'yaml'

yaml = <<YAML
en:
  test: |
    multiline a
    multiline b
    multiline c
YAML

YAML.dump(YAML.load(yaml))
=> "---\nen:\n  test: |\n    multiline a\n    multiline b\n    multiline c\n"

So all of this makes me wonder if it is just about documentation as the intended behaviours or if these are symptoms of some kind of issue.

Any insight is greatly appreciated, thank you 😄

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the YAML.load, YAML.safe_load, and YAML.dump examples in a minimal Ruby script, focusing on quoted strings and the > and | scalar forms. Determine whether the newline and formatting behavior is intended YAML/Psych behavior or a defect; done means a documented explanation or a confirmed, testable issue scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.