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.

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"1.0.0"

Instance Method Summary collapse

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.



33
34
35
# File 'lib/scientist.rb', line 33

def default_scientist_context
  {}
end

#science(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)


18
19
20
21
22
23
24
25
26
# File 'lib/scientist.rb', line 18

def science(name, opts = {})
  experiment = Experiment.new(name)
  experiment.context(default_scientist_context)

  yield experiment

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