Skip to content

class YAML::Builder
inherits Reference

A YAML builder generates valid YAML.

A YAML::Error is raised if attempting to generate an invalid YAML (for example, if invoking end_sequence without a matching start_sequence)

require "yaml"

string = YAML.build do |yaml|
  yaml.mapping do
    yaml.scalar "foo"
    yaml.sequence do
      yaml.scalar 1
      yaml.scalar 2
    end
    yaml.scalar "bar"
    yaml.mapping do
      yaml.scalar "baz"
      yaml.scalar "qux"
    end
  end
end
string # => "---\nfoo:\n- 1\n- 2\nbar:\n  baz: qux\n"

Class methods

.build(io : IO, & : self -> ) : Nil

Creates a YAML::Builder that writes to io and yields it to the block.

After returning from the block the builder is closed.

View source

.new(io : IO)

Creates a YAML::Builder that will write to the given IO.

View source

Methods

#alias(anchor : String) : Nil

Emits an alias to the given anchor.

require "yaml"

yaml = YAML.build do |builder|
  builder.mapping do
    builder.scalar "key"
    builder.alias "example"
  end
end

yaml # => "---\nkey: *example\n"
View source

#close

Closes the builder, freeing up resources.

View source

#document

Starts a document, invokes the block, and then ends it.

View source

#end_document

Ends a document.

View source

#end_mapping

Ends a mapping.

View source

#end_sequence

Ends a sequence.

View source

#end_stream

Ends a YAML stream.

View source

#finalize

View source

#flush

Flushes any pending data to the underlying IO.

View source

#mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY

Starts a mapping, invokes the block, and then ends it.

View source

#max_nesting : Int32

By default the maximum nesting of sequences/mappings is 99. Nesting more than this will result in a YAML::Error. Changing the value of this property allows more/less nesting.

View source

#max_nesting=(max_nesting)

By default the maximum nesting of sequences/mappings is 99. Nesting more than this will result in a YAML::Error. Changing the value of this property allows more/less nesting.

View source

#merge(anchor : String) : Nil

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

See YAML Merge.

require "yaml"

yaml = YAML.build do |builder|
  builder.mapping do
    builder.merge "development"
  end
end

yaml # => "---\n<<: *development\n"
View source

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

Emits a scalar value.

View source

#sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY

Starts a sequence, invokes the block, and the ends it.

View source

#start_document

Starts a document.

View source

#start_mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY)

Starts a mapping.

View source

#start_sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY)

Starts a sequence.

View source

#start_stream

Starts a YAML stream.

View source

#stream

Starts a YAML stream, invokes the block, and ends it.

View source