Parse a file at filename
. Returns the Psych::Nodes::Document
.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Parse a YAML
string in yaml
. Returns the Psych::Nodes::Stream
. This method can handle multiple YAML
documents contained in yaml
. filename
is used in the exception message if a Psych::SyntaxError
is raised.
If a block is given, a Psych::Nodes::Document
node will be yielded to the block as it’s being parsed.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Example:
Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00> Psych.parse_stream("--- a\n--- b") do |node| node # => #<Psych::Nodes::Document:0x00> end begin Psych.parse_stream("--- `", filename: "file.txt") rescue Psych::SyntaxError => ex ex.file # => 'file.txt' ex.message # => "(file.txt): found character that cannot start any token" end
Raises a TypeError
when NilClass
is passed.
See Psych::Nodes
for more information about YAML
AST.
Safely dump Ruby
object o
to a YAML
string. Optional options
may be passed in to control the output format. If an IO
object is passed in, the YAML
will be dumped to that IO
object. By default, only the following classes are allowed to be serialized:
Arbitrary classes can be allowed by adding those classes to the permitted_classes
keyword argument. They are additive. For example, to allow Date
serialization:
Psych.safe_dump(yaml, permitted_classes: [Date])
Now the Date
class can be dumped in addition to the classes listed above.
A Psych::DisallowedClass
exception will be raised if the object contains a class that isn’t in the permitted_classes
list.
Currently supported options are:
:indentation
Number of space characters used to indent. Acceptable value should be in 0..9
range, otherwise option is ignored.
Default: 2
.
:line_width
Max character to wrap line at. For unlimited line width use -1
.
Default: 0
(meaning “wrap at 81”).
:canonical
Write “canonical” YAML
form (very verbose, yet strictly formal).
Default: false
.
:header
Write %YAML [version]
at the beginning of document.
Default: false
.
:stringify_names
Dump symbol keys in Hash
objects as string.
Default: false
.
Example:
# Dump an array, get back a YAML string Psych.safe_dump(['a', 'b']) # => "---\n- a\n- b\n" # Dump an array to an IO object Psych.safe_dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890> # Dump an array with indentation set Psych.safe_dump(['a', ['b']], indentation: 3) # => "---\n- a\n- - b\n" # Dump an array to an IO with indentation set Psych.safe_dump(['a', ['b']], StringIO.new, indentation: 3) # Dump hash with symbol keys as string Psych.dump({a: "b"}, stringify_names: true) # => "---\na: b\n"
Dump a list of objects as separate documents to a document stream.
Example:
Psych.dump_stream("foo\n ", {}) # => "--- ! \"foo\\n \"\n--- {}\n"
Load multiple documents given in yaml
. Returns the parsed documents as a list. If a block is given, each document will be converted to Ruby
and passed to the block during parsing
Example:
Psych.load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar'] list = [] Psych.load_stream("--- foo\n...\n--- bar\n...") do |ruby| list << ruby end list # => ['foo', 'bar']
Loads the document contained in filename
. Returns the yaml contained in filename
as a Ruby
object, or if the file is empty, it returns the specified fallback
return value, which defaults to nil
. See load for options.
Returns the version of libyaml being used
@api private
Convert path
string to a class
Emit a map. The coder will be yielded to the block.
Emit a scalar with value
Emit a map with value
Emit a sequence of list
Called when an alias is found to anchor
. anchor
will be the name of the anchor found.
Here we have an example of an array that references itself in YAML:
--- &ponies - first element - *ponies
&ponies is the anchor, *ponies is the alias. In this case, alias is called with “ponies”.
Called when a scalar value
is found. The scalar may have an anchor
, a tag
, be implicitly plain
or implicitly quoted
value
is the string value of the scalar anchor
is an associated anchor or nil tag
is an associated tag or nil plain
is a boolean value quoted
is a boolean value style
is an integer indicating the string style
See the constants in Psych::Nodes::Scalar
for the possible values of style
Here is a YAML
document that exercises most of the possible ways this method can be called:
--- - !str "foo" - &anchor fun - many lines - | many newlines
The above YAML
document contains a list with four strings. Here are the parameters sent to this method in the same order:
# value anchor tag plain quoted style ["foo", nil, "!str", false, false, 3 ] ["fun", "anchor", nil, true, false, 1 ] ["many lines", nil, nil, true, false, 1 ] ["many\nnewlines\n", nil, nil, false, true, 4 ]
Called when an empty event happens. (Which, as far as I can tell, is never).