Class: Die

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

Overview

Represents an individual die object with the specified number of sides. Defaults to a 6-sided die.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sides = 6) ⇒ Die

Create a new die object with the specified number of sides. Defaults to 6.



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

def initialize(sides=6)
  @sides = sides
  @last_roll = 0
end

Instance Attribute Details

#last_rollObject (readonly)

The result of the last roll.



7
8
9
# File 'lib/game_dice/die.rb', line 7

def last_roll
  @last_roll
end

#sidesObject (readonly)

The number of sides on the die.



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

def sides
  @sides
end

Instance Method Details

#rollObject

Rolls the die and stores the result in the last_roll attribute.



16
17
18
# File 'lib/game_dice/die.rb', line 16

def roll
  @last_roll = Array.new(@sides) { |i| i + 1 }.shuffle.first
end

#to_sObject



20
21
22
# File 'lib/game_dice/die.rb', line 20

def to_s
  "A single die with #{@sides} sides.  Currenly showing: #{@last_roll}."
end