Method: Psych.parse

Defined in:
lib/psych.rb

.parse(yaml, filename: nil) ⇒ Object

Parse a YAML string in yaml. Returns the Psych::Nodes::Document. filename is used in the exception message if a Psych::SyntaxError is raised.

Raises a Psych::SyntaxError when a YAML syntax error is detected.

Example:

Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00>

begin
  Psych.parse("--- `", filename: "file.txt")
rescue Psych::SyntaxError => ex
  ex.file    # => 'file.txt'
  ex.message # => "(file.txt): found character that cannot start any token"
end

See Psych::Nodes for more information about YAML AST.



400
401
402
403
404
405
406
# File 'lib/psych.rb', line 400

def self.parse yaml, filename: nil
  parse_stream(yaml, filename: filename) do |node|
    return node
  end

  false
end