Class: Dice

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

Overview

A Dice object represents a collection of individual Die objects. Defaults to two dice.

Instance Method Summary collapse

Constructor Details

#initialize(amount = 2, sides = 6) ⇒ Dice

Dice collection defaults to 2d6



5
6
7
# File 'lib/game_dice/dice.rb', line 5

def initialize(amount=2, sides=6)
  amount.times { self << Die.new(sides) }
end

Instance Method Details

#rollObject

Roll all the dice



10
11
12
# File 'lib/game_dice/dice.rb', line 10

def roll
  self.inject(0) { |total, current| total += current.roll }
end

#to_sObject



19
20
21
# File 'lib/game_dice/dice.rb', line 19

def to_s
  "A collection of dice #{self.collect { |i| i.last_roll }}, total: #{total}."
end

#totalObject

Check the current roll for all dice



15
16
17
# File 'lib/game_dice/dice.rb', line 15

def total
  self.inject(0) { |total, current| total += current.last_roll }
end