• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

YAMLの復習

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

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

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

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

で、

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

例えば:

   1  class String
   2   def to_yaml_style
   3    return :quote2
   4   end
   5  end
をすれば、YAML::dumpのすべての文字列がダブルクオートに囲まれる。

Rubyでのスタイルは以下:

   1  :plain # クオートなし
   2  :quote1 # シングルクオート
   3  :quote2 # ダブルクオート
   4  :literal # Block Literal
   5  :fold # Block Folding
   6  :inline # Inline (Sequence, Mapping)
   7  nil # デフォルト (Sequence, Mapping)

ちなみに、これを調べた切っ掛けはHashをYAML::dumpした時にvalueの方にクオートがあったりなかったりしたからです。つまりこれ:

   1   name: "鈴木"
   2   height: 170
   3   weight: 60
をこれにする:
   1   name: "鈴木"
   2   height: "170"
   3   weight: "60"

monkeypatch的な解決方法:

   1  class Hash
   2    def to_yaml_with_quoted_strings(*args)
   3      class << self
   4        unless method_defined?(:each_with_quoted_strings)
   5          def each_with_quoted_strings
   6            each_with_normal_strings do |k,v|
   7              if String === v && !v.frozen?
   8                def v.to_yaml_style; return :quote2; end
   9              end
  10              yield k, v
  11            end
  12          end
  13            alias_method :each_with_normal_strings, :each
  14            alias_method :each, :each_with_quoted_strings
  15        end
  16      end
  17      return to_yaml_with_normal_strings(*args)
  18    end
  19    alias_method :to_yaml_with_normal_strings, :to_yaml
  20    alias_method :to_yaml, :to_yaml_with_quoted_strings
  21  end

gist

参考

posted by Face lchin on Wed 1 Apr 2009 at 16:53

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1393
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ