Class: Typingtutor::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/typingtutor/dictionary.rb

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Returns a new instance of Dictionary.



4
5
6
7
8
9
10
11
12
# File 'lib/typingtutor/dictionary.rb', line 4

def initialize
  @letters = {}
  @words   = []

  files = Dir[File.join(File.dirname(__FILE__), '../../dictionary/*')]
  files.each do |file|
    IO.read(file).lines.each {|word| add(word)}
  end
end

Instance Method Details

#add(word) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/typingtutor/dictionary.rb', line 14

def add(word)
  return if word =~ /'s/
  word = word.strip
  @words << word
  word.chars.uniq.each do |letter|
    @letters[letter] ||= []
    @letters[letter] << word
  end
  word
rescue Exception => e
  puts "Error adding '#{word}': #{e.message}"
  raise e
end

#pick_wordObject



33
34
35
# File 'lib/typingtutor/dictionary.rb', line 33

def pick_word
  @words[rand(@words.size)]
end

#pick_word_with_letter(letter) ⇒ Object



28
29
30
31
# File 'lib/typingtutor/dictionary.rb', line 28

def pick_word_with_letter(letter)
  return nil if @letters[letter].nil?
  @letters[letter][rand(@letters[letter].size)]
end

#training_exerciseObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/typingtutor/dictionary.rb', line 37

def training_exercise
  lines = []
  line = ""
  250.times do
    line << (pick_word+" ")
    if line.size > 70
      lines << line.strip
      line = ""
    end
  end
  lines << line
  lines
end