Method: Psych.parse_stream
- Defined in:
- lib/psych.rb
.parse_stream(yaml, filename: nil, &block) ⇒ Object
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. # => "(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.
454 455 456 457 458 459 460 461 462 463 |
# File 'lib/psych.rb', line 454 def self.parse_stream yaml, filename: nil, &block if block_given? parser = Psych::Parser.new(Handlers::DocumentStream.new(&block)) parser.parse yaml, filename else parser = self.parser parser.parse yaml, filename parser.handler.root end end |