Puzzled by \n\n behaviour in a yaml file

オープン
#364 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
ドキュメント
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
ruby
領域
tooling

調査の方向性

まず、最小限の Ruby スクリプトで YAML.load、YAML.safe_load、YAML.dump の例を再現し、引用符付き文字列と > および | のスカラー形式に焦点を当てます。改行とフォーマットの挙動が意図された YAML/Psych の挙動なのか、それとも不具合なのかを判断します。完了の条件は、文書化された説明、または確認済みでテスト可能な問題のスコープです。

索引モデルが issue の本文から書いたものです。

説明

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 😄

主要言語
Ruby
スター
596
フォーク
228
平均マージ
24分
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ruby/psych のほかの issue

ruby/psych の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。