Class: Dice
- Inherits:
-
Object
- Object
- Dice
- Defined in:
- lib/dice.rb
Overview
Class for working with the 5 dice at the same time
Instance Attribute Summary collapse
-
#values ⇒ Array
The dice.
Instance Method Summary collapse
-
#initialize(values = Array.new(5) {new_dice}) ⇒ Dice
constructor
A new instance of Dice.
- #roll(dice_to_roll) ⇒ void
- #roll_all ⇒ Dice
-
#to_s ⇒ String
(also: #display)
Instance variable dice.
Constructor Details
#initialize(values = Array.new(5) {new_dice}) ⇒ Dice
Returns a new instance of Dice.
5 6 7 8 |
# File 'lib/dice.rb', line 5 def initialize(values=Array.new(5) {new_dice}) # @param values [Array<Fixnum>] that contains 5 Fixnums check_dice values @values = values end |
Instance Attribute Details
#values ⇒ Array
Returns the dice.
3 4 5 |
# File 'lib/dice.rb', line 3 def values @values end |
Instance Method Details
#roll(dice_to_roll) ⇒ void
This method returns an undefined value.
15 16 17 18 19 20 |
# File 'lib/dice.rb', line 15 def roll(dice_to_roll) for index in dice_to_roll raise ArgumentError, "Illegal index" if index > 4 @values[index] = new_dice end end |
#roll_all ⇒ Dice
Note:
Rolls all the dice
27 28 29 |
# File 'lib/dice.rb', line 27 def roll_all initialize end |
#to_s ⇒ String Also known as: display
Returns instance variable dice.
32 33 34 |
# File 'lib/dice.rb', line 32 def to_s # @return [String] instance variable dice print @values end |