Class: EasyEncoding::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
15
16
# File 'lib/easy_encoding/node.rb', line 10

def initialize(params)
  @frequency = params.fetch(:frequency, 0)
  @symbol = params.fetch(:symbol, '')
  @left = params.fetch(:left, nil)
  @right = params.fetch(:right, nil)
  @parent = params.fetch(:parent, nil)
end

Instance Attribute Details

#frequencyObject (readonly)

Returns the value of attribute frequency.



7
8
9
# File 'lib/easy_encoding/node.rb', line 7

def frequency
  @frequency
end

#leftObject (readonly)

Returns the value of attribute left.



7
8
9
# File 'lib/easy_encoding/node.rb', line 7

def left
  @left
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#rightObject (readonly)

Returns the value of attribute right.



7
8
9
# File 'lib/easy_encoding/node.rb', line 7

def right
  @right
end

#symbolObject (readonly)

Returns the value of attribute symbol.



7
8
9
# File 'lib/easy_encoding/node.rb', line 7

def symbol
  @symbol
end

Instance Method Details

#<=>(other) ⇒ Object



28
29
30
31
32
# File 'lib/easy_encoding/node.rb', line 28

def <=>(other)
  return nil unless other.is_a?(self.class)

  frequency <=> other.frequency
end

#merged?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/easy_encoding/node.rb', line 34

def merged?
  symbol == ''
end

#walk(&block) ⇒ Object



18
19
20
# File 'lib/easy_encoding/node.rb', line 18

def walk(&block)
  walk_node('', &block)
end

#walk_node(code) {|_self, code| ... } ⇒ Object

Yields:

  • (_self, code)

Yield Parameters:



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

def walk_node(code, &block)
  yield(self, code)
  left&.walk_node(code + EasyEncoding.configuration.left_node_symbol.to_s, &block)
  right&.walk_node(code + EasyEncoding.configuration.right_node_symbol.to_s, &block)
end