Class: Highscore::Wordlist
- Inherits:
-
Object
- Object
- Highscore::Wordlist
- Includes:
- Enumerable
- Defined in:
- lib/highscore/wordlist.rb
Overview
a basic list of words
Instance Attribute Summary collapse
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Class Method Summary collapse
-
.load(data) ⇒ Object
load a file or array of words.
-
.load_file(file_path) ⇒ Object
load a file of keywords.
Instance Method Summary collapse
-
#<<(word) ⇒ Object
add a new word to the blacklist.
-
#each ⇒ Object
iterate over words.
-
#include?(keyword) ⇒ Boolean
does the blacklist contain this keyword?.
-
#initialize(words = []) ⇒ Wordlist
constructor
A new instance of Wordlist.
-
#length ⇒ Object
count of ignored words.
-
#to_a ⇒ Object
get an array of blacklisted words.
Constructor Details
#initialize(words = []) ⇒ Wordlist
Returns a new instance of Wordlist.
37 38 39 |
# File 'lib/highscore/wordlist.rb', line 37 def initialize(words = []) @words = words end |
Instance Attribute Details
#words ⇒ Object (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
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
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
72 73 74 |
# File 'lib/highscore/wordlist.rb', line 72 def <<(word) @words << word end |
#each ⇒ Object
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?
65 66 67 |
# File 'lib/highscore/wordlist.rb', line 65 def include? keyword @words.include? keyword end |
#length ⇒ Object
count of ignored words
50 51 52 |
# File 'lib/highscore/wordlist.rb', line 50 def length @words.length end |
#to_a ⇒ Object
get an array of blacklisted words
57 58 59 |
# File 'lib/highscore/wordlist.rb', line 57 def to_a @words.to_a end |