Method: MTK::Patterns::Choice#initialize

Defined in:
lib/mtk/patterns/choice.rb

#initialize(elements, options = {}) ⇒ Choice

Returns a new instance of Choice.

Examples:

choose the second element twice as often as the first or third:

MTK::Pattern::Choice.new [:first,:second,:third], :weights => [1,2,1]

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

  • elements (Enumerable)

    the list of elements in the pattern

  • options (Hash) (defaults to: {})

    the pattern options

Options Hash (options):

  • :weights (Array)

    a list of chances that each corresponding element will be selected (normalized against the total weight)

  • :max_elements (Fixnum)

    the Pattern#max_elements (default is nil, which means unlimited)

  • :max_cycles (Fixnum)

    the Pattern#max_cycles (default is 1)



16
17
18
19
20
# File 'lib/mtk/patterns/choice.rb', line 16

def initialize(elements, options={})
  super
  @weights = options.fetch :weights, Array.new(@elements.length, 1)
  @total_weight = @weights.inject(:+).to_f
end