Is it possible to specify formatting options for to_yaml in ruby?

前端 未结 5 1149
说谎
说谎 2020-12-11 14:36

The code

require \'yaml\'
puts YAML.load(\"
is_something:
  values: [\'yes\', \'no\']
\").to_yaml

produces

--- 
is_somethin         


        
相关标签:
5条回答
  • 2020-12-11 15:20

    The latest versions of Ruby use the Psych module for YAML parsing. There aren't many options that you can pass but you can change indention and line width. Check the latest Psych documentation for more details.

    0 讨论(0)
  • 2020-12-11 15:26

    Just another hack to specify the output style, but this one allows to customize it per specific object, instead of globally (e.g. for all arrays).

    https://gist.github.com/jirutka/31b1a61162e41d5064fc

    Simple example:

    class Movie
      attr_accessor :genres, :actors
    
      # method called by psych to render YAML
      def encode_with(coder)
        # render array inline (flow style)
        coder['genres'] = StyledYAML.inline(genres) if genres
        # render in default style (block)
        coder['actors'] = actors if actors
      end
    end
    
    0 讨论(0)
  • 2020-12-11 15:28

    About the hash options: see http://yaml4r.sourceforge.net/doc/page/examples.htm

    Ex. 24: Using to_yaml with an options Hash

    puts [[ 'Crispin', 'Glover' ]].to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true )
    # prints:
    #   --- %YAML:1.0
    #   -
    #       - Crispin
    #       - Glover
    

    Ex. 25: Available symbols for an options Hash

    Indent: The default indentation to use when emitting (defaults to 2)
    Separator: The default separator to use between documents (defaults to '---')
    SortKeys: Sort Hash keys when emitting? (defaults to false)
    UseHeader: Display the YAML header when emitting? (defaults to false)
    UseVersion: Display the YAML version when emitting? (defaults to false)
    AnchorFormat: A formatting string for anchor IDs when emitting (defaults to 'id%03d')
    ExplicitTypes: Use explicit types when emitting? (defaults to false)
    BestWidth: The character width to use when folding text (defaults to 80)
    UseFold: Force folding of text when emitting? (defaults to false)
    UseBlock: Force all text to be literal when emitting? (defaults to false)
    Encoding: Unicode format to encode with (defaults to :Utf8; requires Iconv)

    0 讨论(0)
  • 2020-12-11 15:28

    Starting from Ruby 1.9 psych is used as a default YAML engine. It supports some attributes: http://ruby-doc.org/stdlib-2.1.0/libdoc/psych/rdoc/Psych/Handler/DumperOptions.html

    So for me it works:

    irb(main):001:0> require 'yaml'
    => true
    irb(main):002:0> puts [{'a'=> 'b', 'c'=> 'd'}, {'e'=> 'f', 'g'=>'h'}].to_yaml(:indentation => 4)
    ---
    -   a: b
        c: d
    -   e: f
        g: h
    
    0 讨论(0)
  • 2020-12-11 15:28

    This ugly hack seems to do the trick...

    class Array
      def to_yaml_style
        :inline
      end
    end
    

    Browsing through ruby's source, I can't find any options I could pass to achieve the same. Default options are described in the lib/yaml/constants.rb.

    0 讨论(0)
提交回复
热议问题