Class: Fr::TZipper::Node

Inherits:
Object show all
Defined in:
lib/fr/tzipper/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, children = []) ⇒ Node

Returns a new instance of Node.



10
11
12
13
# File 'lib/fr/tzipper/node.rb', line 10

def initialize(value, children = [])
  @value, @children =
    value, children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/fr/tzipper/node.rb', line 8

def children
  @children
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/fr/tzipper/node.rb', line 6

def value
  @value
end

Class Method Details

.build(value, children) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/fr/tzipper/node.rb', line 43

def build(value, children)
  Node.new(value, children.map do |c|
    if Array === c
      value_, *children_ = c
      Node.build(value_, children_)
    else
      Node.new(c)
    end
  end)
end

Instance Method Details

#cons(tail = []) ⇒ Object

This is implemented in object.rb



27
28
29
# File 'lib/fr/tzipper/node.rb', line 27

def cons(tail = [])
  [self] + tail
end

#copy(changes = {}) ⇒ Object



15
16
17
18
19
# File 'lib/fr/tzipper/node.rb', line 15

def copy(changes = {})
  Node.new \
    changes.fetch(:value, @value),
    changes.fetch(:children, @value)
end

#inspectObject



31
32
33
34
35
36
37
# File 'lib/fr/tzipper/node.rb', line 31

def inspect
  if @children.empty?
    "Leaf(#{@value.inspect})"
  else
    "Node(#{@value.inspect}, #{@children.map(&:inspect).join(", ")})"
  end
end

#leaf?Boolean

Allow any node to have children

Returns:

  • (Boolean)


22
23
24
# File 'lib/fr/tzipper/node.rb', line 22

def leaf?
  false
end