Class: EasyEncoding::Node
- Inherits:
-
Object
- Object
- EasyEncoding::Node
- Includes:
- Comparable
- Defined in:
- lib/easy_encoding/node.rb
Instance Attribute Summary collapse
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(params) ⇒ Node
constructor
A new instance of Node.
- #merged? ⇒ Boolean
- #walk(&block) ⇒ Object
- #walk_node(code) {|_self, code| ... } ⇒ Object
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
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
7 8 9 |
# File 'lib/easy_encoding/node.rb', line 7 def frequency @frequency end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
7 8 9 |
# File 'lib/easy_encoding/node.rb', line 7 def left @left end |
#parent ⇒ Object
Returns the value of attribute parent.
8 9 10 |
# File 'lib/easy_encoding/node.rb', line 8 def parent @parent end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
7 8 9 |
# File 'lib/easy_encoding/node.rb', line 7 def right @right end |
#symbol ⇒ Object (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
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
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 |