query: tag:yaml

YAMLの復習

YAMLのノードには3種類(kind)がある:

  • Scalar (Unicodeの文字列として表現できるもの)
  • Sequence (配列みたいなもの)
  • Mapping (連想配列やHashみたいなもの)

それぞれのkindはいくつかのstyleで出力できる。

  • Scalar:
  1. Plain (1行)
    yaml>>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    <<--
  2. Single Quote (1行)
    yaml>>
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
    <<--
  3. Double Quote (1行)
    yaml>>
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
    <<--
  4. Block Literal (改行がそのまま残る)
    yaml>>
    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit.
    Pellentesque tincidunt molestie est.
    Vestibulum ante odio, euismod ac,
    sagittis et, tempus ut, lorem.
    Praesent consectetur tempor ipsum.
    Nulla facilisi.
    <<--
  5. Block Folding (改行が空白になる)
    yaml>>
    --- >
    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit.
    Pellentesque tincidunt molestie est.
    Vestibulum ante odio, euismod ac,
    sagittis et, tempus ut, lorem.
    Praesent consectetur tempor ipsum.
    Nulla facilisi.
    <<--
  • Sequence
  1. デフォルト
    yaml>>
  • one
  • two
  • three
    <<--
  1. inline
    yaml>>
    [1, 2, 3]
    <<--
  • Mapping
  1. デフォルト
    yaml>>
    height: 170
    weight: 60
    <<--
  2. inline
    yaml>>
    { height: 170, weight: 60}
    <<--

で、

YAML::dumpでオブジェクトをダンプしたい時、styleを統一する場合もある。が、YAML::dumpにはstyleを指定する機能はない。そこで、to_yaml_styleをオブジェクトに定義すればよい、という裏技(非ドキュメントメソッド)を紹介したい。このメソッドの返り値がそのオブジェクトのstyleになる。

例えば:
ruby>>
class String
def to_yaml_style
return :quote2
end
end
<<--
をすれば、YAML::dumpのすべての文字列がダブルクオートに囲まれる。

Rubyでのスタイルは以下:

ruby>>
:plain # クオートなし
:quote1 # シングルクオート
:quote2 # ダブルクオート
:literal # Block Literal
:fold # Block Folding
:inline # Inline (Sequence, Mapping)
nil # デフォルト (Sequence, Mapping)
<<--

ちなみに、これを調べた切っ掛けはHashをYAML::dumpした時にvalueの方にクオートがあったりなかったりしたからです。つまりこれ:
yaml>>
name: "鈴木"
height: 170
weight: 60
<<--
をこれにする:
yaml>>
name: "鈴木"
height: "170"
weight: "60"
<<--

monkeypatch的な解決方法:
ruby>>
class Hash
def to_yaml_with_quoted_strings(*args)
class << self
unless method_defined?(:each_with_quoted_strings)
def each_with_quoted_strings
each_with_normal_strings do |k,v|
if String === v && !v.frozen?
def v.to_yaml_style; return :quote2; end
end
yield k, v
end
end
alias_method :each_with_normal_strings, :each
alias_method :each, :each_with_quoted_strings
end
end
return to_yaml_with_normal_strings(*args)
end
alias_method :to_yaml_with_normal_strings, :to_yaml
alias_method :to_yaml, :to_yaml_with_quoted_strings
end
<<--

gist

参考

posted by lchin lchin on Wed 1 Apr 2009 at 16:54 with 0 comments
Contents rssrss
Leading Smart TV App Development Companies | USA | 4 Way Technologies
Apple TV Developer | TVOS Development | USA | 4 Way Technologies
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Value Transformer
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Tags
yaml
Comments rssrss
https://isshin-kikaku.com/resources/ https://isshin-kikaku.com/resources/: 突然のご連絡失礼いたします。 建築リペアガイド(https://isshin-kikaku.co... 04/21 06:07
https://xn--mgmrket7-1ya.com https://xn--mgmrket7-1ya.com: Как распознать поддельное зеркало MEGA: признаки и... 04/21 00:25
https://xn--krken21-bn4c.com https://xn--krken21-bn4c.com: Как отличить настоящий маркетплейс от подделки Kra... 04/20 20:37
https://t.me/wp_seo/238 https://t.me/wp_seo/238: Какой процент погрешности при проверке? 04/20 02:15
https://vk.com/wall24679647_12405 https://vk.com/wall24679647_12405: Спасибо, буду тестировать этот функционал! 04/20 01:46
https://vk.com/wall24679647_12405 https://vk.com/wall24679647_12405: А как этот инструмент справляется с капчей? 04/20 01:17
https://terapistara.com/author/odetteochs4887/ https://terapistara.com/author/odetteochs4887/: Nice post. I appreciated reading. Thank you for sh... 04/19 21:01
https://tamozhenniiy-broker-sb11.ru/ https://tamozhenniiy-broker-sb11.ru/: Подскажите условия по хранению на СВХ, если оформл... 04/19 04:28