Class: SpeedCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/shunkuntype/speed.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpeedCheck

Returns a new instance of SpeedCheck.



15
16
17
18
19
20
21
22
# File 'lib/shunkuntype/speed.rb', line 15

def initialize
  @number = 20 #default 20
  @period = 60
  check_data_files
  data = mk_random_words
  t0,t1,count = exec_speed_check(data)
  keep_record(t0,t1,count)
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



3
4
5
# File 'lib/shunkuntype/speed.rb', line 3

def number
  @number
end

#periodObject (readonly)

Returns the value of attribute period.



3
4
5
# File 'lib/shunkuntype/speed.rb', line 3

def period
  @period
end

Instance Method Details

#check_data_filesObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shunkuntype/speed.rb', line 24

def check_data_files
  begin
    file=open(Shunkuntype::SPEED_FILE,"r")
    if file
      puts "#{Shunkuntype::SPEED_FILE} opened succcessfully"
    end
  rescue
    puts "#{Shunkuntype::SPEED_FILE} does not exist in this directory. --init or try in another dir."
    exit
  end
end

#exec_speed_check(data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shunkuntype/speed.rb', line 49

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

  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.chomp
    end
  end
  t1=Time.now
  return t0,t1,count
end

#keep_record(t0, t1, count) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/shunkuntype/speed.rb', line 72

def keep_record(t0,t1,count)
  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_wordsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shunkuntype/speed.rb', line 35

def mk_random_words
  data=[]
  data_dir=File.expand_path('../../../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 key positions



6
7
8
9
10
11
12
13
# File 'lib/shunkuntype/speed.rb', line 6

def print_keyboard
  content = <<-EOF
 q \\ w \\ e \\ r t \\ y u \\ i \\ o \\ p
a \\ s \\ d \\ f g \\ h j \\ k \\ l \\ ; enter
sh z \\ x \\ c \\ v b \\ n m \\ , \\\ . \\  shift
EOF
  print content
end