Module: Scientist

Defined in:
lib/scientist.rb,
lib/scientist/errors.rb,
lib/scientist/version.rb

Overview

Include this module into any class which requires science experiments in its methods. Provides the ‘science` and `default_scientist_context` methods for defining and running experiments.

If you need to run science on class methods, extend this module instead.

If including or extending this module are not an option, call ‘Scientist.run`.

Defined Under Namespace

Modules: Experiment Classes: BadBehavior, BehaviorMissing, BehaviorNotUnique, Default, NoValue, Observation, Result

Constant Summary collapse

VERSION =
"1.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(name, opts = {}) {|experiment| ... } ⇒ Object

Define and run a science experiment.

name - a String name for this experiment. opts - optional hash with the the named test to run instead of “control”,

:run is the only valid key.

Yields an object which implements the Scientist::Experiment interface. See ‘Scientist::Experiment.new` for how this is defined.

Returns the calculated value of the control experiment, or raises if an exception was raised.

Yields:

  • (experiment)


21
22
23
24
25
26
27
28
# File 'lib/scientist.rb', line 21

def self.run(name, opts = {})
  experiment = Experiment.new(name)

  yield experiment

  test = opts[:run] if opts
  experiment.run(test)
end

Instance Method Details

#default_scientist_contextObject

Public: the default context data for an experiment created and run via the ‘science` helper method. Override this in any class that includes Scientist to define your own behavior.

Returns a Hash.



55
56
57
# File 'lib/scientist.rb', line 55

def default_scientist_context
  {}
end

#science(name, opts = {}) ⇒ Object

Define and run a science experiment.

name - a String name for this experiment. opts - optional hash with the the named test to run instead of “control”,

:run is the only valid key.

Yields an object which implements the Scientist::Experiment interface. See ‘Scientist::Experiment.new` for how this is defined. The context from the `default_scientist_context` method will be applied to the experiment.

Returns the calculated value of the control experiment, or raises if an exception was raised.



42
43
44
45
46
47
48
# File 'lib/scientist.rb', line 42

def science(name, opts = {})
  Scientist.run(name, opts) do |experiment|
    experiment.context(default_scientist_context)

    yield experiment
  end
end