Ruby 2.4.0 リファレンスマニュアル > ライブラリ一覧 > psychライブラリ > Psych::Parserクラス

class Psych::Parser

クラス・モジュールの継承リスト: Psych::Parser < Object < Kernel < BasicObject

要約

YAML のパーサ。

このクラスは YAML ドキュメントをパースし、コンストラクタに渡されたハンドラにイベントを通知(呼び出し)します。このイベントを使って YAML の AST を構築したり YAML ドキュメントを別のフォーマット変換したりします。 Psych::Emitter を使うとパースしたドキュメントを元通りに出力することもできます。

Psych::Parser が生成するイベントは Psych::Handler を見てください。

以下の例では YAML ドキュメント に含まれているスカラー値を表示します。

# 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)

次の例は Psych::Emitter にパースの結果を戻しています。 STDIN からの入力をパース→YAMLフォーマットで STDERR に出力という流れになっています。

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

Psych::ParserPsych::TreeBuilder を組み合わせると YAML の AST を構築することができます。

特異メソッド

定義 説明
new(handler = Handler.new) -> Psych::Parser

新たなパーサオブジェクトを生成して返します。

インスタンスメソッド

定義 説明
handler -> Psych::Handler

セットされているイベントハンドラを返します。

handler=(val)

イベントハンドラをセットします。

mark -> Psych::Parser::Mark

パーサが現在読み込んでいる入力上の位置を Psych::Parser::Mark オブジェクトで返します。

parse(yaml) -> self

YAML ドキュメントをパースし、イベントハンドラにイベントを逐次通知します。

定数

定義 説明
ANY -> Integer

「任意の」エンコーディングを意味します。

UTF16BE -> Integer

UTF-16BE エンコーディングを表します。

UTF16LE -> Integer

UTF-16LE エンコーディングを表します。

UTF8 -> Integer

UTF-8 エンコーディングを表します。

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined !~ <=> == === =~ _dump class clone define_singleton_method display enum_for eql? equal? extend freeze frozen? hash initialize initialize_copy inspect instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_dump marshal_load method methods nil? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_s to_str trust untaint untrust untrusted? .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_ENGINE_VERSION ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE