Puzzled by \n\n behaviour in a yaml file

Aberta
#364 2 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
25/100
Tipo de issue
Documentação
Clareza
Precisa de esclarecimento
Status de atividade
Estagnada
Stack de tecnologia
ruby
Domínio
tooling

Direção de pesquisa

Comece reproduzindo os exemplos de YAML.load, YAML.safe_load e YAML.dump em um script Ruby mínimo, com foco em strings entre aspas e nas formas escalares > e |. Determine se o comportamento de novas linhas e formatação é um comportamento pretendido de YAML/Psych ou um defeito; considera-se concluído quando houver uma explicação documentada ou um escopo do problema confirmado e testável.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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 😄

Linguagem predominante
Ruby
Estrelas
596
Forks
228
Merge médio
24min
PRs com merge (30d)
5

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de ruby/psych

Todas as issues de ruby/psych

Issues semelhantes

Mais issues de Ruby

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.