Class: Typingtutor::Line
- Inherits:
-
Object
- Object
- Typingtutor::Line
- Defined in:
- lib/typingtutor/line.rb
Instance Method Summary collapse
- #actual_char ⇒ Object
- #expected_char ⇒ Object
-
#initialize(string) ⇒ Line
constructor
A new instance of Line.
- #ok? ⇒ Boolean
- #play ⇒ Object
- #print_backspace ⇒ Object
- #print_char ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(string) ⇒ Line
Returns a new instance of Line.
3 4 5 6 7 8 |
# File 'lib/typingtutor/line.rb', line 3 def initialize(string) @original = string @actual = "" @position = 0 @keystrokes = 0 end |
Instance Method Details
#actual_char ⇒ Object
45 |
# File 'lib/typingtutor/line.rb', line 45 def actual_char; @actual[@position]; end |
#expected_char ⇒ Object
44 |
# File 'lib/typingtutor/line.rb', line 44 def expected_char; @original[@position]; end |
#ok? ⇒ Boolean
46 |
# File 'lib/typingtutor/line.rb', line 46 def ok?; expected_char == actual_char; end |
#play ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/typingtutor/line.rb', line 10 def play print "\n#{@original.ljust(80)}\r" while(true) do char = STDIN.getch case char.ord when 3 then exit(1) # CTRL-C when 13 then break # enter when 127 # backspace @actual.chop! @position = [@position - 1, 0].max print_backspace else @actual += char print_char @position += 1 @keystrokes +=1 end end return stats end |
#print_backspace ⇒ Object
40 41 42 |
# File 'lib/typingtutor/line.rb', line 40 def print_backspace print "\b#{HighLine.color(expected_char, :plain)}\b" end |
#print_char ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/typingtutor/line.rb', line 32 def print_char if actual_char print HighLine.color(actual_char, ok? ? :correct : :error) else print HighLine.color(expected_char, :plain) end end |
#stats ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/typingtutor/line.rb', line 48 def stats { total_chars: @original.length, correct_chars: @original.length.times.select {|i| @original[i] == @actual[i] }.size, keystrokes: @keystrokes, total_words: @original.split(' ').size } end |