Class: Psych::Nodes::Node

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/psych/nodes/node.rb

Overview

The base class for any Node in a YAML parse tree. This class should never be instantiated.

Direct Known Subclasses

Alias, Document, Mapping, Scalar, Sequence, Stream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Create a new Psych::Nodes::Node


20
21
22
# File 'lib/psych/nodes/node.rb', line 20

def initialize
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

The children of this node


14
15
16
# File 'lib/psych/nodes/node.rb', line 14

def children
  @children
end

#tagObject (readonly)

An associated tag


17
18
19
# File 'lib/psych/nodes/node.rb', line 17

def tag
  @tag
end

Instance Method Details

#each(&block) ⇒ Object

Iterate over each node in the tree. Yields each node to block depth first.


27
28
29
30
# File 'lib/psych/nodes/node.rb', line 27

def each &block
  return enum_for :each unless block_given?
  Visitors::DepthFirst.new(block).accept self
end

#to_rubyObject Also known as: transform

Convert this node to Ruby.

See also Psych::Visitors::ToRuby


36
37
38
# File 'lib/psych/nodes/node.rb', line 36

def to_ruby
  Visitors::ToRuby.create.accept(self)
end

#yaml(io = nil, options = {}) ⇒ Object Also known as: to_yaml

Convert this node to YAML.

See also Psych::Visitors::Emitter


45
46
47
48
49
50
51
# File 'lib/psych/nodes/node.rb', line 45

def yaml io = nil, options = {}
  real_io = io || StringIO.new(''.encode('utf-8'))

  Visitors::Emitter.new(real_io, options).accept self
  return real_io.string unless io
  io
end