class Spacetree::Node

Represent a node respectively a tree

Attributes

children[RW]
value[RW]

Public Class Methods

new(value=nil, *children) click to toggle source

@param value value of the node @children array of nodes wich represent the children of this node

# File lib/spacetree/node.rb, line 13
def initialize value=nil, *children
  @value = value
  @children = children
end

Public Instance Methods

==(o) click to toggle source
# File lib/spacetree/node.rb, line 18
def == o
  return false unless o.kind_of? Node
  [self.value, self.children] == [o.value, o.children]
end
each(*args, &blk) click to toggle source
# File lib/spacetree/node.rb, line 23
def each *args, &blk
  enum = Enumerator.new do |y|
    y << self
    children.each do |c|
      c.each *args, &blk
    end
  end
  if block_given?
    enum.each *args, &blk
  else
    enum
  end
end
emit(indent: 2, level: 0) click to toggle source

Generate a formatted string representation of a node and its children recursively @param indent Count of spaces to indent a level deeper @param level level of indentation

# File lib/spacetree/node.rb, line 41
def emit indent: 2, level: 0
  res = []
  if value.nil?
    children.each {|c| res << c.emit(indent: indent, level: level)}
  else
    spaces = ' ' * indent * level
    res << (spaces << value.to_s)
    children.each {|c| res << c.emit(indent: indent, level: level+1)}
  end
  res.join("\n")
end
to_s() click to toggle source
# File lib/spacetree/node.rb, line 53
def to_s
  emit
end