Skip to content

class YAML::Nodes::Builder
inherits Reference

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.

Class methods

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"
View source

#document : Document

The document this builder builds.

View source

#mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY, reference = nil, &) : Nil

View 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"
View source

#scalar(value, anchor : String? = nil, tag : String? = nil, style : YAML::ScalarStyle = YAML::ScalarStyle::ANY, reference = nil) : Nil

View source

#sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY, reference = nil, &) : Nil

View source