Class: Dice

Inherits:
Object
  • Object
show all
Defined in:
lib/dice.rb

Overview

Class for working with the 5 dice at the same time

Instance Attribute Summary collapse

Roll Methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = Array.new(5) {new_dice}) ⇒ Dice

Returns a new instance of Dice.

Parameters:

  • values (Array<Fixnum>) (defaults to: Array.new(5) {new_dice})

    that contains 5 Fixnums



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

#valuesArray

Returns the dice.

Returns:

  • (Array)

    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.

Parameters:

  • i (Array<Fixnum>)

    < 4

Raises:

  • (ArgumentError)

    if i element > 4



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_allvoid

Note:

Rolls all the dice

This method returns an undefined value.



29
# File 'lib/dice.rb', line 29

def roll_all; initialize; end

#to_sObject Also known as: display



33
34
35
# File 'lib/dice.rb', line 33

def to_s # @return [String] instance variable dice
  print @values
end