Class: GADDAG::Path

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(letters) ⇒ Path

Initializes a GADDAG path a delimiter, and an optional suffix: REV(PREFIX) ♢ SUFFIX

Parameters:

  • letters (Array<String>)

    a list of letters, containing a reversed prefix,



20
21
22
# File 'lib/gaddag/path.rb', line 20

def initialize(letters)
  @letters = letters
end

Instance Attribute Details

#lettersObject (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

Returns:

  • (Boolean)

    whether this path includes the delimiter



40
41
42
# File 'lib/gaddag/path.rb', line 40

def include_delimiter?
  @letters.include?(DELIMITER)
end

#reversed_prefix_lettersArray<String>

Returns the reversed prefix of this path

Returns:

  • (Array<String>)

    the first portion of this path: the reversed prefix



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

Parameters:

  • letters (Array<String>)

    the letters to check for

Returns:

  • (Boolean)

    whether the path starts with the letters given



61
62
63
# File 'lib/gaddag/path.rb', line 61

def start_with?(letters)
  @letters.join.start_with?(letters.join)
end

#suffix_lettersArray<String>

Returns the suffix of this path

Returns:

  • (Array<String>)

    the last portion of this path: the suffix



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_aryArray<String>

Coerces into an Array.

Examples:

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

Returns:

  • (Array<String>)

    the letters in this path



54
55
56
# File 'lib/gaddag/path.rb', line 54

def to_ary
  @letters
end

#to_sObject

Returns a string presentation of this path

Returns:

  • the string represtentation, letters are delimited with ‘>’



46
47
48
# File 'lib/gaddag/path.rb', line 46

def to_s
  @letters.join(' > ')
end

#to_wordWord

Constructs a word from the partially reversed letter path

Returns:

  • (Word)

    the word that is encoded within this path



67
68
69
# File 'lib/gaddag/path.rb', line 67

def to_word
  Word.new(reversed_prefix_letters.reverse + suffix_letters)
end