Class: Die
- Inherits:
-
Object
- Object
- Die
- 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
-
#last_roll ⇒ Object
readonly
The result of the last roll.
-
#sides ⇒ Object
readonly
The number of sides on the die.
Instance Method Summary collapse
-
#initialize(sides = 6) ⇒ Die
constructor
Create a new die object with the specified number of sides.
-
#roll ⇒ Object
Rolls the die and stores the result in the last_roll attribute.
- #to_s ⇒ Object
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_roll ⇒ Object (readonly)
The result of the last roll.
7 8 9 |
# File 'lib/game_dice/die.rb', line 7 def last_roll @last_roll end |
#sides ⇒ Object (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
#roll ⇒ Object
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_s ⇒ Object
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 |