Module: Frequency

Defined in:
lib/frequency.rb

Overview

Module Frequency A small dsl written in ruby to work with frequency events (never, sometimes, always..)

Constant Summary collapse

NORMALLY =
0.75
SOMETIMES =
0.50
RARELY =
0.25

Instance Method Summary collapse

Instance Method Details

#always(cond = {}) ⇒ Object

always do something example:

always { puts "ok"}


12
13
14
# File 'lib/frequency.rb', line 12

def always(cond={})
  yield if block_given?
end

#never(cond = {}) ⇒ Object

never do something example:

never { puts "ok"}


52
53
54
# File 'lib/frequency.rb', line 52

def never(cond={})
  nil
end

#normally(cond = {}) ⇒ Object

normally (75%) do something, see sometimes method example:

normally { puts "ok"}


20
21
22
# File 'lib/frequency.rb', line 20

def normally(cond={})
  execute_with_probability(cond,NORMALLY ) { yield } if block_given?
end

#rarely(cond = {}) ⇒ Object

rarely (25%) do something, see sometimes method example:

rarely { puts "ok"}


45
46
47
# File 'lib/frequency.rb', line 45

def rarely(cond={})
  execute_with_probability(cond,RARELY   ) { yield } if block_given?
end

#sometimes(cond = {}) ⇒ Object Also known as: maybe

sometimes (50%) do something example:

sometimes do
   # some code
end

or adjusting the probability

sometimes :with_probability => 0.12 do
  # some code
end

you can use “12%” instead 0.12

sometimes(:with_probability => '13.6%') { ... }


35
36
37
# File 'lib/frequency.rb', line 35

def sometimes(cond={})
  execute_with_probability(cond,SOMETIMES) { yield } if block_given?
end