Class: Teletype::Stats::Pair

Inherits:
Object
  • Object
show all
Defined in:
lib/teletype/stats.rb

Overview

It implements hit/miss rate for second keystroke. Key miss tends occur when preceded by a certain key.

Constant Summary collapse

THRESHOLD =
0.7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys, hit: 0, miss: 0, available: true) ⇒ Pair

Returns a new instance of Pair.



63
64
65
66
67
68
# File 'lib/teletype/stats.rb', line 63

def initialize(keys, hit: 0, miss: 0, available: true)
  @keys = keys
  @hit = hit
  @miss = miss
  @available = available
end

Instance Attribute Details

#availableObject

hit/miss is for the second key



61
62
63
# File 'lib/teletype/stats.rb', line 61

def available
  @available
end

#hitObject

hit/miss is for the second key



61
62
63
# File 'lib/teletype/stats.rb', line 61

def hit
  @hit
end

#keysObject

hit/miss is for the second key



61
62
63
# File 'lib/teletype/stats.rb', line 61

def keys
  @keys
end

#missObject

hit/miss is for the second key



61
62
63
# File 'lib/teletype/stats.rb', line 61

def miss
  @miss
end

Instance Method Details

#<=>(other) ⇒ Object



92
93
94
# File 'lib/teletype/stats.rb', line 92

def <=>(other)
  [rate, keys] <=> [other.rate, other.keys]
end

#hit!Object



74
75
76
# File 'lib/teletype/stats.rb', line 74

def hit!
  @hit += 1
end

#inefficient?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/teletype/stats.rb', line 70

def inefficient?
  rate < THRESHOLD
end

#miss!Object



78
79
80
# File 'lib/teletype/stats.rb', line 78

def miss!
  @miss += 1
end

#rateObject



82
83
84
85
86
# File 'lib/teletype/stats.rb', line 82

def rate
  return 0 if total.zero?

  hit / total.to_f
end

#to_sObject



96
97
98
# File 'lib/teletype/stats.rb', line 96

def to_s
  keys.gsub(/[\n\r]/, '↵').gsub(' ', '␣')
end

#totalObject



88
89
90
# File 'lib/teletype/stats.rb', line 88

def total
  hit + miss
end