Class: DiceBox::Cup
- Inherits:
-
Object
- Object
- DiceBox::Cup
- Defined in:
- lib/dice_box/cup.rb
Overview
Commodity class to use multiple Dice instances at the same time
Instance Attribute Summary collapse
-
#dices ⇒ Array
The Array of Dices.
-
#result ⇒ Integer
(also: #rolled, #rolled_value)
readonly
The result from the previous roll.
-
#rolled_sides ⇒ Array
readonly
The last rolled Sides of each Dice.
Instance Method Summary collapse
-
#initialize(dices = []) ⇒ Cup
constructor
A new instance of Cup.
-
#maximum ⇒ Integer
(also: #max)
Returns the highest value the Cup can roll.
-
#minimum ⇒ Integer
(also: #min)
Returns the lowest value the Cup can roll.
-
#roll ⇒ Integer
Rolls all the Dices in the Cup.
Constructor Details
#initialize(dices = []) ⇒ Cup
Returns a new instance of Cup.
19 20 21 22 |
# File 'lib/dice_box/cup.rb', line 19 def initialize(dices = []) @dices = dices @rolled_sides = [] end |
Instance Attribute Details
#dices ⇒ Array
Returns the Array of Dices.
6 7 8 |
# File 'lib/dice_box/cup.rb', line 6 def dices @dices end |
#result ⇒ Integer (readonly) Also known as: rolled, rolled_value
Returns the result from the previous roll.
14 15 16 |
# File 'lib/dice_box/cup.rb', line 14 def result @result end |
#rolled_sides ⇒ Array (readonly)
Returns the last rolled Sides of each Dice.
10 11 12 |
# File 'lib/dice_box/cup.rb', line 10 def rolled_sides @rolled_sides end |
Instance Method Details
#maximum ⇒ Integer Also known as: max
Returns the highest value the Cup can roll
35 36 37 |
# File 'lib/dice_box/cup.rb', line 35 def maximum dices.map(&:maximum).reduce(:+) end |
#minimum ⇒ Integer Also known as: min
Returns the lowest value the Cup can roll
42 43 44 |
# File 'lib/dice_box/cup.rb', line 42 def minimum dices.map(&:minimum).reduce(:+) end |
#roll ⇒ Integer
Rolls all the Dices in the Cup
26 27 28 29 30 31 |
# File 'lib/dice_box/cup.rb', line 26 def roll @result = dices.map { |dice| dice.roll }.reduce(&:+) @rolled_sides = dices.map(&:rolled_side) result end |