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
¶
(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.
Methods¶
#alias(anchor : String) : Nil
¶
(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"
#mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY
¶
(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY
Starts a mapping, invokes the block, and then ends it.
#max_nesting : Int32
¶
: 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.
#max_nesting=(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.
#merge(anchor : String) : Nil
¶
(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"
#scalar(value, anchor : String? = nil, tag : String? = nil, style : YAML::ScalarStyle = YAML::ScalarStyle::ANY)
¶
(value, anchor : String? = nil, tag : String? = nil, style : YAML::ScalarStyle = YAML::ScalarStyle::ANY)
Emits a scalar value.
#sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY
¶
(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY
Starts a sequence, invokes the block, and the ends it.
#start_mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY)
¶
(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY)
Starts a mapping.
#start_sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY)
¶
(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY)
Starts a sequence.