Module: Math

Defined in:
lib/monkey_patches/math.rb

Class Method Summary collapse

Class Method Details

.natural_logarithmic_sleep(set, constant_multiplier = 1, stepping_factor = 1, debugging = false) ⇒ Object

Performs a logarithmic sleep based on a set of integers or up to an input integer



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/monkey_patches/math.rb', line 3

def Math.natural_logarithmic_sleep(set, constant_multiplier=1, stepping_factor=1, debugging=false)
  require 'Logger'
  logger = Logger.new(STDOUT)
  logger.level = Logger::DEBUG

  total_sleep_time = 0
  if !set.is_a? Array and !set.is_a? Range
    set = (1 .. set)
  end

  logger.debug("Constant multiplier for logarithmic scale is set to #{constant_multiplier}") if debugging
  logger.debug("Stepping factor is #{stepping_factor}") if stepping_factor > 1 and stepping_factor != set.size and debugging

  set.step(stepping_factor) do |iteration|
    logarithm = Math.log iteration
    sleep_time = logarithm * constant_multiplier
    logger.debug("Iteration #{iteration} sleeping for #{sleep_time} seconds (zZz)") if debugging
    yield sleep_time, iteration, stepping_factor
    sleep(sleep_time) unless debugging
    total_sleep_time += sleep_time

  end
  logger.debug("Slept for #{total_sleep_time} seconds (zZzZz)") if debugging
end