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

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



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_allDice

Note:

Rolls all the dice

Returns:



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

def roll_all
  initialize
end

#to_sString Also known as: display

Returns instance variable dice.

Returns:

  • (String)

    instance variable dice



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

def to_s # @return [String] instance variable dice

  print @values
end