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.
Roll Methods collapse
Instance Method Summary collapse
-
#initialize(values = Array.new(5) {new_dice}) ⇒ Dice
constructor
A new instance of Dice.
- #to_s ⇒ Object (also: #display)
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.
17 18 19 20 21 22 |
# File 'lib/dice.rb', line 17 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 ⇒ void
Note:
Rolls all the dice
This method returns an undefined value.
29 |
# File 'lib/dice.rb', line 29 def roll_all; initialize; end |
#to_s ⇒ Object Also known as: display
33 34 35 |
# File 'lib/dice.rb', line 33 def to_s # @return [String] instance variable dice print @values end |