lib/rambling/trie/node.rb in rambling-trie-0.5.1 vs lib/rambling/trie/node.rb in rambling-trie-0.5.2

- old
+ new

@@ -25,13 +25,12 @@ # @param [Node, nil] parent the parent of this node. def initialize(word = nil, parent = nil) self.parent = parent self.children = {} - unless word.nil? or word.empty? - letter = word.slice! 0 - self.letter = letter.to_sym if letter + unless word.nil? || word.empty? + self.letter = word.slice! 0 self.terminal = word.empty? self << word end end @@ -43,20 +42,30 @@ # String representation of the current node, if it is a terminal node. # @return [String] the string representation of the current node. # @raise [InvalidOperation] if node is not terminal or is root. def as_word - raise InvalidOperation, 'Cannot represent branch as a word' unless letter.nil? or terminal? - letter_string + raise InvalidOperation, 'Cannot represent branch as a word' if letter && !terminal? + to_s end + # String representation of the current node. + # @return [String] the string representation of the current node. + def to_s + parent.to_s << letter.to_s + end + protected - attr_writer :letter, :children + attr_writer :children attr_accessor :terminal - def letter_string - (parent ? parent.letter_string : '') << letter.to_s + def letter=(letter) + return unless letter + + letter = letter.to_sym + @letter = letter + parent[letter] = self if parent end end end end