Class: InEvery
- Inherits:
-
Object
- Object
- InEvery
- Defined in:
- lib/in_every.rb
Overview
Randomly execute a block of code x number of times in every number of runs Example: in_every = 5.times.in_every(20) do ##some really cool code end
#will return value of the block if executed or nil if nothing happened in_every.execute
Defined Under Namespace
Classes: SetupException
Instance Method Summary collapse
-
#execute(*args) ⇒ Object
execute the given proc if randomly.
-
#initialize(enum, executions, method) ⇒ InEvery
constructor
create new in every instance.
Constructor Details
#initialize(enum, executions, method) ⇒ InEvery
create new in every instance
==Parameters:
- enum
enumeration number for cycling through executions
- executions
number or times the proc is to be randomly executed
- method
What to execute randomly in a cycle
==Returns: new instance of InEvery
29 30 31 32 33 34 35 |
# File 'lib/in_every.rb', line 29 def initialize(enum, executions, method) @executions = (executions - 1) @enum = enum @method = method generate_occurances raise SetupException.new("occurancies must be greater than executions") if @enum.max > @executions end |
Instance Method Details
#execute(*args) ⇒ Object
execute the given proc if randomly
==Parameters:
- args
arguments required to execute the proc
==Returns: the result of the block or nil if nothing was executed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/in_every.rb', line 45 def execute *args @counter ||= 0 if @counter == @execution_points.first result = @method.call(*args) @execution_points.delete_at(0) end if @counter == @executions @counter = nil generate_occurances else @counter += 1 end result end |