Type:
Class
Constants:
ANY : Any encoding

Let the parser choose the encoding

UTF8 : INT2NUM(YAML_UTF8_ENCODING)

UTF-8 Encoding

UTF16LE : INT2NUM(YAML_UTF16LE_ENCODING)

UTF-16-LE Encoding with BOM

UTF16BE : INT2NUM(YAML_UTF16BE_ENCODING)

UTF-16-BE Encoding with BOM

YAML event parser class. This class parses a YAML document and calls events on the handler that is passed to the constructor. The events can be used for things such as constructing a YAML AST or deserializing YAML documents. It can even be fed back to Psych::Emitter to emit the same document that was parsed.

See Psych::Handler for documentation on the events that Psych::Parser emits.

Here is an example that prints out ever scalar found in a YAML document:

1
2
3
4
5
6
7
8
9
# Handler for detecting scalar values
class ScalarHandler < Psych::Handler
  def scalar value, anchor, tag, plain, quoted, style
    puts value
  end
end
 
parser = Psych::Parser.new(ScalarHandler.new)
parser.parse(yaml_document)

Here is an example that feeds the parser back in to Psych::Emitter. The YAML document is read from STDIN and written back out to STDERR:

1
2
parser = Psych::Parser.new(Psych::Emitter.new($stderr))
parser.parse($stdin)

Psych uses Psych::Parser in combination with Psych::TreeBuilder to construct an AST of the parsed YAML document.

mark
  • References/Ruby on Rails/Ruby/Classes/Psych/Psych::Parser

parser.mark # => # Instance Public methods Returns a

2025-01-10 15:47:30
parse
  • References/Ruby on Rails/Ruby/Classes/Psych/Psych::Parser

parser.parse(yaml) Instance Public methods Parse the YAML document contained

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/Psych/Psych::Parser

new(handler = Handler.new) Class Public methods Creates a new

2025-01-10 15:47:30