Class: SpeedCheck
- Inherits:
-
Object
- Object
- SpeedCheck
- Defined in:
- lib/shunkuntype/speed.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Instance Method Summary collapse
- #check_data_files ⇒ Object
- #exec_speed_check(data) ⇒ Object
-
#initialize ⇒ SpeedCheck
constructor
A new instance of SpeedCheck.
- #keep_record(t0, t1, count) ⇒ Object
- #mk_random_words ⇒ Object
- #print_keyboard ⇒ Object
Constructor Details
#initialize ⇒ SpeedCheck
Returns a new instance of SpeedCheck.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/shunkuntype/speed.rb', line 7 def initialize @number = 20 #default 20 @period = 60 check_data_files data = mk_random_words t0, t1, count = exec_speed_check(data) # ここでnilチェック if t0.nil? || t1.nil? puts "Speed check was interrupted or no input was given." return end keep_record(t0, t1, count) end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
5 6 7 |
# File 'lib/shunkuntype/speed.rb', line 5 def number @number end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
5 6 7 |
# File 'lib/shunkuntype/speed.rb', line 5 def period @period end |
Instance Method Details
#check_data_files ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/shunkuntype/speed.rb', line 25 def check_data_files retried = false begin file = open(Shunkuntype::SPEED_FILE, "r") if file puts "#{Shunkuntype::SPEED_FILE} opened succcessfully" end rescue if retried puts "Failed to create #{Shunkuntype::SPEED_FILE}. Please check permissions." exit 1 end puts "#{Shunkuntype::SPEED_FILE} does not exist in this directory. Creating required files..." DataFiles.prepare retried = true retry end end |
#exec_speed_check(data) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/shunkuntype/speed.rb', line 58 def exec_speed_check(data) print "\n\n"+number.to_s+" words should be cleared." print "\nType return-key to start." p '' line = $stdin.gets return [nil, nil, nil] if line.nil? # breakの代わりにreturnで早期リターン line = line.chomp t0 = Time.now count = 0 @number.times do |i| print_keyboard() puts (i+1).to_s word = data[i] count += word.length while line != word do puts word p '' line = $stdin.gets return [t0, Time.now, count] if line.nil? # ここもnilチェック line = line.chomp end end t1 = Time.now return t0, t1, count end |
#keep_record(t0, t1, count) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/shunkuntype/speed.rb', line 85 def keep_record(t0, t1, count) return if t0.nil? || t1.nil? # テスト環境なら記録しない return if ENV['TEST'] == 'true' statement = t0.to_s + "," statement << @number.to_s + "," statement << (t1 - t0).to_s + "," icount = @period / (t1 - t0) * count statement << icount.to_s + "\n" data_file = open(Shunkuntype::SPEED_FILE, "a+") data_file << statement p statement printf("%5.3f sec\n", Time.now - t0) printf("%4d characters.\n", icount) end |
#mk_random_words ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/shunkuntype/speed.rb', line 44 def mk_random_words data=[] data_dir=File.('../../../lib/data', __FILE__) file=open("#{data_dir}/word.list",'r') while word=file.gets do data << word.chomp end data.shuffle! data.each do |word| print word+" " end return data end |
#print_keyboard ⇒ Object
21 22 23 |
# File 'lib/shunkuntype/speed.rb', line 21 def print_keyboard Shunkuntype.print_keyboard end |