class

YAML::Nodes::Builder

Inherits Reference / Object

Builds a tree of YAML nodes.

This builder is similar to YAML::Builder, but instead of directly emitting the output to an IO it builds a YAML document tree in memory.

All "emitting" methods support specifying a "reference" object that will be associated to the emitted object, so that when that reference object is emitted again an anchor and an alias will be created. This generates both more compact documents and allows handling recursive data structures.

Constructors

Instance methods

alias(anchor : String) : Nil

Emits an alias to the given anchor.

require "yaml"

nodes_builder = YAML::Nodes::Builder.new

nodes_builder.mapping do
  nodes_builder.scalar "foo"
  nodes_builder.alias "key"
end

yaml = YAML.build do |builder|
  nodes_builder.document.to_yaml builder
end

yaml # => "---\nfoo: *key\n"
Source
document

The document this builder builds.

Source
mapping(anchor : String | Nil = nil, tag : String | Nil = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY, reference = nil, &) : Nil
Source
merge(anchor : String) : Nil

Emits the scalar "<<" followed by an alias to the given anchor.

See YAML Merge.

require "yaml"

nodes_builder = YAML::Nodes::Builder.new

nodes_builder.mapping do
  nodes_builder.merge "key"
end

yaml = YAML.build do |builder|
  nodes_builder.document.to_yaml builder
end

yaml # => "---\n<<: *key\n"
Source
scalar(value, anchor : String | Nil = nil, tag : String | Nil = nil, style : YAML::ScalarStyle = YAML::ScalarStyle::ANY, reference = nil) : Nil
Source
sequence(anchor : String | Nil = nil, tag : String | Nil = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY, reference = nil, &) : Nil
Source