Class: Highscore::Wordlist

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/highscore/wordlist.rb

Overview

a basic list of words

Direct Known Subclasses

Blacklist, Whitelist

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words = []) ⇒ Wordlist

Returns a new instance of Wordlist.

Parameters:

  • words (defaults to: [])

    Array



37
38
39
# File 'lib/highscore/wordlist.rb', line 37

def initialize(words = [])
  @words = words
end

Instance Attribute Details

#wordsObject (readonly)

Returns the value of attribute words.



34
35
36
# File 'lib/highscore/wordlist.rb', line 34

def words
  @words
end

Class Method Details

.load(data) ⇒ Object

load a file or array of words

Parameters:

  • data

    String Array

Returns:

  • Highscore::Wordlist



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/highscore/wordlist.rb', line 20

def self.load(data)
  if data.instance_of?(String)
    words = data.split(' ')
  elsif data.instance_of? Array
    words = data
  else
    raise ArgumentError, "don't know how to handle a %s class" % data.class
  end

  words.map! {|x| x.gsub(/[\!\.\:\,\;\-\+]/, '') }

  self.new(words)
end

.load_file(file_path) ⇒ Object

load a file of keywords

Parameters:

  • file_path

    String

Returns:

  • Highscore::Wordlist



11
12
13
14
# File 'lib/highscore/wordlist.rb', line 11

def self.load_file file_path
  words = File.read(file_path).split(' ')
  self.load(words)
end

Instance Method Details

#<<(word) ⇒ Object

add a new word to the blacklist

Parameters:

  • word

    String



72
73
74
# File 'lib/highscore/wordlist.rb', line 72

def <<(word)
  @words << word
end

#eachObject

iterate over words



43
44
45
# File 'lib/highscore/wordlist.rb', line 43

def each
  @words.each {|word| yield word }
end

#include?(keyword) ⇒ Boolean

does the blacklist contain this keyword?

Parameters:

  • keyword

    String

Returns:

  • (Boolean)

    true/false



65
66
67
# File 'lib/highscore/wordlist.rb', line 65

def include? keyword
  @words.include? keyword
end

#lengthObject

count of ignored words

Returns:

  • Fixnum



50
51
52
# File 'lib/highscore/wordlist.rb', line 50

def length
  @words.length
end

#to_aObject

get an array of blacklisted words

Returns:

  • Array



57
58
59
# File 'lib/highscore/wordlist.rb', line 57

def to_a
  @words.to_a
end