Class: ParaDice::Results::Number

Inherits:
Object
  • Object
show all
Defined in:
lib/para_dice/results/number.rb

Overview

Note:

this class could also be used for more complex operations using the reduce with operator and initial values. Blocks seem like another likely refinement. But I couldn’t come with a reason to use them yet that didn’t seem more confusing than coding a new result resolver

Constant Summary collapse

DEFAULT_INITIAL =

If not overridden the initial value is 0

0
DEFAULT_OPERATOR =

If not overridden the operator is :+

:+

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_val = DEFAULT_INITIAL, default_operator = DEFAULT_OPERATOR) ⇒ Number

Returns a new instance of Number.

Parameters:

  • default_operator (Numeric, Obj) (defaults to: DEFAULT_OPERATOR)
  • default_operator (Symbol) (defaults to: DEFAULT_OPERATOR)


25
26
27
28
# File 'lib/para_dice/results/number.rb', line 25

def initialize(initial_val = DEFAULT_INITIAL, default_operator = DEFAULT_OPERATOR)
  @default_initial_value = initial_val
  @default_operator = default_operator
end

Instance Attribute Details

#default_initial_valueNumeric, Obj

Returns:

  • (Numeric, Obj)


21
22
23
# File 'lib/para_dice/results/number.rb', line 21

def default_initial_value
  @default_initial_value
end

#default_operatorSymbol

Returns:

  • (Symbol)


21
# File 'lib/para_dice/results/number.rb', line 21

attr_accessor :default_initial_value, :default_operator

Instance Method Details

#resolve(faces, initial_value = default_initial_value, operator = default_operator) ⇒ Numeric, Object

Returns result of inject with faces, initial_value, and operator.

Parameters:

  • faces (Array<Numeric,Object>)
  • initial (Numeric, Object)

    value, defaults to DEFAULT_INITIAL = 0

  • operator, (Symbol)

    defaults to DEFAULT_OPERATOR = :+

Returns:

  • (Numeric, Object)

    result of inject with faces, initial_value, and operator



34
35
36
# File 'lib/para_dice/results/number.rb', line 34

def resolve(faces, initial_value = default_initial_value, operator = default_operator)
  faces.inject(initial_value, operator)
end