Class: ParaDice::Results::CancelOpposing
- Inherits:
-
Object
- Object
- ParaDice::Results::CancelOpposing
- Defined in:
- lib/para_dice/results/cancel_opposing.rb
Overview
Results class to have some faces cancel other faces out of the results,
can also be used to drop faces entirely by defining them as their own opposite
Constant Summary collapse
- DEFAULT_OPPOSING =
set empty here so it will work if no values passed to it.
[]
Instance Attribute Summary collapse
-
#default_opposing ⇒ Object
Returns the value of attribute default_opposing.
-
#rw default_opposing(default_opposing) ⇒ Array<Array>
default to DEFAULT_OPPOSING = [].
Instance Method Summary collapse
-
#initialize(default_opposing = DEFAULT_OPPOSING) ⇒ CancelOpposing
constructor
A new instance of CancelOpposing.
- #resolve(faces, opposing = default_opposing) ⇒ Array<String>
Constructor Details
#initialize(default_opposing = DEFAULT_OPPOSING) ⇒ CancelOpposing
Returns a new instance of CancelOpposing.
19 20 21 |
# File 'lib/para_dice/results/cancel_opposing.rb', line 19 def initialize(default_opposing = DEFAULT_OPPOSING) @default_opposing = default_opposing end |
Instance Attribute Details
#default_opposing ⇒ Object
Returns the value of attribute default_opposing.
14 15 16 |
# File 'lib/para_dice/results/cancel_opposing.rb', line 14 def default_opposing @default_opposing end |
Instance Method Details
#resolve(faces, opposing = default_opposing) ⇒ Array<String>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/para_dice/results/cancel_opposing.rb', line 26 def resolve(faces, opposing = default_opposing) summary = Utility.to_summary(faces) opposing.each do |one_face, another_face| one_face = one_face.to_s another_face = another_face.to_s next unless summary.key?(one_face) && summary.key?(another_face) if summary[one_face] == summary[another_face] summary.delete(one_face) summary.delete(another_face) else low, high = [one_face, another_face].sort_by { |e| summary[e] } summary[high] -= summary[low] summary.delete(low) end end Utility.from_summary(summary) end |