class GADDAG::Path

Represents a (final) path within the GADDAG data structure

Constants

DELIMITER

The path delimiter that seperates the reversed prefix and the suffix

Attributes

letters[R]

The letters that make up this GADDAG path

Public Class Methods

new(letters) click to toggle source

Initializes a GADDAG path @param letters [Array<String>] a list of letters, containing a reversed prefix, a delimiter, and an optional suffix: REV(PREFIX) ♢ SUFFIX @return [Path]

# File lib/gaddag/path.rb, line 20
def initialize(letters)
  @letters = letters
end

Public Instance Methods

include_delimiter?() click to toggle source

Tells whether the path includes {GADDAG::DELIMITER} @return [Boolean] whether this path includes the delimiter

# File lib/gaddag/path.rb, line 40
def include_delimiter?
  @letters.include?(DELIMITER)
end
reversed_prefix_letters() click to toggle source

Returns the reversed prefix of this path @return [Array<String>] the first portion of this path: the reversed prefix

# 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) click to toggle source

Tells whether the path starts with the given letters @param letters [Array<String>] the letters to check for @return [Boolean] whether the path starts with the letters given

# File lib/gaddag/path.rb, line 61
def start_with?(letters)
  @letters.join.start_with?(letters.join)
end
suffix_letters() click to toggle source

Returns the suffix of this path @return [Array<String>] the last portion of this path: the suffix

# 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() click to toggle source

Coerces into an Array. @example

['K', 'E'] + Path.new(%w(A R B)) # => ['K', 'E', 'A', 'R', 'B']

@return [Array<String>] the letters in this path

# File lib/gaddag/path.rb, line 54
def to_ary
  @letters
end
to_s() click to toggle source

Returns a string presentation of this path @return the string represtentation, letters are delimited with ‘>’

# File lib/gaddag/path.rb, line 46
def to_s
  @letters.join(' > ')
end
to_word() click to toggle source

Constructs a word from the partially reversed letter path @return [Word] the word that is encoded within this path

# File lib/gaddag/path.rb, line 67
def to_word
  Word.new(reversed_prefix_letters.reverse + suffix_letters)
end