monte_carlo 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba714d8eb65cabff56fc508ab075c62e0afaf188
4
- data.tar.gz: 0b5910159859348ebab617e7abc1ccb9e23ce18d
3
+ metadata.gz: 27d0d6f2d94ff5bae23bdce1a811746b7565ac72
4
+ data.tar.gz: 5abd18cc7ddaab12b7c3b3517bf116bff212e5ca
5
5
  SHA512:
6
- metadata.gz: 944b151e94f22a06db7ff167ee71897ea57436448c7e37dcf7dfcb0440e0aa34e050264183f2e03e5ae6d428b4a86820dd0c156fb5176d16c542e22453ed3596
7
- data.tar.gz: c2aae5f7d6a5ddafcbbea7bf54995d2180f6c0a1e7144541cffcbb1c61090ee99fc9a697adabe6f6cdb9d1cf340cdf7c0b0ff32e4d855776cf5efd7bc62ffe3e
6
+ metadata.gz: 622bf094b783f579e2562c7b1198c59471f20d14f2d3090a7f7e8c70a9d030cd80787dab016260a81c5bf99333ed336b31082f3deee072a61c575c64565712a9
7
+ data.tar.gz: d6ffd095586563317402af2b8e019763428f625aad0be7383e5839f1e15b7661c76604da6884da57b2b50d6679cd45ecc4a092d1f12920bfe8bd64de156e2b76
data/README.md CHANGED
@@ -24,6 +24,7 @@ Each experiment conatins:
24
24
  - `times`: the number of sample to create (defaults to 10,000)
25
25
  - `sample_method`: the method with which to generate a sample each iteration
26
26
  - `computation`: an optional coputation method to run on each sample to obtain a result
27
+ - `before_each` & `after_each`: optional methods to run before and after each iteration
27
28
 
28
29
  For example;
29
30
 
@@ -3,7 +3,7 @@ module MonteCarlo
3
3
 
4
4
  DEFAULT_TIMES = 10000
5
5
 
6
- attr_accessor :times, :sample_method, :computation
6
+ attr_accessor :times, :sample_method, :computation, :before_each, :after_each
7
7
 
8
8
  def self.run(times = DEFAULT_TIMES, &block)
9
9
  MonteCarlo::Experiment.new(times, &block).run
@@ -22,7 +22,12 @@ module MonteCarlo
22
22
  results = MonteCarlo::ExperimentResults.new
23
23
 
24
24
  @times.times do |index|
25
- results << run_sample(index)
25
+ begin
26
+ @before_each.call() unless @before_each.nil?
27
+ results << run_sample(index)
28
+ ensure
29
+ @after_each.call() unless @after_each.nil?
30
+ end
26
31
  end
27
32
 
28
33
  results
@@ -1,3 +1,3 @@
1
1
  module MonteCarlo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,12 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe MonteCarlo::Experiment do
4
4
 
5
- let(:times) { 1000 }
5
+ let(:times) { 10 }
6
6
  let(:sample_value) { 1 }
7
7
  let(:computation) { -> (sample) {sample * 2} }
8
8
  let(:experiment) do
9
9
  experiment = MonteCarlo::Experiment.new
10
- experiment.times = 1000
10
+ experiment.times = times
11
11
  experiment.sample_method = -> { sample_value }
12
12
  experiment.computation = computation
13
13
  experiment
@@ -44,4 +44,32 @@ describe MonteCarlo::Experiment do
44
44
  end
45
45
  end
46
46
 
47
+ describe :before_each do
48
+ let(:before) { double(call: nil) }
49
+
50
+ before { experiment.before_each = before }
51
+
52
+ it 'should run the before_each method before each run' do
53
+ expect(before).to receive(:call).exactly(times).times
54
+ experiment.run
55
+ end
56
+ end
57
+
58
+ describe :after_each do
59
+ let(:after) { double(call: nil) }
60
+
61
+ before { experiment.after_each = after }
62
+
63
+ it 'should run the after_each method after each run' do
64
+ expect(after).to receive(:call).exactly(times).times
65
+ experiment.run
66
+ end
67
+
68
+ it 'should run even when sample method raises exception' do
69
+ experiment.sample_method = -> { raise }
70
+ expect(after).to receive(:call).exactly(:once)
71
+ experiment.run rescue nil
72
+ end
73
+ end
74
+
47
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monte_carlo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Assaf Gelber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-29 00:00:00.000000000 Z
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler