File: C:/Ruby27-x64/share/doc/ruby/html/Psych/Nodes.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>module Psych::Nodes - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="module">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div class="nav-section">
<h3>Table of Contents</h3>
<ul class="link-list" role="directory">
<li><a href="#module-Psych::Nodes-label-Overview">Overview</a>
<li><a href="#module-Psych::Nodes-label-YAML+AST+Requirements">YAML AST Requirements</a>
</ul>
</div>
<div id="class-metadata">
</div>
</nav>
<main role="main" aria-labelledby="module-Psych::Nodes">
<h1 id="module-Psych::Nodes" class="module">
module Psych::Nodes
</h1>
<section class="description">
<h1 id="module-Psych::Nodes-label-Overview">Overview<span><a href="#module-Psych::Nodes-label-Overview">¶</a> <a href="#top">↑</a></span></h1>
<p>When using <a href="../Psych.html#method-c-load"><code>Psych.load</code></a> 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.</p>
<p>In the opposite direction, when using <a href="../Psych.html#method-c-dump"><code>Psych.dump</code></a>, the Ruby object graph is translated to an intermediary AST which is then converted to a YAML document.</p>
<p><a href="Nodes.html"><code>Psych::Nodes</code></a> contains all of the classes that make up the nodes of a YAML AST. You can manually build an AST and use one of the visitors (see <a href="Visitors.html"><code>Psych::Visitors</code></a>) to convert that AST to either a YAML document or to a Ruby object graph.</p>
<p>Here is an example of building an AST that represents a list with one scalar:</p>
<pre class="ruby"><span class="ruby-comment"># Create our nodes</span>
<span class="ruby-identifier">stream</span> = <span class="ruby-constant">Psych</span><span class="ruby-operator">::</span><span class="ruby-constant">Nodes</span><span class="ruby-operator">::</span><span class="ruby-constant">Stream</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">doc</span> = <span class="ruby-constant">Psych</span><span class="ruby-operator">::</span><span class="ruby-constant">Nodes</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">seq</span> = <span class="ruby-constant">Psych</span><span class="ruby-operator">::</span><span class="ruby-constant">Nodes</span><span class="ruby-operator">::</span><span class="ruby-constant">Sequence</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">scalar</span> = <span class="ruby-constant">Psych</span><span class="ruby-operator">::</span><span class="ruby-constant">Nodes</span><span class="ruby-operator">::</span><span class="ruby-constant">Scalar</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'foo'</span>)
<span class="ruby-comment"># Build up our tree</span>
<span class="ruby-identifier">stream</span>.<span class="ruby-identifier">children</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">doc</span>
<span class="ruby-identifier">doc</span>.<span class="ruby-identifier">children</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">seq</span>
<span class="ruby-identifier">seq</span>.<span class="ruby-identifier">children</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">scalar</span>
</pre>
<p>The stream is the root of the tree. We can then convert the tree to YAML:</p>
<pre>stream.to_yaml => "---\n- foo\n"</pre>
<p>Or convert it to Ruby:</p>
<pre>stream.to_ruby => [["foo"]]</pre>
<h2 id="module-Psych::Nodes-label-YAML+AST+Requirements">YAML AST Requirements<span><a href="#module-Psych::Nodes-label-YAML+AST+Requirements">¶</a> <a href="#top">↑</a></span></h2>
<p>A valid YAML AST <strong>must</strong> have one <a href="Nodes/Stream.html"><code>Psych::Nodes::Stream</code></a> at the root. A <a href="Nodes/Stream.html"><code>Psych::Nodes::Stream</code></a> node must have 1 or more <a href="Nodes/Document.html"><code>Psych::Nodes::Document</code></a> nodes as children.</p>
<p><a href="Nodes/Document.html"><code>Psych::Nodes::Document</code></a> nodes must have one and <strong>only</strong> one child. That child may be one of:</p>
<ul><li>
<p><a href="Nodes/Sequence.html"><code>Psych::Nodes::Sequence</code></a></p>
</li><li>
<p><a href="Nodes/Mapping.html"><code>Psych::Nodes::Mapping</code></a></p>
</li><li>
<p><a href="Nodes/Scalar.html"><code>Psych::Nodes::Scalar</code></a></p>
</li></ul>
<p><a href="Nodes/Sequence.html"><code>Psych::Nodes::Sequence</code></a> and <a href="Nodes/Mapping.html"><code>Psych::Nodes::Mapping</code></a> nodes may have many children, but <a href="Nodes/Mapping.html"><code>Psych::Nodes::Mapping</code></a> nodes should have an even number of children.</p>
<p>All of these are valid children for <a href="Nodes/Sequence.html"><code>Psych::Nodes::Sequence</code></a> and <a href="Nodes/Mapping.html"><code>Psych::Nodes::Mapping</code></a> nodes:</p>
<ul><li>
<p><a href="Nodes/Sequence.html"><code>Psych::Nodes::Sequence</code></a></p>
</li><li>
<p><a href="Nodes/Mapping.html"><code>Psych::Nodes::Mapping</code></a></p>
</li><li>
<p><a href="Nodes/Scalar.html"><code>Psych::Nodes::Scalar</code></a></p>
</li><li>
<p><a href="Nodes/Alias.html"><code>Psych::Nodes::Alias</code></a></p>
</li></ul>
<p><a href="Nodes/Scalar.html"><code>Psych::Nodes::Scalar</code></a> and <a href="Nodes/Alias.html"><code>Psych::Nodes::Alias</code></a> are both terminal nodes and should not have any children.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>