Class: Arbolobra::Node

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/arbolobra/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, *children) ⇒ Node

Returns a new instance of Node.



14
15
16
17
18
19
20
21
# File 'lib/arbolobra/node.rb', line 14

def initialize value, *children
  @children = if children.size == 1 && children.first.class == Array
                children.first
              else
                children
              end
  @value = value
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



12
13
14
# File 'lib/arbolobra/node.rb', line 12

def children
  @children
end

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/arbolobra/node.rb', line 11

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/arbolobra/node.rb', line 32

def <=> other
  cmp = value <=> other.value
  if cmp == 0
    cmp = children <=> other.children
  end
  cmp
end


23
24
25
26
# File 'lib/arbolobra/node.rb', line 23

def print intro: "", lead: "", output: $stdout, charset: Arbolobra::CharSet::DEFAULT
  fmt = Formatter.new charset: charset, output: output
  fmt.print self, intro, lead
end

#to_sObject



28
29
30
# File 'lib/arbolobra/node.rb', line 28

def to_s
  "value: #{@value}, #{@children && @children.size}"
end