Class: GADDAG::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/gaddag/word.rb

Overview

Represents a word in the GADDAG data structure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(letters) ⇒ Word

Initializes a GADDAG word the word consists

Parameters:

  • letters (Array<String>)

    an ordered list of letters of which



17
18
19
# File 'lib/gaddag/word.rb', line 17

def initialize(letters)
  @letters = letters
end

Instance Attribute Details

#lettersObject (readonly)

The letters that make up this word



8
9
10
# File 'lib/gaddag/word.rb', line 8

def letters
  @letters
end

Instance Method Details

#to_delimited_pathsArray<Path>

Constructs a list of delimited GADDAG paths from this word a delimiter, and a suffix: REV(PREFIX) ♢ SUFFIX

Returns:

  • (Array<Path>)

    a list of paths, each containing a reversed prefix,



30
31
32
33
34
35
36
# File 'lib/gaddag/word.rb', line 30

def to_delimited_paths
  1.upto(letters.length - 1).map do |index|
    reversed_prefix = @letters.slice(0, index).reverse
    suffix = @letters.slice(index, @letters.count)
    Path.new(reversed_prefix + [Path::DELIMITER] + suffix)
  end
end

#to_sObject

Returns the word as string

Returns:

  • a string representation of the word



23
24
25
# File 'lib/gaddag/word.rb', line 23

def to_s
  @letters.join
end