Class: ParaDice::Cup
- Inherits:
-
Object
- Object
- ParaDice::Cup
- Defined in:
- lib/para_dice/cup.rb
Overview
A Cup hold a selection of dice and a selection of readers. It’s main purpose
is to provide a reusable component that you can call #roll on and get a
complex result from
Instance Attribute Summary collapse
-
#Dice ⇒ Array<ParaDice::Die]
Array<ParaDice::Die].
- #readers ⇒ Array<#resolvce>
- #rng ⇒ Random
Instance Method Summary collapse
-
#roll(roll_rng = rng) ⇒ Object
roll reach die in cup and call each reader in turn on the results, return the result of the last reader.
-
#roll!(roll_rng = rng) ⇒ Object
roll each die in cup using roll_rng and return array of raw results from.
Instance Attribute Details
#Dice ⇒ Array<ParaDice::Die]
Returns Array<ParaDice::Die].
12 |
# File 'lib/para_dice/cup.rb', line 12 attribute :dice, Array[ParaDice::Die], default: [] |
#readers ⇒ Array<#resolvce>
20 |
# File 'lib/para_dice/cup.rb', line 20 attribute :readers, Array, default: [] |
#rng ⇒ Random
16 |
# File 'lib/para_dice/cup.rb', line 16 attribute :rng, Random, default: ->(*a) { Random.new } |
Instance Method Details
#roll(roll_rng = rng) ⇒ Object
roll reach die in cup and call each reader in turn on the results, return
the result of the last reader
25 26 27 |
# File 'lib/para_dice/cup.rb', line 25 def roll(roll_rng = rng) readers.reduce(roll!(roll_rng)) { |faces, reader| reader.resolve(faces) } end |
#roll!(roll_rng = rng) ⇒ Object
roll each die in cup using roll_rng and return array of raw results from
31 32 33 |
# File 'lib/para_dice/cup.rb', line 31 def roll!(roll_rng = rng) dice.map { |d| d.roll(roll_rng) } end |