Ruby 1.9.3 リファレンスマニュアル > ライブラリ一覧 > psychライブラリ

library psych

要約

yaml のバックエンドライブラリです。libyaml ベースで作成されてお り、YAML バージョン 1.1 を扱う事ができます。

Overview

Psych is a YAML parser and emitter. Psych leverages libyaml[http://libyaml.org] for it's YAML parsing and emitting capabilities. In addition to wrapping libyaml, Psych also knows how to serialize and de-serialize most Ruby objects to and from the YAML format.

I NEED TO PARSE OR EMIT YAML RIGHT NOW!

# Parse some YAML
Psych.load("--- foo") # => "foo"

# Emit some YAML
Psych.dump("foo")     # => "--- foo\n...\n"
{ :a => 'b'}.to_yaml  # => "---\n:a: b\n"

Got more time on your hands? Keep on reading!

YAML Parsing

Psych provides a range of interfaces for parsing a YAML document ranging from low level to high level, depending on your parsing needs. At the lowest level, is an event based parser. Mid level is access to the raw YAML AST, and at the highest level is the ability to unmarshal YAML to ruby objects.

Low level parsing

The lowest level parser should be used when the YAML input is already known, and the developer does not want to pay the price of building an AST or automatic detection and conversion to ruby objects. See Psych::Parser for more information on using the event based parser.

Mid level parsing

Psych provides access to an AST produced from parsing a YAML document. This tree is built using the Psych::Parser and Psych::TreeBuilder. The AST can be examined and manipulated freely. Please see Psych::parse_stream, Psych::Nodes, and Psych::Nodes::Node for more information on dealing with YAML syntax trees.

High level parsing

The high level YAML parser provided by Psych simply takes YAML as input and returns a Ruby data structure. For information on using the high level parser see Psych.load

YAML Emitting

Psych provides a range of interfaces ranging from low to high level for producing YAML documents. Very similar to the YAML parsing interfaces, Psych provides at the lowest level, an event based system, mid-level is building a YAML AST, and the highest level is converting a Ruby object straight to a YAML document.

Low level emitting

The lowest level emitter is an event based system. Events are sent to a Psych::Emitter object. That object knows how to convert the events to a YAML document. This interface should be used when document format is known in advance or speed is a concern. See Psych::Emitter for more information.

Mid level emitting

At the mid level is building an AST. This AST is exactly the same as the AST used when parsing a YAML document. Users can build an AST by hand and the AST knows how to emit itself as a YAML document. See Psych::Nodes, Psych::Nodes::Node, and Psych::TreeBuilder for more information on building a YAML AST.

High level emitting

The high level emitter has the easiest interface. Psych simply takes a Ruby data structure and converts it to a YAML document. See Psych.dump for more information on dumping a Ruby data structure.

クラス

class Psych::Coder

If an object defines +encode_with+, then an instance of Psych::Coder will be passed to the method when the object is being serialized. The Coder automatically assumes a Psych::Nodes::Mapping is being emitted. Other objects like Sequence and Scalar may be emitted if +seq=+ or +scalar=+ are called, respectively.

class Psych::Handler
  class Psych::Emitter
  class Psych::TreeBuilder

This class works in conjunction with Psych::Parser to build an in-memory parse tree that represents a YAML document.

class Psych::JSON::TreeBuilder

Psych::JSON::TreeBuilder is an event based AST builder. Events are sent to an instance of Psych::JSON::TreeBuilder and a JSON AST is constructed.

class Psych::Nodes::Node
  class Psych::Nodes::Alias

This class represents a {YAML Alias}[http://yaml.org/spec/1.1/#alias]. It points to an +anchor+.

  class Psych::Nodes::Document
  class Psych::Nodes::Mapping
  class Psych::Nodes::Scalar
  class Psych::Nodes::Sequence
  class Psych::Nodes::Stream
class Psych::Parser
class Psych::ScalarScanner

Scan scalars for built in types

class Psych::Visitors::Visitor
  class Psych::Visitors::DepthFirst
  class Psych::Visitors::Emitter
  class Psych::Visitors::ToRuby

This class walks a YAML AST, converting each node to ruby

  class Psych::Visitors::YAMLTree

YAMLTree builds a YAML ast given a ruby object. For example:

   class Psych::Stream

Psych::Stream is a streaming YAML emitter. It will not buffer your YAML, but send it straight to an IO.

   class Psych::Visitors::JSONTree
    class Psych::JSON::Stream
class Psych::Parser::Mark
class Psych::Omap
class Psych::Set

モジュール

module Psych

yaml のバックエンドのためのモジュールです。

module Psych::JSON
module Psych::Nodes

When using Psych.load to deserialize a YAML document, the document is translated to an intermediary AST. That intermediary AST is then translated in to a Ruby object graph.

module Psych::Streaming

例外クラス

class Psych::Exception

同時にrequireされるライブラリ

date

日付をあつかうためのライブラリです。

psych/coder
psych/core_ext

ClassModuleKernel といった基本的なクラスを拡張す るためのサブライブラリです。

psych/deprecated
psych/handler

Psych::Handler is an abstract base class that defines the events used when dealing with Psych::Parser. Clients who want to use Psych::Parser should implement a class that inherits from Psych::Handler and define events that they can handle.

psych/json
psych/json/stream
psych/json/tree_builder
psych/nodes
psych/nodes/alias
psych/nodes/document
psych/nodes/mapping
psych/nodes/node
psych/nodes/scalar
psych/nodes/sequence
psych/nodes/stream
psych/omap
psych/parser

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.

psych/scalar_scanner
psych/set
psych/stream
psych/streaming
psych/tree_builder
psych/visitors
psych/visitors/depth_first
psych/visitors/emitter
psych/visitors/json_tree
psych/visitors/to_ruby
psych/visitors/visitor
psych/visitors/yaml_tree
stringio

文字列に IO と同じインタフェースを持たせるためのライブラリです。

strscan

strscan は 文字列を高速にスキャンするためのライブラリです。

library psych