Class: GADDAG::Path
- Inherits:
-
Object
- Object
- GADDAG::Path
- Defined in:
- lib/gaddag/path.rb
Overview
Represents a (final) path within the GADDAG data structure
Constant Summary collapse
- DELIMITER =
The path delimiter that seperates the reversed prefix and the suffix
'♢'.freeze
Instance Attribute Summary collapse
-
#letters ⇒ Object
readonly
The letters that make up this GADDAG path.
Instance Method Summary collapse
-
#include_delimiter? ⇒ Boolean
Tells whether the path includes DELIMITER.
-
#initialize(letters) ⇒ Path
constructor
Initializes a GADDAG path a delimiter, and an optional suffix: REV(PREFIX) ♢ SUFFIX.
-
#reversed_prefix_letters ⇒ Array<String>
Returns the reversed prefix of this path.
-
#start_with?(letters) ⇒ Boolean
Tells whether the path starts with the given letters.
-
#suffix_letters ⇒ Array<String>
Returns the suffix of this path.
-
#to_ary ⇒ Array<String>
Coerces into an Array.
-
#to_s ⇒ Object
Returns a string presentation of this path.
-
#to_word ⇒ Word
Constructs a word from the partially reversed letter path.
Constructor Details
#initialize(letters) ⇒ Path
Initializes a GADDAG path a delimiter, and an optional suffix: REV(PREFIX) ♢ SUFFIX
20 21 22 |
# File 'lib/gaddag/path.rb', line 20 def initialize(letters) @letters = letters end |
Instance Attribute Details
#letters ⇒ Object (readonly)
The letters that make up this GADDAG path
8 9 10 |
# File 'lib/gaddag/path.rb', line 8 def letters @letters end |
Instance Method Details
#include_delimiter? ⇒ Boolean
Tells whether the path includes DELIMITER
40 41 42 |
# File 'lib/gaddag/path.rb', line 40 def include_delimiter? @letters.include?(DELIMITER) end |
#reversed_prefix_letters ⇒ Array<String>
Returns the reversed prefix of this path
26 27 28 29 |
# File 'lib/gaddag/path.rb', line 26 def reversed_prefix_letters return [] if @letters.empty? @letters.join.split(DELIMITER).first.chars end |
#start_with?(letters) ⇒ Boolean
Tells whether the path starts with the given letters
61 62 63 |
# File 'lib/gaddag/path.rb', line 61 def start_with?(letters) @letters.join.start_with?(letters.join) end |
#suffix_letters ⇒ Array<String>
Returns the suffix of this path
33 34 35 36 |
# File 'lib/gaddag/path.rb', line 33 def suffix_letters return [] if !include_delimiter? || @letters.last == DELIMITER @letters.join.split(DELIMITER).last.chars end |
#to_ary ⇒ Array<String>
Coerces into an Array.
54 55 56 |
# File 'lib/gaddag/path.rb', line 54 def to_ary @letters end |
#to_s ⇒ Object
Returns a string presentation of this path
46 47 48 |
# File 'lib/gaddag/path.rb', line 46 def to_s @letters.join(' > ') end |