Module: Lexdrill::Rand

Defined in:
lib/lexdrill/rand.rb

Overview

Global "how often to actually show a word" setting, applied by drill next itself (both automatic hook-triggered calls and manual invocations respect it equally). Lives at ~/.drill.rand as a plain integer; defaults to 1 (show every time).

Constant Summary collapse

PATH =
File.join(Dir.home, ".drill.rand")
DEFAULT =
1

Class Method Summary collapse

Class Method Details

.set(denominator) ⇒ Object



18
19
20
# File 'lib/lexdrill/rand.rb', line 18

def self.set(denominator)
  File.write(PATH, denominator.to_s)
end

.skip?Boolean

True with probability (n-1)/n: "skip this show".

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/lexdrill/rand.rb', line 23

def self.skip?
  denominator = value
  return false if denominator <= 1

  Kernel.rand(denominator) != 0
end

.valueObject



11
12
13
14
15
16
# File 'lib/lexdrill/rand.rb', line 11

def self.value
  return DEFAULT unless File.exist?(PATH)

  denominator = File.read(PATH).strip.to_i
  denominator.positive? ? denominator : DEFAULT
end